title: Avoiding ConcurrentModificationException on List by making a shallow copy
body: <p>I have a class like the following:</p>
  
  <pre><code>class Test
  {
      private LinkedList&lt;Person&gt; persons = new LinkedList&lt;Person&gt;;
  
      public synchronized void remove(Person person)
      {
          persons.remove(person);
      }
  
      public List&lt;Person&gt; getAllPersons()
      {
          // Clients may iterate over the copy returned and modify the structure.
          return new ArrayList&lt;Person&gt;(persons);
      }
  }
  </code></pre>
  
  <p><code>persons</code> may be modified concurrently: one is via <code>remove()</code> by one thread and two via the shallow copied instance returned by <code>getAllPersons()</code>.</p>
  
  <p>I have tested the above scenario in a multithreaded environment to see if I can avoid <code>ConcurrentModificationException</code> by returning a shallow copy when <code>getAllPersons()</code> is called. It seemed to work.  I have never once encountered a <code>ConcurrentModificationException</code>.</p>
  
  <p>Why, in this case, does making only a shallow copy of <code>persons</code> avoid a <code>ConcurrentModificationException</code>?</p>