单例设计模式
1、
public class Singleton1 {
private static Singleton1 uniqueInstance = new Singleton1();
private Singleton1(){
}
public static Singleton1 getInstance() {
return uniqueInstance;
}
}
2、
public class Singleton2 {
private volatile static Singleton2 uniqueInstance;
private Singleton2(){
}
public static Singleton2 getInstance(){
if(uniqueInstance == null){
synchronized (Singleton2.class) {
if(uniqueInstance == null){
uniqueInstance = new Singleton2();
}
}
}
return uniqueInstance;
}
}