- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在迭代 ArrayList
并将对象添加到辅助 ArrayList
时,我得到了 ConcurrentModificationException
。我真的不知道为什么,因为我没有编辑我正在迭代的列表。
这发生在我的代码的两个部分。这些是代码。
编辑 - 代码 1:
public static ConcurrentHashMap<Long, ArrayList<HistoricalIndex>> historicalIndexesMap = new ConcurrentHashMap<Long, ArrayList<HistoricalIndex>>();
ArrayList<HistoricalIndex> historicalIndexList = IndexService.historicalIndexesMap.get(id);
List<Double> tmpList = new ArrayList<Double>();
for(HistoricalIndex hi : historicalIndexList){ //EXCEPTION HERE
if((System.currentTimeMillis()-hi.getTimestamp()) >= ONE_MINUTE){
tmpList.add(hi.getIndex());
}
}
在上面的代码 1 中,我是否应该像这样复制 historicalIndexList:
ArrayList<HistoricalIndex> historicalIndexList = new ArrayList<HistoricalIndex>(IndexService.historicalIndexesMap.get(id));
而不是这样做: ?
ArrayList<HistoricalIndex> historicalIndexList = IndexService.historicalIndexesMap.get(id);
代码 2:
List<Double> tmpList = new ArrayList<Double>();
for(HistoricalIndex hi : list){ //EXCEPTION HERE
tmpList.add(hi.getIndex());
}
有人知道为什么会这样吗?
堆栈跟踪:
21:19:50,426 ERROR [stderr] (pool-9-thread-6) java.util.ConcurrentModificationException
21:19:50,429 ERROR [stderr] (pool-9-thread-6) at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
21:19:50,432 ERROR [stderr] (pool-9-thread-6) at java.util.ArrayList$Itr.next(ArrayList.java:831
最佳答案
让我们简要了解一下导致 ConcurrentModificationException 的原因。 ArrayList 在内部维护一个修改计数值,它只是一个整数,每次对列表进行修改时都会递增。创建迭代器时,它会获取该值的“快照”。然后,每次使用迭代器时,它都会检查该值的副本是否仍然与数组自己的副本相匹配。如果没有,则抛出异常。
这意味着要发生 ConcurrentModificationException,必须在您首次创建迭代器后对 ArrayList 进行一些修改(即在首次执行 for() 语句之后但在结束之前)。由于您的 for() 循环没有修改 ArrayList,这意味着在您遍历数组时一定有其他线程在更改数组。
编辑:在回复您的编辑时,是的,如果其他线程要更改它,您应该复制该数组。你甚至可以这样做:
for(HistoricalIndex hi: new ArrayList<HistoricalIndex>(historicalIndexList))
... 在开始循环时复制列表。
综上所述,ConcurrentModificationException 与我们通常认为的并发问题无关。通过修改迭代器循环内的数组,而不是通过迭代器的 remove() 方法,您可以很容易地在单个线程中获得一个。在这种情况下,“并发”意味着迭代和修改同时发生 - 无论是在相同还是不同的线程中。
关于迭代 ArrayList 时出现 Java ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818867/
这个问题在这里已经有了答案: Why is a ConcurrentModificationException thrown and how to debug it (8 个答案) 关闭 3 年前。
这个问题已经有答案了: Concurrent Modification exception [duplicate] (9 个回答) 已关闭 8 年前。 在oldSet.removeAll(已删除);抛
这个问题在这里已经有了答案: Concurrent Modification exception [duplicate] (9 个回答) 关闭 9 年前。 我的程序抛出 ConcurrentModi
我有以下结构: public class Object1{ private HashMap myMap; ... public void cancelItem(Item o)
在本例中使用 list.remove((Object)93) 会导致 ConcurrentModificationException: List list = new ArrayList<>(); l
更新应用程序后,我收到来自 Firebase 的错误几乎在所有 Activity 中。 Fatal Exception: java.lang.RuntimeException: Unable to s
我有大量的东西,一个重复迭代它们的线程,以及一个偶尔删除或添加单个东西的单独线程。事物在同步链表中: private List things = Collections.synchronizedLis
在 DTO 中我有 public class ProductTypesDto extends BaseDto { private List colors = new ArrayList<>();
我正在创建一个 Swing 应用程序来制作游戏。它在屏幕外的随机位置创建图像,当它们离开屏幕时我想将它们删除。请看一下代码片段: public void checkTrolls(){ //CAUSES
我在向 Android 表插入数据时遇到问题。这是我的 Dao 函数: @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(frei
我有一个 Grails (2.4.3) 应用程序,它使用 PersistenceListener 来监听 GORM 事件。 PersistenceListener 工作正常。在 PreUpdate 事
基于 Spring Boot、spring-data-jpa 的大型 REST Web 应用程序。我有 PersonEntity ,它与 ParentEntity 具有 ManyToOne 关系。关系
我有一个作业要编写一个 Java 程序,该程序将读取上下文无关语法并返回所有非终结符的 FirstSet。我使用 FirstSet() 方法采用了递归方法: public static ArrayLi
我收到java.util.ConcurrentModificationException,但我不知道为什么。 在 Logcat 中,它指向此代码,但我没有看到任何可能导致 ConcurrentModi
我在以下情况下收到 ConcurrentModificationException 错误。线路发生这种情况的地方标有“ list = Collections.synchronizedList(them
自过去两个小时以来,我一直在尝试解决此异常..我得到了抛出异常的代码行..但没有找到解决方案..请帮助我发生错误@ mUsers.add(user);//在其他部分 private void read
我有以下代码: List list = getItems(); //where getItems() returns List do { list.add(ad
我得到ConcurrentModificationException当迭代 map 的内容时 for (String sourceKey : sMap.getContent().keySet(
代码是葡萄牙语的,对此我很抱歉。 我在这里读到另一个问题,因为我正在使用progSelecionada.remove(),所以抛出了异常,所以我更改为iterator.remove()但错误仍然存
我目前正在用 Java 编写多人游戏。我当前的代码(即出现错误)是这样的。 @Override public void onClose(WebSocket conn, int code, String
我是一名优秀的程序员,十分优秀!