title: Why is arraylist's size not right when multiple threads add elements into it?
body: <p>Why is Arraylist's size not right when multiple threads add elements into it?</p>
  
  <pre><code>threadCount = 100;
  List&lt;Object&gt; list = new ArrayList&lt;&gt;();
  for (int i = 0; i &lt; threadCount; i++) {
      Thread thread = new Thread(new MyThread(list, countDownLatch));
      thread.start();
  }
  
  class MyThread implements Runnable {
      // ......
  
      @Override
      public void run() {
          for (int i = 0; i &lt; 100; i++) {
              list.add(new Object());
          }
      }
  }
  </code></pre>
  
  <p>When this program is done, the size of the list should be 10000. Actually, the size may be 9950, 9965 or some other numbers. Why?</p>
  
  <p>I know that why this program may raise <code>IndexOutofBoundsException</code> and why there are some nulls in it, but I just do not understand why the size is wrong.</p>