title: Java ConcurrentHashMap initialization
body: <p>I am currently reading through the source code of ConcurrentHashMap in jdk1.8, and I find that <code>initalTable()</code> method a bit confusing.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ConcurrentHashMap&lt;K,V&gt; extends AbstractMap&lt;K,V&gt;
      implements ConcurrentMap&lt;K,V&gt;, Serializable {
  //some fields revolved in this method
  transient volatile Node&lt;K,V&gt;[] table;
  private transient volatile int sizeCtl;
  
    private final Node&lt;K,V&gt;[] initTable() {
          Node&lt;K,V&gt;[] tab; int sc;
          while ((tab = table) == null || tab.length == 0) {
              if ((sc = sizeCtl) &lt; 0)
                  Thread.yield(); // lost initialization race; just spin
              else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                  try {
                      if ((tab = table) == null || tab.length == 0) {
                          int n = (sc &gt; 0) ? sc : DEFAULT_CAPACITY;
                          @SuppressWarnings("unchecked")
                          Node&lt;K,V&gt;[] nt = (Node&lt;K,V&gt;[])new Node&lt;?,?&gt;[n];//place 1
                          table = tab = nt;//place 2
                          sc = n - (n &gt;&gt;&gt; 2);
                      }
                  } finally {
                      sizeCtl = sc;
                  }
                  break;
              }
          }
          return tab;
      }
  }
  </code></pre>
  
  <p>So main confusion mainly focused on the following points:</p>
  
  <ol>
  <li>Why would we need <code>while((tab = table) == null || tab.length == 0)</code> to check both <code>table</code> is not null and its length is not 0 and determine whether to continue the loop, so it exits only when both two condition is false. I just cannot think of a situation when the <code>table</code> is not null but with a length of 0, since it is initialized in the <code>else if</code> block where n is always given a value larger than 0. Then why we need the both rather than only one?</li>
  </ol>
  
  <p><del>2. The second question is not a problem but rather I just want to check whether my assumption makes sense. So imagine two threads  run subsequently but in a very close manner. The later thread can enter the <code>else if</code> block if the earlier one hasn't reach the point of initialization of <code>table</code> or place 2 nor has <code>sizeCtl</code> been updated, then the second thread will be able to enter the <code>else if</code> block, so <code>table</code> field will be initialized once more? Will this scenario happens in rare situation? I know this will not affect the correctness of the program but will it happens in some situation?</del></p>
  
  <p>Hope anyone can give me a clarification. Thanks in advance.</p>
  
  <hr>
  
  <p><strong>Updated:</strong>
  I forgot the fact that one successful CAS operation will swap the <code>sizeCtl</code> to -1 which will block other threads from entering the <code>else if</code> block, but I am still not clear about why we need two conditions in the <code>if</code> and <code>while</code>.</p>