2.DCL单例模式

1.首先vlolatile是禁止指令进行重排优化的:

public class Singleton {
    private volatile static Singleton Instance;
    public static Singleton getInstance(){
        if(Instance == null){
            synchronized (Singleton.class){
                if(Instance==null){
                    Instance=new Singleton();
                }
            }
        }
        return Instance;
    }
    public static void main(String[] args){
            Singleton.getInstance();
    }
}

DCL单例模式如上所示:

进行反编译过程如下所示:

未完待续

猜你喜欢

转载自blog.csdn.net/qq_35561207/article/details/84307680