单例模式详情

 

package test;

public class Singleton {

private Singleton() {
};

private static Singleton single = null;

public static Singleton getInstance() {

if (null == single) {

synchronized (Singleton.class) {

if (null == single) {

single = new Singleton();
}
}

}
return single;
}

public static void main(String[] args) {

Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if(s1.equals(s2)){
System.out.println("tongyige"); //运行代码显示tongyige
}else{
System.out.println("botong");
}

System.out.println(s1 == s2); //true
}

}

posted on 2017-10-16 23:34  尴尬而不失礼貌的微笑  阅读(229)  评论(0编辑  收藏  举报