京峰LInux運維安全高級架構師班
// 創(chuàng)建單例class SingletonObject {
? ?private static SingletonObject SINGLETON_INSTANCE;
? ?public static SingletonObject getInstance() {
? ? ? ?// 創(chuàng)建過程非常昂貴,希望第一次使用的時候才去創(chuàng)建
? ? ? ?// check-then-act
? ? ? ?// 多線程模式下可能創(chuàng)建多個實例
? ? ? ?if (SINGLETON_INSTANCE == null) {
? ? ? ? ? ?SINGLETON_INSTANCE = new SingletonObject();
? ? ? ?}
? ? ? ?return SINGLETON_INSTANCE;
? ?}
標簽: