title: Java HashMap sometimes returning wrong value in some threads
body: <p><strong>Update:</strong> It was a static buried deep in some code where it was used for just a couple of instructions. Thank you all for the suggestions.</p>
  
  <p>We are not using one HashMap across threads (yes that is bad for many reasons). Each thread has its own HashMap.</p>
  
  <p>We have a class that extends from Thread. In Thread.run() we create a HashMap, set a key/value pair in it, and pass that HashMap to a method. That method retrieves the value from the HashMap, inserts it into a string, and returns the string.</p>
  
  <p>Sometimes the returned string has a different value (still in Thread.run()). This only occurs on hardware with 3+ physical cores. And it has only happened twice (before we added logging to help us find exactly what is going on of course).</p>
  
  <p>Any idea why this would occur.</p>
  
  <p>Update: here's the full code. The ProcessTxt is what pulls the value from the HashMap and puts it in the string.</p>
  
  <pre><code>import java.io.*;
  import java.util.HashMap;
  
  import junit.framework.TestCase;
  import net.windward.datasource.dom4j.Dom4jDataSource;
  import net.windward.xmlreport.ProcessReport;
  import net.windward.xmlreport.ProcessTxt;
  
  /**
   * Test calling from multiple threads
   */
  public class TestThreads extends TestCase {
  
      private static String path = ".";
  
      // JUnit stuff
      public TestThreads(String name) {
          super(name);
      }
  
      // Get logging going - called before any tests run
      protected void setUp() throws Exception {
          ProcessReport.init();
      }
  
      // this is not necessary - called after any tests are run
      protected void tearDown() {
      }
  
      private static final int NUM_THREADS = 100;
  
      private boolean hadWithVarError = false;
  
  
      /**
       * Test that each thread has unique variables.
       */
      public void testRunReportsWithVariables() throws Exception {
  
          // run 10 threads
          ReportThreadWithVariables[] th = new ReportThreadWithVariables[NUM_THREADS];
          for (int ind = 0; ind &lt; NUM_THREADS; ind++) {
              th[ind] = new ReportThreadWithVariables(this, ind);
              th[ind].setName("Run " + ind);
          }
          for (int ind = 0; ind &lt; NUM_THREADS; ind++)
              th[ind].start();
  
          boolean allDone = false;
          while (!allDone) {
              Thread.sleep(100);
              allDone = true;
              for (int ind = 0; ind &lt; NUM_THREADS; ind++)
                  if (th[ind].isAlive())
                      allDone = false;
          }
  
          assertTrue(!hadWithVarError);
      }
  
      public static class ReportThreadWithVariables extends Thread {
  
          private TestThreads obj;
          private int num;
  
          public ReportThreadWithVariables(TestThreads tt, int num) {
              obj = tt;
              this.num = num;
          }
  
          public void run() {
  
              try{
                  System.out.println("starting " + num);
                  ByteArrayOutputStream out = new ByteArrayOutputStream();
                  ProcessTxt pt = new ProcessTxt(new FileInputStream(new File(path, "Thread_Test.docx")), out);
  
                  pt.processSetup();
  
                  // don't use order1.xml, but need a datasource.
                  Dom4jDataSource datasource = new Dom4jDataSource(new FileInputStream(new File(path, "order1.xml")));
                  HashMap map = new HashMap();
                  map.put("num", new Integer(num));
                  datasource.setMap(map);
                  pt.processData(datasource, "");
  
                  pt.processComplete();
                  String result = out.toString().trim();
                  System.out.println("complete " + num + ", result = " + result);
  
                  String expected = "Number: " + num;
                  if (!result.equals( expected ))
                      obj.hadWithVarError = true;
                  assertEquals(expected, result);
              } catch (Throwable e) {
                  obj.hadWithVarError = true;
                  e.printStackTrace();
              }
  
          }
      }
  }
  </code></pre>
  
  <p>(edit to format code)</p>