【发布时间】:2010-12-07 00:41:56
【问题描述】:
我不明白为什么当我遍历这个multimap 时会收到 ConcurrentModificationException。
我阅读了以下entry,但我不确定我是否理解了整个事情。
我试图添加一个同步块。但我的疑问是与什么同步,何时同步。
multimap 是一个字段,创建方式如下:
private Multimap<GenericEvent, Command> eventMultiMap =
Multimaps.synchronizedMultimap(HashMultimap.<GenericEvent, Command> create());
并像这样使用:
eventMultiMap.put(event, command);
和这样(我尝试在地图上同步这部分,但没有成功)
for (Entry<GenericEvent, Command> entry : eventMultiMap.entries()) {
if (entry.getValue().equals(command)) {
eventMultiMap.remove(entry.getKey(), entry.getValue());
nbRemoved++;
}
}
【问题讨论】:
标签: java concurrency guava multimap concurrentmodification