单例模式实现
方法一:双重校验锁
public class Singleton{
private volatile static Singleton instance=null;
private Singleton{
}
public static Singleton getInstance(){
if(null==instance){
synchronized(Singleton.class){
if(null==instance){
instance=new Singleton();
}
}
}
return instance;
}
}
方法二:静态内部类
public class Singleton{
private Singleton{
}
public static class Inner{
private static Singleton instance=new Singleton();
}
public static Singleton getInstance(){
return Inner.instance;
}
}
网友评论