title: Ordering in simultaneous Read and Write on a collection in Threads in Java
body: <p>I am trying out a code to see what happens if a thread modifies a collection while other threads read the collection. Here's the code</p>
  
  <pre><code>package threading;
  
  import java.util.ArrayList;
  
  public class ReadAndWrite {
  
      static ArrayList&lt;Integer&gt; coll = new ArrayList&lt;Integer&gt;();
  
      public static void main(String[] args) {
          coll.add(1);
          coll.add(3);
          coll.add(5);
  
      Thread t = new Thread() {
          public void run(){
              coll.add(2);
              coll.add(6);
              coll.add(8);
          }
      };
      Thread t1 = new Thread() {
              public void run(){
                  System.out.println(" collection is "+coll + " and size is "+coll.size());
              }
      };
      Thread t2 = new Thread() {
          public void run(){
              System.out.println(" collection is "+coll+ " and size is "+coll.size());
          }
  };
  Thread t3 = new Thread() {
      public void run(){
          System.out.println(" collection is "+coll+ " and size is "+coll.size());
      }
  };
  Thread t4 = new Thread() {
      public void run(){
          System.out.println(" collection is "+coll+ " and size is "+coll.size());
      }
  };
  Thread t5 = new Thread() {
      public void run(){
          System.out.println(" collection is "+coll+ " and size is "+coll.size());
      }
  };
  t.start();
  
  t1.start();
  
  t2.start();
  t3.start();
  
  t4.start();
  t5.start();
  
          }
  
          }
  </code></pre>
  
  <p>When I start write thread first this is the response I get all the times. </p>
  
  <pre><code>collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
  </code></pre>
  
  <p>As soon as I change the order like one below, things go haywire in response</p>
  
  <pre><code> t1.start();
  t.start();
  t2.start();
  t3.start();
  
  t4.start();
  t5.start();
  </code></pre>
  
  <p>Response</p>
  
  <pre><code> collection is [1, 3, 5] and size is 3
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5] and size is 3
   collection is [1, 3, 5] and size is 3
  </code></pre>
  
  <p>If we further experiment with ordering we'd see an error like below</p>
  
  <pre><code>Exception in thread "Thread-1"  collection is [1, 3, 5, 2, 6, 8] and size is 6Exception in thread "Thread-2" 
   collection is [1, 3, 5, 2, 6, 8] and size is 6
   collection is [1, 3, 5, 2, 6, 8] and size is 6
  java.util.ConcurrentModificationException
      at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
      at java.util.ArrayList$Itr.next(ArrayList.java:859)
      at java.util.AbstractCollection.toString(AbstractCollection.java:461)
      at java.lang.String.valueOf(String.java:2994)
      at java.lang.StringBuilder.append(StringBuilder.java:131)
      at threading.ReadAndWrite$3.run(ReadAndWrite.java:28)
  java.util.ConcurrentModificationException
      at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
      at java.util.ArrayList$Itr.next(ArrayList.java:859)
      at java.util.AbstractCollection.toString(AbstractCollection.java:461)
      at java.lang.String.valueOf(String.java:2994)
      at java.lang.StringBuilder.append(StringBuilder.java:131)
      at threading.ReadAndWrite$2.run(ReadAndWrite.java:23)
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li>Synchronization or locking seems to be an obvious solution here but why does starting the write thread first give us uniform results, threads are supposed be running out of order isn't it?</li>
  <li>I thought simultaneous writing is an issue but even when we have only one write thread we are running into errors, why?</li>
  </ol>
  
  <p>Will deeply appreciate guidance on above scenario, thanks in advance. Sid</p>