title: Java multi threading atomic assignment
body: <p>Same with the follow link, I use the same code with the questioner.<br>
  <a href="https://stackoverflow.com/questions/5307003/java-multi-threading-atomic-reference-assignment">Java multi-threading atomic reference assignment</a>
  In my code, there </p>
  
  <p><code>HashMap&lt;String,String&gt; cache = new HashMap&lt;String,String&gt;();</code></p>
  
  <pre><code>public class myClass {
      private HashMap&lt;String,String&gt; cache = null;
      public void init() {
         refreshCache();
      }
      // this method can be called occasionally to update the cache.
      //Only one threading will get to this code.
  
      public void refreshCache() {
          HashMap&lt;String,String&gt; newcache = new HashMap&lt;String,String&gt;();
         // code to fill up the new cache
         // and then finally
         cache = newcache; //assign the old cache to the new one in Atomic way
      }
  
      //Many threads will run this code
      public void getCache(Object key) {
          ob = cache.get(key)
          //do something
      }
  }
  </code></pre>
  
  <p>I read the sjlee's answer again and again, I can't understand in which case these code will go wrong. Can anyone give me a example?<br>
  Remember I don't care about the getCache function will get the old data.<br>
  I'm sorry I can't add comment to the above question because I don't have 50 reputation.
  So I just add a new question.</p>