Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

concurrentModificationException tricky..

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two methods:






I get a concurrentModificationException..

any ideas or should I catch this error

 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for helping !
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic