单例模式:
public class Singleton{
private static class SingletonHolder{
final static Singleton INSTANCE = new Singleton();
}
private Singleton(){};
public static Singleton getInstance(){
return SingletonHolder.INSTANCE ;
}
}
jvm在加载类SingletonHolder时会初始化INSTANCE ,这样就保证了Singleton是lazy load又是单例的,在多线程的情况下也是一个实例。
public class Singleton{
private static class SingletonHolder{
final static Singleton INSTANCE = new Singleton();
}
private Singleton(){};
public static Singleton getInstance(){
return SingletonHolder.INSTANCE ;
}
}
jvm在加载类SingletonHolder时会初始化INSTANCE ,这样就保证了Singleton是lazy load又是单例的,在多线程的情况下也是一个实例。