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?
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."
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.