package singleton;
public class SingletonStaticInnerClass {
private SingletonStaticInnerClass() {}
// 利用jvm的靜態(tài)內(nèi)部類的特性來(lái)完成單例模式
private static class SingletonInstance {
private final static SingletonStaticInnerClass INSTANCE = new SingletonStaticInnerClass();
}
public static SingletonStaticInnerClass getInstance() {
return SingletonInstance.INSTANCE;
}
}
優(yōu)點(diǎn)
- 線程安全音比,采用了類裝載機(jī)制保證初始化時(shí)只有一個(gè)線程
- 符合懶加載模式陵刹,內(nèi)部靜態(tài)類在調(diào)用getInstance()方法時(shí)才會(huì)裝載