饿汉式单例模式(线程安全)
class LazySingleton {
private static LazySingleton ls = null;
private LazySingleton() {
}
public static synchronized LazySingleton getInstance() {
if (ls == null) {
ls = new LazySingleton();
}
return ls;
}
}
饿汉式单例模式(线程安全双锁)
class DoubleLockSingleton {
private static DoubleLockSingleton ds = null;
private DoubleLockSingleton() {
}
public static DoubleLockSingleton getInstance() {
if (ds == null) {
synchronized (DoubleLockSingleton.class) {
if (ds == null) {
ds = new DoubleLockSingleton();
}
}
}
return ds;
}
}