wood burning stoves*
The moose likes Java in General and the fly likes Concurrent Modification Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Concurrent Modification Exception" Watch "Concurrent Modification Exception" New topic
Author

Concurrent Modification Exception

Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
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 ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
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

Joined: Jul 11, 2008
Posts: 49
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
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Well done
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Changing to a different implementation of Map (one that doesn't modify the structure with a get) may also provide a solution.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
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 ]

Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent Modification Exception
 
Similar Threads
How to refresh a StringBuffer object for creating a HashMap object
Is HashMap.KeySet() thread safe ?
HashMap Problem
HashMap values-- I think I know why but how do I fix this null pointer exception?
how to search equalignorecase search with hashmap