コードは次のようになります。ここで使用されているマップは Guava マップです。
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);
}
したがって、上記はjava.util.ConcurrentModificationException
スローされ、シナリオを再現しようとしています。しかし、私が何をしようとしても、システムは回復力があるようです. これが私が試したものです:
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.
上記のいずれも機能せず、実行を続けるか、OOM に移行します。これを行う方法についての指針はありますか?
EDITマップ{ }ConcurrentModificationException
のコピーを返すときにスローされる
と思います。Maps.newHashMap(myMap);
このプロセス中にイテレータはコピーを作成し、イテレータが動作している間にマップの内容が変更された場合は満足できません。