参考文章:https://liuyanzhao.com/3212.html
public class Singleton {
private static class LazyHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}
public class Singleton {
private static class LazyHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}
2017-11-17
加双锁,或者内部静态类就能实现线程安全。还有种不常用却是最好的方式就是枚举,从jvm层面保证。不要谢我,请叫我红领巾。。。
2017-09-06