• 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

Why does Iterator sometime throw ConcurrentModificationException

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

I have the following code where I need to remove certain objects from a collection while iterating through it...



The problem is that sometimes this code runs fine and sometimes it throws....

java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:1036)
at java.util.HashMap$KeyIterator.next(HashMap.java:1073)
at com.hfa.sra.core.service.persist.SongPopulation.updateSongSplits(SongPopulation.java:1483)

Any suggestions? Am I doing something wrong? The exception happens at this line..



What I don't understand is how come it works sometimes and not others?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from HashMap API:


The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.



so, the culprit is : pNumbers.put(splitAcct, songReqPubSplit);[last statement of while loop]. to avoid this problem use java.util.concurrent.ConcurrentHashMap which is efficiently synchronized[since java1.5]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has nothing to do with synchronization. This exception is thrown even in a single thread application.

Almost none of the collection / map classes allow you to modify the collection / map while you are iterating over that very same collection / thread, except through the Iterator. Since the only Iterator method that can change the backing collection is remove(), adding is not possible. (ListIterator does have methods to add or replace elements.)

In your case, you iterate over the songReqPubSplits, then call songReqPubSplits.remove(...) inside the loop. Change this one line into itrSplit.remove() and your exception should be gone.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:This has nothing to do with synchronization. This exception is thrown even in a single thread application.

Almost none of the collection / map classes allow you to modify the collection / map while you are iterating over that very same collection / thread, except through the Iterator. Since the only Iterator method that can change the backing collection is remove(), adding is not possible. (ListIterator does have methods to add or replace elements.)

In your case, you iterate over the songReqPubSplits, then call songReqPubSplits.remove(...) inside the loop. Change this one line into itrSplit.remove() and your exception should be gone.



Thanks for your help Rob.

I have 2 questions though. 1 is that how come this sometimes works fine, and 2 is that I need modify the collection itself. Because later on in the code I am doing something with that collection. Can I re-initialize the collection from the Iterator if I do the remove from the latter?

Thanks again.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jehan Jaleel wrote:1 is that how come this sometimes works fine


This problem only occurs when using the Iterator and modifying the collection together. If you stop iterating immediately after you've modified the collection, that's just fine.

2 is that I need modify the collection itself. Because later on in the code I am doing something with that collection.


That's just fine because you're not iterating over it anymore.

Can I re-initialize the collection from the Iterator if I do the remove from the latter?


If you remove using the Iterator, the element is actually removed from collection immediately. You don't need to "re-initialize" or "synchronize".
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

Jehan Jaleel wrote:1 is that how come this sometimes works fine


This problem only occurs when using the Iterator and modifying the collection together. If you stop iterating immediately after you've modified the collection, that's just fine.



Ok that explains it. In some of my test cases I may have been exiting the loop soon after modifying the collection and that was why it must have been worked.

Thanks again for your help. JavaRanch rocks!!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic