[英]Why doesn't ConcurrentModificationException happen with remove method of LinkedList?
Deque<Employee> employeeDeque = new LinkedList<>();
employeeDeque.offerLast(new Employee("Michael", 250));
employeeDeque.offerLast(new Employee("John", 250));
Iterator iterator = employeeDeque.iterator();
while (iterator.hasNext()) {
iterator.next();
employeeDeque.remove(new Employee("Michael", 250));
}
The same code with ArrayList with produce this exception, but remove on LinkedList doesn't.与 ArrayList 相同的代码会产生此异常,但在 LinkedList 上删除不会。 Why is it so?为什么会这样? Add() and Offer() methods still produce it. Add() 和 Offer() 方法仍然产生它。
The Javadoc for LinkedList
says, LinkedList
的Javadoc说,
Note that the fail-fast behavior of an iterator cannot be guaranteed...Fail-fast iterators throw
ConcurrentModificationException
on a best-effort basis.请注意,不能保证迭代器的快速失败行为……快速失败的迭代器会在尽力而为的基础上抛出ConcurrentModificationException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.