title: How do I create a thread-safe write-once read-many value in Java?
body: <p>This is a problem I encounter frequently in working with more complex systems and which I have never figured out a good way to solve.  It usually involves variations on the theme of a shared object whose construction and initialization are necessarily two distinct steps.  This is generally because of architectural requirements, similar to applets, so answers that suggest I consolidate construction and initialization are not useful. The systems have to target Java 4 at the latest, so answers that suggest support available only in later JVMs are not useful either.</p>
  
  <p>By way of example, let's say I have a class that is structured to fit into an application framework like so:</p>
  
  <pre><code>public class MyClass
  {
  
  private /*ideally-final*/ SomeObject someObject;
  
  MyClass() {
      someObject=null;
      }
  
  public void startup() {
      someObject=new SomeObject(...arguments from environment which are not available until startup is called...);
      }
  
  public void shutdown() {
      someObject=null; // this is not necessary, I am just expressing the intended scope of someObject explicitly
      }
  }
  </code></pre>
  
  <p>I can't make someObject final since it can't be set until startup() is invoked.  But I would really like it to reflect its write-once semantics and be able to directly access it from multiple threads, preferably avoiding synchronization.</p>
  
  <p>The idea being to express and enforce a degree of finalness, I conjecture that I could create a generic container, like so (<strong>UPDATE - corrected threading sematics of this class</strong>):</p>
  
  <pre><code>public class WormRef&lt;T&gt;
  {
  private volatile T                      reference;                              // wrapped reference
  
  public WormRef() {
      reference=null;
      }
  
  public WormRef&lt;T&gt; init(T val) {
      if(reference!=null) { throw new IllegalStateException("The WormRef container is already initialized"); }
      reference=val;
      return this;
      }
  
  public T get() {
      if(reference==null) { throw new IllegalStateException("The WormRef container is not initialized"); }
      return reference;
      }
  
  }
  </code></pre>
  
  <p>and then in <code>MyClass</code>, above, do:</p>
  
  <pre><code>private final WormRef&lt;SomeObject&gt; someObject;
  
  MyClass() {
      someObject=new WormRef&lt;SomeObject&gt;();
      }
  
  public void startup() {
      someObject.init(new SomeObject(...));
      }
  
  public void sometimeLater() {
      someObject.get().doSomething();
      }
  </code></pre>
  
  <p>Which raises some questions for me:</p>
  
  <ol>
  <li>Is there a better way, or existing Java object (would have to be available in Java 4)?</li>
  </ol>
  
  <p>Secondarily, in terms of thread safety:</p>
  
  <ol>
  <li>Is this thread-safe <strong><em>provided</em></strong> that no other thread accesses <code>someObject.get()</code> until after its <code>set()</code> has been called.  The other threads will only invoke methods on MyClass between startup() and shutdown() - the framework guarantees this.</li>
  <li>Given the completely unsynchronized <code>WormReference</code> container, it is ever possible under either JMM to see a value of <code>object</code> which is neither null nor a reference to a SomeObject?  In other words, does the JMM always guarantee that no thread can observe the memory of an object to be whatever values happened to be on the heap when the object was allocated.  I believe the answer is "Yes" because allocation explicitly zeroes the allocated memory - but can CPU caching result in something else being observed at a given memory location?</li>
  <li>Is it sufficient to make WormRef.reference volatile to ensure proper multithreaded semantics?</li>
  </ol>
  
  <p>Note the primary thrust of this question is how to <strong>express and enforce the finalness</strong> of <code>someObject</code> without being able to actually mark it <code>final</code>; secondary is what is necessary for thread-safety.  That is, don't get too hung up on the thread-safety aspect of this.</p>