首页 > 其他 > 详细

单例模式,双重校验的懒汉式

时间:2019-10-04 17:59:10      阅读:95      评论:0      收藏:0      [点我收藏+]
/**
 * 单例模式,双重校验的懒汉式
 */
public class bTestSingleton {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1==s2);
    }
}


class Singleton{
    private  Singleton(){

    }

    private static Singleton instance = null;

    public static Singleton getInstance(){
        if(instance==null) {   //为了不让其他的做无谓的等待,不是空的话直接返回
            synchronized (Singleton.class) {  //线程安全
                if (instance == null) {       //是空的时候 创建
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

单例模式,双重校验的懒汉式

原文:https://www.cnblogs.com/liyao0312/p/11622658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!