• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent Modification Exception  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I wrote (myself! ) this very sophisticated piece of code:



Simple as it is, it nevertheless manages to produce exception. The output is:

THE MAP:
10
Exc msg: null, class: class java.util.ConcurrentModificationException

Now can anybody explain to me, how the hell does this happen? Can you spot this concurrent modification the JVM is complaining about??
[ July 18, 2008: Message edited by: Dariusz Kordonski ]
 
author
Sheriff
Posts: 23329
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now can anybody explain to me, how the hell does this happen? Can you spot this concurrent modification the JVM is complaining about??



When an iterator is working through a collection, you are not allowed to change the structural organization of the collection. It is not really a concurrent modification -- it is more of a modification while the iterator is still working on it.

Anyway, to answer your question, you configured you linked hash map to keep access order. This means that an access of any kind can change the structural organization of the map. Your get() method is basically changing the structure of the map, while you are still iterating, hence, concurrent modification exception.

Henry
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks a lot, now I get it. So to iterate over keys and values of such a map I would need to use entrySet() instead. That's quite tricky
 
Marshal
Posts: 57276
175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Using EntrySet isn't that tricky.

You get a Set from the entrySet method. Now your Map is parameterised as <String, String>, so your Set will contain Entries of that type. It will read something like this:I think that is how it works. You do not keep the insertion order like this, I am afraid.
[ July 18, 2008: Message edited by: Campbell Ritchie ]
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thank you for your insights! If anybody's interested in such details: I've just tested the entrySet() iteration and it seems that neither entry.getKey() nor entry.getValue() count as access in this case, so the insertion order remains actually intact.
 
Campbell Ritchie
Marshal
Posts: 57276
175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing to a different implementation of Map (one that doesn't modify the structure with a get) may also provide a solution.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dariusz Kordonski:
I've just tested the entrySet() iteration and it seems that neither entry.getKey() nor entry.getValue() count as access in this case



The put, get and putAll methods are the only ones which will affect the access time.
[ July 22, 2008: Message edited by: Joanne Neal ]
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!