Questions tagged «double-checked-locking»

7
为什么在双重检查锁定中使用了volatile
在Head First设计模式手册中,具有双重检查锁定的单例模式已实现如下: public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } 我不明白为什么volatile要使用它。volatile使用不会 违反使用双重检查锁定(即性能)的目的吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.