How to implement thread-safe lazy initialization?

This can be done in lock-free manner by using AtomicReference as instance holder:

// in class declaration
private AtomicReference<Foo> instance = new AtomicReference<>(null);  

public Foo getInstance() {
   Foo foo = instance.get();
   if (foo == null) {
       foo = new Foo();                       // create and initialize actual instance
       if (instance.compareAndSet(null, foo)) // CAS succeeded
           return foo;
       else                                   // CAS failed: other thread set an object 
           return instance.get();             
   } else {
       return foo;
   }
}

Main disadvantage here is that multiple threads can concurrently instantiate two or more Foo objects, and only one will be lucky to be set up, so if instantiation requires I/O or another shared resource, this method may not be suitable.

At the other side, this approach is lock-free and wait-free: if one thread which first entered this method is stuck, it won't affect execution of others.


For singletons there is an elegant solution by delegating the task to the JVM code for static initialization.

public class Something {
    private Something() {
    }

    private static class LazyHolder {
            public static final Something INSTANCE = new Something();
    }

    public static Something getInstance() {
            return LazyHolder.INSTANCE;
    }
}

see

http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom

and this blog post of Crazy Bob Lee

http://blog.crazybob.org/2007/01/lazy-loading-singletons.html


If you're using Apache Commons Lang, then you can use one of the variations of ConcurrentInitializer like LazyInitializer.

Example:

ConcurrentInitializer<Foo> lazyInitializer = new LazyInitializer<Foo>() {

        @Override
        protected Foo initialize() throws ConcurrentException {
            return new Foo();
        }
    };

You can now safely get Foo (gets initialized only once):

Foo instance = lazyInitializer.get();

If you're using Google's Guava:

Supplier<Foo> fooSupplier = Suppliers.memoize(new Supplier<Foo>() {
    public Foo get() {
        return new Foo();
    }
});

Then call it by Foo f = fooSupplier.get();

From Suppliers.memoize javadoc:

Returns a supplier which caches the instance retrieved during the first call to get() and returns that value on subsequent calls to get(). The returned supplier is thread-safe. The delegate's get() method will be invoked at most once. If delegate is an instance created by an earlier call to memoize, it is returned directly.


The easiest way is to use a static inner holder class :

public class Singleton {

    private Singleton() {
    }

    public static Singleton getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final Singleton INSTANCE = new Singleton();
    }
}