synchronized 问题
来源:13-12 JMM的应用

qq_雨声_2
2019-10-25
public class Singleton6 {
private volatile static Singleton6 instance;
private Singleton6(){
}
public static Singleton6 getInstance() {
if (instance == null) {
synchronized (Singleton6.class){
if(instance == null){
instance = new Singleton6();
}
}
}
return instance;
}
}
synchronized 加在方法上就没有重排序的问题了吗,synchronized 不是可以防止重排序的吗?
写回答
1回答
-
悟空
2019-10-25
你想问的是加在方法上,会不会有重排序的问题?如果加在方法上,两个线程会串行获取单例,不会有问题的,但是效率稍低一些。
052020-04-20
相似问题
重排序的问题
回答 1
synchronized可见性问题
回答 1