Why is volatile used in double checked locking

后端 未结 7 988
太阳男子
太阳男子 2020-11-22 05:01

From Head First design patterns book, the singleton pattern with double checked locking has been implemented as below:

public class Singleton {
            


        
7条回答
  •  温柔的废话
    2020-11-22 05:34

    As quoted by @irreputable, volatile is not expensive. Even if it is expensive, consistency should be given priority over performance.

    There is one more clean elegant way for Lazy Singletons.

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

    Source article : Initialization-on-demand_holder_idiom from wikipedia

    In software engineering, the Initialization on Demand Holder (design pattern) idiom is a lazy-loaded singleton. In all versions of Java, the idiom enables a safe, highly concurrent lazy initialization with good performance

    Since the class does not have any static variables to initialize, the initialization completes trivially.

    The static class definition LazyHolder within it is not initialized until the JVM determines that LazyHolder must be executed.

    The static class LazyHolder is only executed when the static method getInstance is invoked on the class Singleton, and the first time this happens the JVM will load and initialize the LazyHolder class.

    This solution is thread-safe without requiring special language constructs (i.e. volatile or synchronized).

提交回复
热议问题