How does an Iterator in Java know when to throw ConcurrentModification Exception(Java 中的 Iterator 如何知道何时抛出 ConcurrentModification 异常)
问题描述
我有以下代码引发 ConcurrentModificationException,因为我在同一个列表上使用了两个不同的迭代器,其中一个正在修改列表.所以,第二个迭代器在读取列表时会抛出异常,因为其他迭代器已经修改了列表.
I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some other iterator has modified the list.
List<Integer> list = new ArrayList<>();
populate(list);//A method that adds integers to list
ListIterator<Integer> iterator1 = list.listIterator();
ListIterator<Integer> iterator2 = list.listIterator();
while (iterator1.hasNext()) {
if(iterator1.next() < 5)
iterator1.remove();
}
while (iterator2.hasNext()){
if(iterator2.next() < 5) {
//Call handler
}
}
我的问题是 iterator2
如何知道 internally 如果 list
尚未到达某个元素,则它已被其他迭代器修改iterator1
还删除了哪个?它如何确定其他一些 iterator
已经改变了 list
?一种方法是跟踪大小,但这不是原因,因为其他一些迭代器可以替换任何元素.
My question is how does iterator2
know internally that the list
has has been modified by some other iterator if it has not reached an element which is yet removed by iterator1
? How does it figure out that some other iterator
has mutated the list
? One way could be keep track of size but that can't be the reason since some other iterator can just replace any element.
推荐答案
回答此类问题的一个好方法是查看源代码,例如 ArrayList 的源代码.搜索 ConcurrentModificationException
.
A good way to answer questions like this is to look at the source code, for example the source code for ArrayList. Search for ConcurrentModificationException
.
你应该能够说事情是这样工作的:
You should be able to tell that things work rather like this:
- 集合对象有一个修改计数,该计数从零开始,并在发生添加或删除或类似操作时增加.
- 创建迭代器对象时,我们将集合的当前修改计数存储在迭代器中.
- 每次使用迭代器时,它都会检查集合的 mod 计数与迭代器在创建时获得的 mod 计数.如果这些值不同,则会引发异常.
在您的情况下,列表中 iterator1
执行的删除操作会更改列表的结构操作计数 (modCount
).当 iterator2
被要求删除时,它会看到它最初收到的 expectedModCount
为 0,与列表的当前 mod 计数不同.
In your case, remove operations performed by iterator1
on the list change the structural operation count (modCount
) of the list. When iterator2
is asked to remove, it sees its expectedModCount
, which it received initially as 0, differing from the current mod count of the list.
需要注意的是,it.remove
是一个特例.当迭代器自己删除时,它的 expectedModCount
会相应地调整,以与底层列表保持同步.
It should be noted that it.remove
is a special case. When an iterator does a remove itself, its expectedModCount
adjusts accordingly, to keep in sync with the underlying list.
这篇关于Java 中的 Iterator 如何知道何时抛出 ConcurrentModification 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 中的 Iterator 如何知道何时抛出 ConcurrentModif
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01