天天看點

java 枚舉法單例模式_JAVA三種實作單例模式方法(三):枚舉式實作單例設計模式...

package instance.enumStyle;

public class InstanceEnumStyle {

private InstanceEnumStyle() {}

public static InstanceEnumStyle getInstance() {

return Singleton.INSTANCE.getSingle();

}

private static enum Singleton {

INSTANCE;

private InstanceEnumStyle instance;

private Singleton () {

instance = new InstanceEnumStyle();

}

public InstanceEnumStyle getSingle() {

return instance;

}

}

public static void main(String[] args) {

for (int i = 0; i < 3; i++) {

new Thread(new Runnable() {

@Override

public void run() {

InstanceEnumStyle instanceEnumStyle = InstanceEnumStyle.getInstance();

System.out.println("hashCode:" + instanceEnumStyle.hashCode());

}

}).start();

}

}

}

測試結果:

hashCode:1575346308

hashCode:1575346308

hashCode:1575346308

更多内容:

https://www.cnblogs.com/linjiaxin/p/7923135.html