[concurrency-interest] AtomicBoolean and Double-Checking Locking? Is this Singleton safe?

John D. Heintz jheintz at gmail.com
Tue Jun 28 14:17:15 EDT 2005

Hello all,

I've been waiting for a bunch of new Double-Checked locking articles
to come out since the release of the java.util.concurrent.atomic
package, but I've seen very little ;-)

The most recent discussion I could find was
but of course I may have missed something.

I have a simple question: can an AtomicBoolean check _safely_ replace
the '== null' from yester-years un-safe Double-Checked Locking
Singleton idiom?

Here's an example Singleton implementation to show what I mean.
import java.util.concurrent.atomic.AtomicBoolean;

public class Singleton {
	static Singleton _instance;
	static AtomicBoolean _instanceFlag = new AtomicBoolean(false);
	public Singleton() {
	static Singleton getInstance() {
		if (_instanceFlag.get() != true) {

			synchronized (_instanceFlag) { // block others until finished constructing
				// double check
				if (_instanceFlag.get() != true) {
					_instance = new Singleton();
					if (!_instanceFlag.compareAndSet(false, true)) {
						throw new IllegalStateException("Ack! Can't fail this call!!!");
		return _instance;

Thanks all,

John D. Heintz
Software Craftsman
Austin, TX
(512) 633-1198

jheintz at pobox.com

More information about the Concurrency-interest mailing list