gpt4 book ai didi

java - 在 iterator.remove 上抛出 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-11-29 06:10:54 24 4
gpt4 key购买 nike

尽管使用迭代器执行删除操作,我还是收到了 ConcurrentModificationException。关于原因有什么想法吗?

for (Iterator<Thread> iter = threads.iterator(); iter.hasNext();) {
Thread hook = iter.next();
if(someCondition){
iter.remove();
}
}

最佳答案

来自 JavaDoc Iterator.remove() :

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

似乎行为取决于集合。也正如 aioobe 指出的那样。当我在其他地方修改集合时,我可以重现 ConcurrentModificationException。仅使用 Iterator 接口(interface),我只能重现 IllegalStateException

关于java - 在 iterator.remove 上抛出 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com