静态内部类
public class InnerSingleton() {
private static class Singleton {
private static Singleton single = new Singleton();
}
private InnerSingleton() {}
public static Singleton getInstance() {
return Singleton.single;
}
}
双重检查锁
public class DubbleSingleton {
private static DubbleSingleton ds;
public static DubbleSingleton getDs () {
if (ds == null) {
synchronized (DubbleSingleton.class) {
if (ds == null) {
ds = new DubbleSingleton();
}
}
}
return ds;
}
}