如果在 doSomething() 中修改了列表,此代码将引发并发修改异常。是否可以通过将代码包含在某个同步块中来避免它?
List l = Collections.synchronizedList(new ArrayList());
// normal iteration -- can throw ConcurrentModificationException
// may require external synchronization
for (Iterator i=list.iterator(); i.hasNext(); ) {
doSomething(i.next());
}