977
技術社區[雲棲]
線程安全的單例模式
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if(uniqueInstance == null) { //(1)
//隻有第一次才徹底執行這裏的代碼
synchronized() {
//再檢查一次
if(uniqueInstance == null)
uniqueInstance = new Singleton();
}
}
return uniqueInstance;
}
}
最後更新:2017-04-02 06:52:15