title: Is it safe to add items to a linked list while iterating
body: <p>Is it safe to add items to a <code>LinkedList</code> while iterating?</p>
  
  <pre><code>class Worker {
  
      final LinkedList&lt;Foo&gt; worklist = new LinkedList&lt;&gt;();
  
      public void work() {
  
          Iterator&lt;Foo&gt; iterator = worklist.iterator();
  
          while (iterator.hasNext()) {
  
              Foo foo = iterator.next();
  
              doSomethingWith(foo);
          }
      }
  
      public void doSomethingWith(Foo foo) {
  
          // do something with foo            
  
          // and possibly add one (or more) foo's to the worklist
          if (expression) {
              worklist.add(new Foo());
          }
      }
  }
  </code></pre>
  
  <p>If not, how can this behaviour be implemented in a safe and efficient way?</p>
  
  <p>Note that this is not <a href="https://stackoverflow.com/questions/26888845/is-it-safe-to-add-items-to-the-list-while-iterating-through-it">about a <code>List</code></a>, but specifically about a <code>LinkedList</code>. If it isn't safe, I'm asking about alternatives.</p>