• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ConcurrentModificationExc

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

i need your help!

I have a static cache database from type vector. One thread iterate over the cache database while an other thread remove one entry. The iterating thread get a ConcurrentModificationException.

How can i solve this problem

Please Help!

Oliver
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although Vector is a synchronized list, it's not thread safe for iteration through its elements. To avoid the ConcurrentModification exception, the iteration should be done in the synchronized block.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Oliver,

One way to solve it is this:

synchronized(myVector) {
//Iterate myVector here
}

This way your Vector cannot be modified while iterating over it.

Cheers,
Lars
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Oliver,

Another way is to do something like:(Note: I am guessing at what you put into your Vector - you may have to change the definition of records to suit your implementation.) This has the advantage that you are synchronizing on myVector for the shortest possible time, then doing the work on a local snapshot of myVector.

You could also use the clone() method to create a local copy of myVector - from memory the clone method does a deep copy (Vector overrides clone for just this purpose) - then you could iterate over the local copy in the same way that I used the array (but the array would probably be faster - it then becomes more of a code readability issue).

Origanally posted by John Smith
Although Vector is a synchronized list, it's not thread safe for iteration through its elements.

The synchronization on Vector is usually not much help to the programmer, and we generally recommend against using it. Perhaps you could mention why you need a synchronized collection, and we can comment on whether Vector suits your purpose?

Regards, Andrew
 
Bodenstab Oliver
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for advice!

I take the following solution.


The clone method only does a flat copy not a deep!
But i think, flat is good for this problem and with this solution the time of synchronizing is very minimal (clone-method is synchronized).

If someone have remarks to this solution, please let me know

Oliver
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic