双重检查锁定(double-checked locking)与单
2018-07-18 本文已影响1人
青城楼主
可以使用volatile变量禁止指令重排序,让DCL生效:
[java] view plain copy
package com.zzj.pattern.singleton;
public class Singleton {
private static volatile Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance =new Singleton();
}
}
}
return instance;
}
}