File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Concurrent Modification Exception Bug Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Concurrent Modification Exception Bug" Watch "Concurrent Modification Exception Bug" New topic
Author

Concurrent Modification Exception Bug

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
It looks like I'm not the only one having a problem with a bug that causes the ConcurrentModificationException, but I didn't want to high-jack Tom's thread.

I'm trying to track down a ConcurrentModificationError. I've got some code that uses separate threads to iterate over a single Java Collection. The tricky part is during the iteration an object that implements the Runnable interface is created and sent to the AWT Event Dispatching Thread. Someone told me that this code can't be the source of the exception becuase the actions encapsulated by the objects implementing runnable are executed seqentially on the AWT Event Dispatching Thread. Therefore, the Java Collection couldn't be getting modified at the same time.

Here is my question. The code that actually iterates over the collection appears outside of the AWT Event Dispatching Thread. This means that the Java Collection could be interated over by 2 separate threads at the same time, although in this case we aren't "modifying" the actual objects stored in the collection, or the order of the objects in the collection.

Can the act of iteration over a collection by multiple threads cause the ConcurrentModificationException, or do the threads have to actually change something about the collection or the objects it stores?

Thanks,

Landon

P.S. - Here is the applicable section of the Javadoc for ConcurrentModificationException:

Note what the Javadoc for the ConcurrentModificationException thread states:

"it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3414
    
  12
Originally posted by Landon Blake:
Can the act of iteration over a collection by multiple threads cause the ConcurrentModificationException, or do the threads have to actually change something about the collection or the objects it stores?


My immediate thought on this would be that the collection needs to be modified to cause a ConcurrentModificationException - just iterating over it in two different threads shouldn't be a problem.
However, to be sure, the best way is to write a little test program and see if you can get it to happen.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent Modification Exception Bug
 
Similar Threads
ConcurrentModificationException
Confusion in ConcurrentModificationException
Setting Fields value through JNI
Removing entries from a collection
ConcurrentModificationException with HashSet recursion