DCL双检查锁机制实现的线程安全的单例模式

public class MyObject {
    
    private volatile static MyObject instance;
    
    private MyObject(){}
    
    public static MyObject getInstance(){
        try {
            if(instance != null){
                return instance;
            }else{synchronized ( instance.class) {
                    if(instance == null){
                        instance = new MyObject();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

}

 

posted @ 2016-08-31 15:08  Acode  阅读(497)  评论(0编辑  收藏  举报
您是本站第访问量位访问者!