饿汉模式
public class HungrySingleton {
private static HungrySingleton hungrySingleton = new HungrySingleton();
private HungrySingleton() {
}
public static HungrySingleton getN(){
return hungrySingleton;
}
}
懒汉模式
public class LazySingleton {
private static volatile LazySingleton instance = null;
private LazySingleton() {
}
public synchronized static LazySingleton getN(){
if(instance == null){
instance = new LazySingleton();
}
return instance;
}
}