public class Singleton {
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance_0(){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
return instance;
}
public static Singleton getInstance_1(){
if(instance == null){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
区别在于第一次的时候instance都是null,多线程就会产生两个实例。