aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ConcurrentModificationExc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "ConcurrentModificationExc" Watch "ConcurrentModificationExc" New topic
Author

ConcurrentModificationExc

Bodenstab Oliver
Ranch Hand

Joined: Mar 03, 2005
Posts: 47
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


SCJP
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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.
Lars Vonk
Ranch Hand

Joined: Aug 05, 2005
Posts: 30
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
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11424
    
  85

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Bodenstab Oliver
Ranch Hand

Joined: Mar 03, 2005
Posts: 47
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ConcurrentModificationExc