title: some elements processed more than once, some not at all
body: <p>I have a fairly straightforward task: I have a list of strings each of which is processed and a score is produced. The string and its score then get added to a map:</p>
  
  <pre><code>public class My1Thread 
  {
      final private static List&lt;String&gt; ids = Arrays.asList("id1","id2","id3","id4","id5");
  
      private static HashMap&lt;String,Double&gt; result = null;
  
      private Double computeResult(String id)
      {
          Double res = 0.0;
          // do stuff to compute result
          return res;
      }
  
      public static void main(String[] args) 
      {
          result = new HashMap&lt;String,Double&gt;();
  
          for (String id: ids)
          {
              result.put(id,computeResult(id));
          }
  
      }
  
  }
  </code></pre>
  
  <p>Since scores of any two strings are independent of each other, this seems to be a perfect case to use multithreading. However, I am getting unexpected results, which is probably a typical result for a multithreading newbie. </p>
  
  <p>Here's a m/t version of the above:</p>
  
  <pre><code>public class MyMultiThread 
  {
      final private static int nWorkers = 3;  // number of threads
  
      final private static List&lt;String&gt; ids = Arrays.asList("id1","id2","id3","id4","id5");
  
      private static int curIndex = 0;    // indexing pointing to position in ids currently being processed
  
      private static HashMap&lt;String,Double&gt; result = null;
  
      public static class Worker implements Runnable {
  
          private int id;
  
          public Worker(int id) {
              this.id = id;
          }
  
          synchronized void setCounter(final int counter)
          {
              curIndex = counter;
          }
  
          synchronized int getCounter()
          {
              return curIndex;
          }
  
          synchronized void addToResult(final String id, final Double score)
          {
              result.put(id,score);
          }
  
          @Override
          public void run() 
          {
              try {
                  while (true)
                  {
  
                      int index = getCounter();
                      if (index &gt;= ids.size())
                      {
                          // exit thread 
                          return;
                      }
  
                      String id = ids.get(index);
                      setCounter(index+1);
                      System.out.print(String.format("Thread %d: processing %s from pos %d\n", id, id, curIndex-1));
  
                      Double score = ...  // compute score here
  
                      addToResult(id,score);
                  }
  
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
  
      }
  
      public static void main(String[] args) 
      {
  
          result = new HashMap&lt;String,ArrayList&lt;Pair&lt;Document,Double&gt;&gt;&gt;();
  
          for (int i = 0; i &lt; nWorkers; i++) {
              Thread worker = new Thread(new MyMultiThread.Worker(i));
              worker.start();
          }
      }
  
  }
  </code></pre>
  
  <p>According to the output produced by <code>System.out.print</code>, this code appears to be processing some elements of <code>ids</code> more than once while not processing others at all. What am I doing wrong here?</p>