title: Why does ExecutorService deadlock when performing HashMap operations?
body: <p>When running the following class the ExecutionService will often deadlock.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  
  public class ExecutorTest {
  public static void main(final String[] args) throws InterruptedException {
      final ExecutorService executor = Executors.newFixedThreadPool(10);
  
      final HashMap&lt;Object, Object&gt; map = new HashMap&lt;Object, Object&gt;();
      final Collection&lt;Callable&lt;Object&gt;&gt; actions = new ArrayList&lt;Callable&lt;Object&gt;&gt;();
      int i = 0;
      while (i++ &lt; 1000) {
          final Object o = new Object();
          actions.add(new Callable&lt;Object&gt;() {
              public Object call() throws Exception {
                  map.put(o, o);
                  return null;
              }
          });
          actions.add(new Callable&lt;Object&gt;() {
              public Object call() throws Exception {
                  map.put(new Object(), o);
                  return null;
              }
          });
          actions.add(new Callable&lt;Object&gt;() {
              public Object call() throws Exception {
                  for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
                      iterator.next();
                  }
                  return null;
              }
          });
      }
      executor.invokeAll(actions);
      System.exit(0);
  }
  
  }
  </code></pre>
  
  <p>So why does this happen? Or better yet - how can I write a test to ensure that implementations of an custom abstract map are thread safe? (Some implementations have multiple maps, another delegates to a cache implementation etc)</p>
  
  <p>Some background:
  this occurs under Java 1.6.0_04 and 1.6.0_07 on Windows.  I know that the problem comes from sun.misc.Unsafe.park():</p>
  
  <ul>
  <li>I can reproduce the problem on my Core2 Duo 2.4Ghz laptop but not while running in debug</li>
  <li>I can debug on my Core2 Quad at work, but I've hung it over RDP, so won't be able to get 
  a stack trace until tomorrow</li>
  </ul>
  
  <p>Most answers below are about the non-thread safety of HashMap, but I could find no locked threads in HashMap - it was all in the ExecutionService code (and Unsafe.park()). I shall closely examine the threads tomorrow.</p>
  
  <p>All this because a custom abstract Map implementation was not thread-safe so I set about ensuring that all implementations would be thread-safe.  In essence, I'm wanting to ensure that my understanding of ConcurrentHashMap etc are exactly what I expect, but have found the ExecutionService to be strangely lacking...</p>