title: Why is volatile used in double checked locking
body: <p>From <em>Head First</em> design patterns book, the singleton pattern with double checked locking has been implemented as below: </p>
  
  <pre><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></pre>
  
  <p>I don't understand why <code>volatile</code> is being used. Doesn't  <code>volatile</code> usage defeat the purpose of using double checked locking i.e performance?</p>