gpt4 book ai didi

java - 在单个集合上使用 iterator.remove() 的多个线程返回快速失败迭代器

转载 作者:行者123 更新时间:2023-12-02 03:30:13 41 4
gpt4 key购买 nike

甲骨文says

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

这是否意味着即使多个线程在同一个集合的快速失败实现上一起迭代(Vector,Hashmap ,ArrayList,HashSet) 对象执行 iterator.remove() 不会出现 ConcurrentModificationException抛出?

最佳答案

没有。这告诉您在迭代时(在一个线程中)删除元素的唯一安全方法是使用 iterator.remove。如果从其他线程访问(迭代或修改)集合 - 有时您会遇到异常,有时不会 - 一般情况下行为不是确定性的,因此您应该避免使用它或依赖它。

话虽如此,唯一的异常(exception)是并发集合。

关于java - 在单个集合上使用 iterator.remove() 的多个线程返回快速失败迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38201412/

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