Java: Why is volatile used in this example of double checked locking

Richards

New Member
I'm reading Head First design patterns and in the chapter on the singleton pattern it shows you how to implement double checked locking as follows:\[code\]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; }}\[/code\]I don't understand why volatile is being used. Doesn't using volatile defeat the purpose of using double checked locking i.e performance?
 
Top