我对这个话题进行了一些调查,但是我仍然看不到Double Checked Locking singleton可能比Holder上的singleton更好的选择。
如果有人能给我一个例子,那就太好了。
DCL
public class Singleton {
public static volatile Singleton INSTANCE = null;
private Singleton(){
}
public static Singleton getInstance(){
if (INSTANCE == null){
synchronized (Singleton.class) {
if (INSTANCE == null){
INSTANCE = new Singleton();
}
}
}
return INSTANCE;
}
}
HOLDER
public class Singleton {
private Singleton() {
}
private static class Holder {
public static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance(){
return Holder.INSTANCE;
}
}