title: Why doesn't double checked locking fail like this in case of Singleton classes?
body: <pre><code>1.   class Foo {
  2.       private Helper helper = null;
  3.       public Helper getHelper() {
  4.           if (helper == null) {
  5.              synchronized(this) {
  6.                 if (helper == null) {
  7.                    helper = new Helper();
  8.                 }
  9.              }
  10.          }
  11.          return helper;
  12.      }
  13.  }
  </code></pre>
  
  <p>The reason why this structure is considered broken is generally described the reordering of assignments done by the compiler such that the Helper constructor is called after the write to the helper variable. My question is that how is this code thread-safe and are the following steps possible?</p>
  
  <ul>
  <li>Thread 1, enters the synchronized block and finds out that helper is null. </li>
  <li>Thread 1, gives up the monitor at this point</li>
  <li>Thread 2, enters the object monitor and instantiates the helper</li>
  <li>Thread 1, comes back and re-initializes the helper instance as </li>
  </ul>
  
  <p>I don't see how this solution is any better than single checked locking. </p>