单例模式详情
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
}
}