DCL思想单列模式
public class Singleton{
private volatile static Singleton instance;
private Singleton(){};
public static Singleton getInstance(){
if(instance==null){
sychronized(Singleton.class){
if(instance==null)
instance=new Singleton();
}
}
return instatnce;
}
}
推荐使用
内部类单例模式实现
public class Singleton{
private Singleton(){}
public static Singleton getInstance(){
return SingletonHolder.sInstance;
}
/**
*静态内部类
*/
private static class SingletonHolder {
private static final Singleton sInstance = new Singleton();
}
}