DCL¶
Не ленивая инициализация¶
Показать код
class Singleton {
private final static Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
Не Thread-safe Синглтон¶
Показать код
class Singleton {
private static Singleton INSTANCE;
private Singleton() {}
public static Singleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
}
Синхронизированный Синглтон¶
Показать код
class Singleton {
private static Singleton INSTANCE;
private Singleton() {}
public synchronized static Singleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
}
DCL (double check locking) Синглтон¶
Показать код
class Singleton {
private volatile static Singleton INSTANCE;
private Singleton() {}
public static Singleton getInstance() {
if (INSTANCE == null) { // Приходят 3 потока
synchronized (Singleton.class) { // Первый заходит, остальные ждут и заходят по очереди
if (INSTANCE == null) { // Первый проходит. Остальные видят изменившееся значение, так как volatile
INSTANCE = new Singleton(); // Инициализирует только первый
}
}
}
return INSTANCE;
}
}
DCL с оптимизацией (+ 20-25%)¶
Показать код
class Singleton {
private volatile static Singleton INSTANCE;
private Singleton() {}
public static Singleton getInstance() {
Singleton localRef = INSTANCE;
if (localRef == null) {
synchronized (Singleton.class) {
localRef = INSTANCE;
if (localRef == null) {
localRef = new Singleton();
INSTANCE = localRef;
}
}
}
return localRef;
}
}
Lazy static load Singleton¶
Показать код
class Singleton {
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
private final static Singleton INSTANCE = new Singleton();
}
}