单例模式

代码

public class Singleton {
    private static volatile Singleton singleton;

    private Singleton(){}

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

volatile关键字详解

posted @ 2018-03-07 13:50  The_shy  阅读(127)  评论(0编辑  收藏  举报