作者写的太好了,无法超越
https://www.jianshu.com/p/b99e870f4ce0
public class Singleton {
private volatile static Singleton singleton;
private Singleton() {
}
public static Singleton getInsatnce() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
静态内部类实现单例模式
public class Singleton {
private static class SingletonHolder {
private static final Singleton singleton = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.singleton;
}
}