单例模式-双检锁/双重校验锁

双检锁/双重校验锁(DCL,即 double-checked locking)

JDK 版本 JDK1.5 起
是否 Lazy 初始化
是否多线程安全
实现难度 较复杂

描述:这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
getInstance() 的性能对应用程序很关键。


/**
 * 
 * @version 1.0.0
 * @date 2019\8\4  20:29
 */
public class Singleton {
    private static volatile Singleton instance;
    private Singleton() { }

    public static Singleton getInstance(){
        if (instance==null){
            synchronized (Singleton.class){
                if (instance==null){
                    instance=new Singleton();
                }
            }
        }
        return instance;
    }

}

发布了23 篇原创文章 · 获赞 1 · 访问量 3139

猜你喜欢

转载自blog.csdn.net/qq_43669912/article/details/98474719