aspose file tools*
The moose likes Java in General and the fly likes concurrentModificationException  tricky.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "concurrentModificationException  tricky.." Watch "concurrentModificationException  tricky.." New topic
Author

concurrentModificationException tricky..

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have two methods:






I get a concurrentModificationException..

any ideas or should I catch this error

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nimo:

The problem's in line 6 of your second method. You shouldn't try and remove items from a list of things that you're iterating over. It's better to create a smaller list of things to remove and remove that list from the original one after you're done iterating over it. You might also want to consider using the foreach loop, too.

John.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Other options would be to use a more thread-friendly collection, like those provided in the java.util.concurrent package which are guaranteed to never throw the ConcurrentModificationException. Off the top of my head, the ConcurrentLinkedQueue looks like a good fit. The CopyOnWriteArrayList might work as well depending on needs.


Steve
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks!!

The solution:





So what would be the main difference in using "CopyOnWriteArrayList" instead of the solution above?
A cat can be deleted from CatsList via method "doThat", would the deleted cat also be in a "CopyOnWriteArrayList" ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

With your code, if you delete a cat from the list while the ArrayList is copying from it then you can get an inconsistent state in the List (cats in the wrong order, perhaps, a null where there should not be an empty spot, a duplicate cat in two consecutive locations. To protect against that sort of thing you would need to synchronize the removal and the copy. CopyOnWriteArrayList prevents such a problem by swapping views internally in a thread-safe manner - preventing you from having to worry about it (and preventing blocking that might happen with synchronized lock contention).
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
With your code, if you delete a cat from the list while the ArrayList is copying from it then you can get an inconsistent state in the List


So you rightly say,
deleting a cat in "list A" by another method, even if the deep copy of "list A" to "list B" is not finished, can indeed causes lacks.

But in my case, the copy is already finished before invoking doThat-method:



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

nimo frey wrote:
With your code, if you delete a cat from the list while the ArrayList is copying from it then you can get an inconsistent state in the List


So you rightly say,
deleting a cat in "list A" by another method, even if the deep copy of "list A" to "list B" is not finished, can indeed causes lacks.

But in my case, the copy is already finished before invoking doThat-method:





My bad. You are right, I thought doThat was being done in another thread (don't know how I got that idea). The copy is the way to go if you are working in the same Thread.

Sorry about that.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
thank you for helping !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: concurrentModificationException tricky..