• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concurrentModificationException tricky..

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two methods:






I get a concurrentModificationException..

any ideas or should I catch this error

 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for helping !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic