Code looks like this: The maps being used here are Guava maps
private Map<SomeObject, SomeOtherObject> myMap = Maps.newLinkedHashMap();
public Map<SomeObject, SomeOtherObject> getMap() {
return Maps.newHashMap(myMap);
}
public void putMap(SomeObject a, SomeOtherObject b) {
myMap.put(a,b);
}
So, the above threw java.util.ConcurrentModificationException and am trying to recreate the scenario. But no matter what I try, the system seems resilient. Here is what I tried:
1. Created 'n' threads some of which call getMap() and some call putMap()
2. Created 'n' threads that only call putMap() and one thread the is in an infinite loop that calls getMap()
3. Created 2 threads each of which alternates calling getMap() and putMap(). Meaning, thread-1 first calls get then put and thread-2 first calls put and then get.
None of the above works, either keeps running or goes to OOM. Any pointers on how to go about doing this?
EDIT
I believe the ConcurrentModificationException is thrown when returning the copy of the map {Maps.newHashMap(myMap);}. During this process the iterator creates a copy and while the iterator is at work, if the contents of the map are modified its not happy.
Maps.newHashMapdo? Where is theConcurrentModificationExceptionthrown from? What is the stack trace of the exception?