可以使用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;
}
}
网友评论