Thread Safe Singletons in Java

24,601

Answer 1: static synchronized methods use the class object as the lock - ie in this case Singleton.class.

Answer 2: The java language, among other things:

  • loads classes when they are first accessed/used
  • guarantees that before access to a class is allowed, all static initializers have completed

These two facts mean that the inner static class SingletonHolder is not loaded until the getInstance() method is called. At that moment, and before the thread making the call is given access to it, the static instance of that class is instantiated as part of class loading.

This all means we have safe lazy loading, and without any need for synchronization/locks!

This pattern is the pattern to use for singletons. It beats other patterns because MyClass.getInstance() is the defacto industry standard for singletons - everyone who uses it automatically knows that they are dealing with a singleton (with code, it's always good to be obvious), so this pattern has the right API and the right implementation under the hood.

btw Bill Pugh's article is worth reading for completeness when understanding singleton patterns.

Share:
24,601

Related videos on Youtube

donnyton
Author by

donnyton

Updated on July 09, 2022

Comments

  • donnyton
    donnyton almost 2 years

    The wikipedia article on Singletons mentions a few thread safe ways to implement the structure in Java. For my questions, let's consider Singletons that have lengthy initialization procedures and are acccessed by many threads at once.

    Firstly, is this unmentioned method thread-safe, and if so, what does it synchronize on?

    public class Singleton {
        private Singleton instance;
    
        private Singleton() {
            //lots of initialization code
        }
    
        public static synchronized Singleton getInstance() {
            if(instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    

    Secondly, why is the following implementation thread safe AND lazy in initialization? What exactly happens if two threads enter the getInstance() method at the same time?

    public class Singleton {
        private Singleton() {
            //lots of initialization code
        }
    
        private static class SingletonHolder { 
            public static final Singleton instance = new Singleton();
        }
    
        public static Singleton getInstance() {
            return SingletonHolder.instance;
        }
    }
    

    Finally, in the second example, what if one thread gets an instance first and another thread gets an instance and tries to perform actions on it before the constructor has finished in the first thread? Can you get into an unsafe state then?

  • donnyton
    donnyton almost 13 years
    After reading that article, am I correct in saying that Java does some "internal" synchronization when loading static fields?
  • Matt Ball
    Matt Ball almost 13 years
    "This pattern is the pattern to use for singletons." - and what about Effective Java's enum singleton?
  • Bohemian
    Bohemian almost 13 years
    The enum singleton IMHO isn't as clean or obvious as this one and is therefore not as good: MyClass.getInstance() is the industry standard for singletons - everyone who uses it automatically (should) knows that they are dealing with a singleton. This pattern has the right API and the right implementation under the hood.
  • Bohemian
    Bohemian almost 13 years
    @donnyton - yes the class loader employes its own synchronization - think of it a bit like calling a static synchronized loadClass() method`on the class being loaded.