为什么我在更新集合元素时没有得到并发修改异常?
Why im not getting concurrent modification exception during update of collection element?
我刚读到,如果
我们在调用迭代器方法后添加、删除或更新集合
我理解为什么添加和删除集合元素会导致并发修改异常,但为什么更新会导致并发修改?
毕竟我们在更新元素时不会在结构上改变任何东西。
例如下面的代码来自 arraylist 实现
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
我们不更新实际用于检查并发修改的变量"modcount"。
我也尝试了我的自定义代码:
public static void main(String[] args) {
ArrayList l= new ArrayList();
l.add("string");
l.add(3);
Iterator it=l.iterator();
Object o=it.next();
l.set(0, "element");
l.remove(o);
//l.add(7);
//it.next();
System.out.println(it.next());
System.out.println(l.get(0));
int i;
System.out.println(j+" "+j);
}
这也不会抛出并发修改异常。
我可以知道为什么吗?
why updating should cause concurrent modification?
如果"updating"是指调用set
方法,则不会引起并发修改。
在 List
中设置元素的值不是结构修改,因此在迭代期间执行时不会导致 ConcurrentModificationException
。
引自 ArrayList
Javadoc:
A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification
...
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
我刚读到,如果 我们在调用迭代器方法后添加、删除或更新集合
我理解为什么添加和删除集合元素会导致并发修改异常,但为什么更新会导致并发修改? 毕竟我们在更新元素时不会在结构上改变任何东西。
例如下面的代码来自 arraylist 实现
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
我们不更新实际用于检查并发修改的变量"modcount"。
我也尝试了我的自定义代码:
public static void main(String[] args) {
ArrayList l= new ArrayList();
l.add("string");
l.add(3);
Iterator it=l.iterator();
Object o=it.next();
l.set(0, "element");
l.remove(o);
//l.add(7);
//it.next();
System.out.println(it.next());
System.out.println(l.get(0));
int i;
System.out.println(j+" "+j);
}
这也不会抛出并发修改异常。
我可以知道为什么吗?
why updating should cause concurrent modification?
如果"updating"是指调用set
方法,则不会引起并发修改。
在 List
中设置元素的值不是结构修改,因此在迭代期间执行时不会导致 ConcurrentModificationException
。
引自 ArrayList
Javadoc:
A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification ...
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.