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

Concurrent Modification Exception Bug

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic