public static Singleton getInstance() {
if (instance == null) {
synchronized(instance) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
1 回答
Genment
TA贡献43条经验 获得超25个赞
public class Singleton { private volatile static Singleton singleton; private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { // <<- 你的instance是null,不能使用null作为锁 if (singleton == null) { singleton = new Singleton(); } } } return singleton; } }
添加回答
举报
0/150
提交
取消