ConcurrentModificationException with just 1 thread?
I get this ConcurrentModificationException that I didn't expect as the working thread is not sharing anything with other threads. Is it 100% sure that this exception can only occur if at least 2 threads access this resource?
The exception occurs in a simple method that just iterates a Map:
java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
at java.util.TreeMap$ValueIterator.next (Unknown Source)
at <my method> ...
privatevoid myMethod(final Map<String, Transaction> entries){
for (final Transaction t : entries.values()){
...
}
}
Are you modifying the objects that are in the Map while you iterate it?
I'm not sure on the details, but collections like maps and sets depend on the hashcode etc. of objects. If you modify an object that's in a map or set, its hashcode may change and the map or set needs to reorganize the data.
Nope, I don't modify it while iterating because I just create 1 thread that instantiates and uses this class that has no static fields other than constants.
Unless you post the actual code that is accessing the Map, there is no point in asking the question.
ConcurrentModificationException does not need to be thread related. One can get this if, in one thread, one modifies a collection while iterating over it. I would suggest that directly or indirectly you are modifying the content of the 'entries' map within your 'for loop'.
> ConcurrentModificationException does not need to be
> thread related. One can get this if, in one thread,
> one modifies a collection while iterating over it. I
> would suggest that directly or indirectly you are
> modifying the content of the 'entries' map within
> your 'for loop'.
... by e.g. calling remove or add
> Is it 100% sure that> this exception can only occur if at least 2 threads> access this resource?No. , you can get the exception with a single thread, when that thread modifies a collection while it is iterating over it at the same time.
Or in other words: an iterator becomes "invalid" if the collection/map is modfied after the iterator has been created no matter which thread modified it.
Thanks, that was indeed the case! Within the loop I indirectly changed the map. I didn't see it as I just called nother method within the loop to add a new object to antoher map. But this map had the same entries (references) as the map I iterated over.
I think the name of the exception "ConcurrentModificationException" is quite missleading. Better would be something like "IterationModificationException" ... ?
> I think the name of the exception> "ConcurrentModificationException" is quite> missleading. Better would be something like> "IterationModificationException" ... ?Probably. Though HeadlessException is still my favorite exception name ...
> I think the name of the exception> "ConcurrentModificationException" is quite> missleading. Better would be something like> "IterationModificationException" ... ?Well, it is concurrent modification. It's modified while the iterator is "alive".
> I think the name of the exception> "ConcurrentModificationException" is quite> missleading. It's explained quite clearly in the API. http://java.sun.com/j2se/1.4.2/docs/api/java/util/ConcurrentModificationException.html~
I bet you one hundred space dollars you're modifying the map in some way while you iterate over it
> I bet you one hundred space dollars you're modifying> the map in some way while you iterate over itErrr... #1, #4, #6 and #7 .
> > I bet you one hundred space dollars you're modifying> > the map in some way while you iterate over it> > Errr... #1, #4, #6 and #7 .This way it's safer to bet ;)
> > I bet you one hundred space dollars you're> modifying> > the map in some way while you iterate over it> > Errr... #1, #4, #6 and #7 .I know. I thought he was still in denial about it, but he's not :-)
> > > I bet you one hundred space dollars you're
> modifying
> > > the map in some way while you iterate over it
> >
> > Errr... #1, #4, #6 and #7 .
>
> This way it's safer to bet ;)
Especially since the OP admits that he does so (reply #8 )
kajbja at 2007-7-21 16:28:44 >

> > > > I bet you one hundred space dollars you're
> > modifying
> > > > the map in some way while you iterate over it
> > >
> > > Errr... #1, #4, #6 and #7 .
> >
> > This way it's safer to bet ;)
>
> Especially since the OP admits that he does so (reply
> #8 )
alright alright, don't labour the point!
> > I think the name of the exception
> > "ConcurrentModificationException" is quite
> > missleading. Better would be something like
> > "IterationModificationException" ... ?
>
> Probably. Though HeadlessException is still my
> favorite exception name ...
I prefer WTF_Exception, with the first part of the name abbreviated here for the sake of the Forum rules of conduct.
Though I admit that the name lacks precision as to the exact type of Exception. Not a Best Practice.
> Not a Best Practice.You forgot the <air-quotes>Best Practice</air-quotes> tag...:o)~