- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
The iterator does NOT support the remove method.
但为什么它在增强型 for 循环中起作用?
List<String> lst = new CopyOnWriteArrayList<>();
lst.add("one");
lst.add("two");
lst.add("three");
for (String str : lst) {
if (str.equals("one")) {
lst.remove("two"); // no ConcurrentModificationException
}
}
System.out.println(lst); // [one, three] => removed "two" !
List<String> lst = new ArrayList<>();
会生成 ConcurrentModificationException
Javadoc 明确指出 CopyOnWriteArrayList.iterator()
不支持 remove()
=> 它会抛出 UnsupportedOperationException
!我知道它是弱一致的——说没有 ConcurrentModificationException
如果我在从 CopyOnWriteArrayList
附言抱歉,我不专心 - 我没有在迭代器上调用 remove()!我对在 enhanced-for 内部感到困惑(它隐式使用迭代器)。
最佳答案
CopyOnWriteArrayList 迭代器故障安全实现支持修改操作。
当您迭代 CopyOnWriteArrayList 和 CopyOnWriteArraySet 时,迭代器使用基础列表(或集合)的快照,并且不会反射(reflect)在创建快照后对列表或集合的任何更改。迭代器永远不会抛出 ConcurrentModificationException。
阅读更多信息:https://markusjais.com/java-concurrency-understanding-copyonwritearraylist-and-copyonwritearrayset/
顺便说一句,在 ArrayList() 等经典 List 实现中,您不需要显式使用迭代器。使用 list.removeIf(predicate) 方法。
关于java - 为什么CopyOnWriteArrayList的迭代器允许在增强型for循环中使用remove(),而它的迭代器不支持remove()操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57827559/
简述 选中类,执行Ctrl + alt + U 查看类的继承示意图如下: public class CopyOnWriteArrayList implements List, RandomAc
我目前正在使用 ExecutorService 在 CopyOnWrite ArrayLists 中发送批处理字符串以进行并行处理,其中处理这些列表的 Runnable 任务需要遍历列表并对每个字符串
我正在使用一个集合 CopyOnWriteArrayList myCOW = new CopyOnWriteArrayList(); 其中 X 是一个可变对象。我从 Java Concurr
许多列表实现都有一个选项来指定集合的初始容量,为什么 CopyOnWriteArrayList 不允许这样做? 最佳答案 在传统的ArrayList中,容量是在后备数组中保留更多空间的提示,以便稍
根据 CopyOnWriteArrayList JavaDoc: A thread-safe variant of java.util.ArrayList in which all mutative
可能很简单,但我自己无法弄清楚。我有一些 Spring Boot WebSockets 实现示例,并且想要显示总 Activity session 。所以我创建了@Scheduled activeSe
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
这个问题已经有答案了: In what situations is the CopyOnWriteArrayList suitable? [duplicate] (2 个回答) 已关闭 5 年前。 我
我从一些教程中理解了这个概念,只要我知道一个线程何时迭代列表,就允许其他线程修改底层列表,我们不会得到 ConcurrentModificationException(CME),但在 ArrayLis
CopyOnWriteArrayList 被标记为可序列化。但它的内部状态是短暂的。任何人都可以回答我们试图在这种类型的列表中序列化什么。 /** The array, accessed only v
根据 CopyOnWritearrayList 的 javadoc : A thread-safe variant of ArrayList in which all mutative operati
我想使用可以被多线程代码不断访问的数据结构。请让我知道 java 中的任何数据结构,我可以在其中等待并使用通知选项。请看下面的代码,为什么CDRemove的run方法不打印所有值 public cla
我需要将一些对象存储到数据库中。 首先 我将它们存储在内存中(存储在集合中) 当其中一个正确存储在数据库中时,我会将其删除 所以, public class AuditService { pr
我试图将 CopyOnWriteArrayList 理解为我的代码: 我的代码是: public class AuditService { private CopyOnWriteArrayLi
我正在为 OCP 考试做一些练习。目前,我尝试打印 CopyOnWriteArrayList 的内容当使用多个线程时。根据文档,CopyOnWriteArrayList 的迭代器将 print the
拥有一个映射,按类型作为键保存同一事件的事件监听器列表, func_1() 将开始从映射中获取一种类型的监听器列表,并迭代该列表以处理每个监听器的事件。 当一个监听器完成处理后,它会要求将其从 map
我查看了 OpenJDK source code的CopyOnWriteArrayList似乎所有写操作都受到同一个锁的保护,而读操作则根本不 protected 。据我了解,在 JMM 下,对变量的
我想在内部了解并发修改异常是如何在并发集合中处理的,例如 ConcurrentHashMap 和 CopyOnWriteArrayList。 网上有很多博客建议使用这两种数据结构来避免并发修改异常。但
CopyOnWriteArrayList 的 Javadocs 说 A thread-safe variant of ArrayList in which all mutative operation
我查看了 OpenJDK source code的 CopyOnWriteArrayList似乎所有的写操作都受到同一个锁的保护,而读操作根本不 protected 。据我了解,在 JMM 下,对变量
我是一名优秀的程序员,十分优秀!