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()){

...

}

}

[945 byte] By [MartinHilperta] at [2007-11-26 15:36:17]
# 1

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.

jesperdja at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 2
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.
MartinHilperta at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 3
Unless you post the actual code that is accessing the Map, there is no point in asking the question.
macrules2a at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 4
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'.
sabre150a at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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

kajbja at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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.
karma-9a at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 7
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.
ekupcika at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 8

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

MartinHilperta at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 9
> 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 ...
quittea at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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".
CeciNEstPasUnProgrammeura at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 11
> 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~
yawmarka at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 12
I bet you one hundred space dollars you're modifying the map in some way while you iterate over it
georgemca at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 13
> 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 .
sabre150a at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 14
> > 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 ;)
quittea at 2007-7-8 21:53:56 > top of Java-index,Java Essentials,Java Programming...
# 15
> > 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 :-)
georgemca at 2007-7-21 16:28:44 > top of Java-index,Java Essentials,Java Programming...
# 16

> > > 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 > top of Java-index,Java Essentials,Java Programming...
# 17

> > > > 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!

georgemca at 2007-7-21 16:28:44 > top of Java-index,Java Essentials,Java Programming...
# 18

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

karma-9a at 2007-7-21 16:28:44 > top of Java-index,Java Essentials,Java Programming...
# 19
> Not a Best Practice.You forgot the <air-quotes>Best Practice</air-quotes> tag...:o)~
yawmarka at 2007-7-21 16:28:44 > top of Java-index,Java Essentials,Java Programming...