这不是同步问题。如果要迭代的基础集合被Iterator本身以外的任何东西修改,则会发生这种情况。
Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
Entry item = it.next();
map.remove(item.getKey());
}
第二次调用it.hasNext()
时,将抛出ConcurrentModificationException
。
正确的方法是
Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
Entry item = it.next();
it.remove();
}
假设此迭代器支持remove()
操作。