• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion in ConcurrentModificationException

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have created some of the examples of the JCF (Java Collections Framework).One thing that had confused me is that

When we are iterating a Colection say ArrayList using the iterator and during the iteration if we try to modify the collection by by calling the ArrayList's add method then it will give me the exception
ConcurrentModificationException.

But i did the iterations and try to add the new element by using the listiterator's add method then it is not giving me such exception.

My Question is why is such a different behaviour is there? Why java allowed to modify the collection using the iterator's own methods when iterating and why not through the arraylist method ?

After all both ways we are going to add the element to the ArrayList .

Please any help would be appreciated.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why you might get a ConcurrentModificationException when you modify a collection while you're iterating over it, is because many iterators can't work reliably anymore when you change the underlying collection. Some iterators provide operations like add() and remove() (on the iterator, not on the collection) that allow safe modification of the collection without the iterator getting confused. If you use those, it will work.

Note that it depends on the implementation of the iterator and the collection class whether you get a ConcurrentModificationException or not - it's not guaranteed that you get that exception. The API documentation of ConcurrentModificationException explains it:

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This exception may be thrown if you try to modify your collection while you iterate over it. There are Collections like the CopyOnWriteArrayList that will not throw this exception. If the data structure allows you to change the collection contents during iteration then this exception will not be thrown.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ConcurrentModificationException can occur in single-threaded code as well. This can happens when objects removed from the collection directly rather than through Iterator.remove.
reply
    Bookmark Topic Watch Topic
  • New Topic