title: Concurrent Modification Exception
body: <p>I'm currently working on a multi-threaded application, and I occasionally receive a concurrently modification exception (approximately once or twice an hour on average, but occurring at seemingly random intervals).</p>
  
  <p>The faulty class is essentially a wrapper for a map -- which extends <code>LinkedHashMap</code> (with accessOrder set to true). The class has a few methods:</p>
  
  <pre><code>synchronized set(SomeKey key, SomeValue val)
  </code></pre>
  
  <p>The set method adds a key/value pair to the internal map, and is protected by the synchronized keyword.</p>
  
  <pre><code>synchronized get(SomeKey key)
  </code></pre>
  
  <p>The get method returns the value based on the input key.</p>
  
  <pre><code>rebuild()
  </code></pre>
  
  <p>The internal map is rebuilt once in a while (~every 2 minutes, intervals do not match up with the exceptions).  The rebuild method essentially rebuilds the values based on their keys.  Since rebuild() is fairly expensive, I did not put a synchronized keyword on the method.  Instead, I am doing:</p>
  
  <pre><code>public void rebuild(){
    /* initialization stuff */
    List&lt;SomeKey&gt; keysCopy = new ArrayList&lt;SomeKey&gt;();
    synchronized (this) {
      keysCopy.addAll(internalMap.keySet());
    }
    /* 
      do stuff with keysCopy, update a temporary map
     */    
    synchronized (this) {
      internalMap.putAll(tempMap);
    }
  }
  </code></pre>
  
  <p>The exception occurs at </p>
  
  <pre><code>keysCopy.addAll(internalMap.keySet());
  </code></pre>
  
  <p>Inside the synchronized block.</p>
  
  <p>Suggestions are greatly appreciated.  Feel free to point me to specific pages/chapters in <em>Effective Java</em> and/or <em>Concurrency in Practice</em>.</p>
  
  <p>Update 1:</p>
  
  <p>Sanitized stacktrace:</p>
  
  <pre><code>java.util.ConcurrentModificationException
          at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:365)
          at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:376)
          at java.util.AbstractCollection.toArray(AbstractCollection.java:126)
          at java.util.ArrayList.addAll(ArrayList.java:473)
          at a.b.c.etc.SomeWrapper.rebuild(SomeWraper.java:109)
          at a.b.c.etc.SomeCaller.updateCache(SomeCaller.java:421)
          ...
  </code></pre>
  
  <p>Update 2:</p>
  
  <p>Thanks everyone for the answers so far.  I think the problem lies within the LinkedHashMap and its accessOrder attribute, although I am not entirely certain atm (investigating).</p>
  
  <p><strong>If accessOrder on a LinkedHashMap is set to true, and I access its keySet then proceed to add the keySet to a linkedList via addAll, do either of these actions mutate the order (i.e. count towards an "access")?</strong></p>