• 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

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic