Jul 20, 2013

Concurrent Modification Exception in Collections in Java

When more than one iterator accessing the same collection reference , then this concurrent modification exception will occur.
Ex:-
      List list = new LinkedList();
       Iterator it1 = list.iterator();
       Iterator it2 = list.iterator();
          it1.next();
           it1.remove();
          it2.next();

The above code will surely end up in concurrent modification exception.
   
 Say now, if this is the case , how to tackle this exception.
  You can have any number of iterators , make all your iterators readable , make only one read and write.
Tags:-
   

No comments:

Post a Comment

Post a Comment

Note: Only a member of this blog may post a comment.