Java Singleton and Synchronization Java Singleton and Synchronization multithreading multithreading

Java Singleton and Synchronization


Yes, it is necessary. There are several methods you can use to achieve thread safety with lazy initialization:

Draconian synchronization:

private static YourObject instance;public static synchronized YourObject getInstance() {    if (instance == null) {        instance = new YourObject();    }    return instance;}

This solution requires that every thread be synchronized when in reality only the first few need to be.

Double check synchronization:

private static final Object lock = new Object();private static volatile YourObject instance;public static YourObject getInstance() {    YourObject r = instance;    if (r == null) {        synchronized (lock) {    // While we were waiting for the lock, another             r = instance;        // thread may have instantiated the object.            if (r == null) {                  r = new YourObject();                instance = r;            }        }    }    return r;}

This solution ensures that only the first few threads that try to acquire your singleton have to go through the process of acquiring the lock.

Initialization on Demand:

private static class InstanceHolder {    private static final YourObject instance = new YourObject();}public static YourObject getInstance() {    return InstanceHolder.instance;}

This solution takes advantage of the Java memory model's guarantees about class initialization to ensure thread safety. Each class can only be loaded once, and it will only be loaded when it is needed. That means that the first time getInstance is called, InstanceHolder will be loaded and instance will be created, and since this is controlled by ClassLoaders, no additional synchronization is necessary.


This pattern does a thread-safe lazy-initialization of the instance without explicit synchronization!

public class MySingleton {     private static class Loader {         static final MySingleton INSTANCE = new MySingleton();     }     private MySingleton () {}     public static MySingleton getInstance() {         return Loader.INSTANCE;     }}

It works because it uses the class loader to do all the synchronization for you for free: The class MySingleton.Loader is first accessed inside the getInstance() method, so the Loader class loads when getInstance() is called for the first time. Further, the class loader guarantees that all static initialization is complete before you get access to the class - that's what gives you thread-safety.

It's like magic.

It's actually very similar to the enum pattern of Jhurtado, but I find the enum pattern an abuse of the enum concept (although it does work)


If you are working on a multithreaded environment in Java and need to gurantee all those threads are accessing a single instance of a class you can use an Enum. This will have the added advantage of helping you handle serialization.

public enum Singleton {    SINGLE;    public void myMethod(){      }}

and then just have your threads use your instance like:

Singleton.SINGLE.myMethod();