title: ConcurrentHashMap not giving correct output in multi threaded application
body: <p>I created one example to understand concurrent HashMap but it seems to be giving indeterministic outout after 7-8 runs.</p>
  
  <p>In my example, I create three threads(they are mimicking three services to fetch scores for Math,Science,English ) which update a concurent HashMap which has just 3 key value pairs (Keys are three names A,B,C and values at the end of the run should be cumulative score for three subjects)</p>
  
  <p>I am posting the code below, pls advise.</p>
  
  <p>Wrong Output is shown below (Correct one should end like be  All Done main : {C=27, A=57, B=42})</p>
  
  <pre><code>Within run Math : {C=0, A=0, B=0}
  Within run Science : {C=0, A=0, B=0}
  Completed Science : {C=10, A=39, B=29}
  Completed Math : {C=10, A=39, B=29}
  Within run English : {C=0, A=0, B=0}
  Completed English : {C=18, A=57, B=42}
  All Done main : {C=18, A=57, B=42}
  </code></pre>
  
  <p>ConcurrentHashMapExample class:</p>
  
  <pre><code>    package com.ll.thread.concurrency;
  
      import java.util.Iterator;
      import java.util.concurrent.ConcurrentHashMap;
      import java.util.concurrent.CountDownLatch;
  
      public class ConcurrentHashMapExample {
  
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              ConcurrentHashMap&lt;String, Integer&gt;   concurrentHashMap = new ConcurrentHashMap&lt;String, Integer&gt;();
              concurrentHashMap.put("A", 0);
              concurrentHashMap.put("B", 0);
              concurrentHashMap.put("C", 0);
              CountDownLatch  countDownLatch = new CountDownLatch(3);
  
              Runnable runnableScience = new Worker(concurrentHashMap , "Science" , countDownLatch);
              Runnable runnableMath = new Worker(concurrentHashMap , "Math" , countDownLatch);
              Runnable runnableEnglish = new Worker(concurrentHashMap , "English" , countDownLatch);
  
              new Thread(runnableScience , "Science").start();
              new Thread(runnableMath ,"Math").start();
              new Thread(runnableEnglish ,"English").start();
  
              try {
                  countDownLatch.await();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              System.out.println("All Done " + Thread.currentThread().getName() + " : "+concurrentHashMap);
  
  
  
  
              concurrentHashMap = null;
  
  
  
          }
  
      }
  </code></pre>
  
  <p>Worker class:</p>
  
  <pre><code>    package com.ll.thread.concurrency;
  
      import java.util.Iterator;
      import java.util.concurrent.ConcurrentHashMap;
      import java.util.concurrent.CountDownLatch;
  
      public class Worker  implements Runnable{
  
          ConcurrentHashMap&lt;String, Integer&gt;  concurrentHashMap ;
          String threadName = "";
          CountDownLatch  countDownLatch ;
  
          public Worker(ConcurrentHashMap&lt;String, Integer&gt; concurrentHashMap,
                  String subject, CountDownLatch countDownLatch) {
              this.concurrentHashMap = concurrentHashMap;
              this.threadName = subject ;
              this.countDownLatch = countDownLatch;
  
              // TODO Auto-generated constructor stub
          }
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              System.out.println("Within run " +Thread.currentThread().getName() + " : "+concurrentHashMap);
              try{
           for(Iterator&lt;String&gt;  iterator = concurrentHashMap.keySet().iterator() ;   iterator.hasNext();){
              String key = iterator.next();
  
              //synchronized (this) {
  
  
              if("Math".equals(Thread.currentThread().getName())) {
              if("A".equals(key))
                  concurrentHashMap.put(key, concurrentHashMap.get(key) +20);
              else if("B".equals(key))
                  concurrentHashMap.put(key, concurrentHashMap.get(key) + 15);
              else if("C".equals(key))
                  concurrentHashMap.put(key, concurrentHashMap.get(key) +10);
              }else
  
              if("Science".equals(Thread.currentThread().getName())) {
                  if("A".equals(key))
                      concurrentHashMap.put(key, concurrentHashMap.get(key) +19);
                  else if("B".equals(key))
                      concurrentHashMap.put(key, concurrentHashMap.get(key) +14);
                  else if("C".equals(key))
                      concurrentHashMap.put(key, concurrentHashMap.get(key) +9);
                  }
              else
                  if("English".equals(Thread.currentThread().getName())) {
                      if("A".equals(key))
                          concurrentHashMap.put(key, concurrentHashMap.get(key) +18);
                      else if("B".equals(key))
                          concurrentHashMap.put(key, concurrentHashMap.get(key) +13);
                      else if("C".equals(key))
                          concurrentHashMap.put(key, concurrentHashMap.get(key) +8);
                      }
  
              }
          // }
              }
              finally{
  
                  System.out.println("Completed " + Thread.currentThread().getName() + " : " + concurrentHashMap);
                  countDownLatch.countDown();
              }
           }
          }
  </code></pre>