简体   繁体   English

迭代ArrayList时出现ConcurrentModificationException <Integer>

[英]ConcurrentModificationException when iterating through an ArrayList <Integer>

I'm trying to iterate through a Integer ArrayList and getting the value at each element, but I'm getting an error at the int value = .... 我正在尝试迭代整数ArrayList并获取每个元素的值,但我在int值= ...时收到错误

Not sure whats happening. 不知道发生了什么。 Please advise. 请指教。

Iterator<Integer> listItr = executeList.iterator(); // iterator for the execute list 
    while (listItr.hasNext()) { // iterate through list and do work!
        int robIndex = listItr.next();
        int timer = fakeRob.exeCountDown(robIndex); // decrement first then return timer
        if (timer == 0) {// check if instr finished execution
            System.out.println("timer expired. fire");
            executeList.remove(executeList.indexOf(robIndex)); // 1. remove instr from exeList
            transitState(robIndex, EX, WB); // 2. transit from EX state to WB state
            int tag = fakeRob.getTag(robIndex); // get producer tag
            regFile.setRdy(tag); // 3a. set register file ready flag for this tag
            fakeRob.wakeUp(tag); // 3b. wake up instructions with this tag
        }
    }

Error: 错误:

java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at sim.execute(sim.java:180)
at sim.<init>(sim.java:71)
at sim.main(sim.java:270

Thanks, 谢谢,

Hank 汉克

If the local variable value is the thing that you "do some stuff with", and you are not modifying the list, then some outside thread is modifying the list while you are in there. 如果局部变量值是你“做一些东西”的事情,并且你没有修改列表,那么当你在那里时,一些外部线程正在修改列表。

Otherwise, see the link provided by @edalorozo for some ideas 否则, 请参阅@edalorozo提供的链接以获取一些想法

EDIT ADDED 编辑增加

I never use the iterator.remove() idiom, because I never got all that familiar with the iterator idiom. 我从不使用iterator.remove()习惯用法,因为我从来没有熟悉迭代器的习惯用法。 And always mixed it up with the short lived and poorly implemented Enumerator stuff. 并且总是将它与短暂的和实现不佳的Enumerator东西混在一起。 Before the enhanced for loop, I typically looped using the very old fashioned for (int i-0; i<foo.length; i++) style. 在增强的for循环之前,我通常使用非常老式for (int i-0; i<foo.length; i++)样式进行for (int i-0; i<foo.length; i++) And not all Iterators support remove(), so what's the point? 并非所有迭代器都支持remove(),那么重点是什么?

Therefore, I got "used to" the "collect everything and delete it afterwards" style, even when I now use the enhanced for loop. 因此,即使我现在使用增强的for循环,我也“习惯”“收集所有内容并在之后删除它”的样式。 In your code, that would be: 在您的代码中,这将是:

ArrayList <Integer> toBeRemoved = new ArrayList <Integer>();
for (Integer robIndex : executeList) {
   // note, I distrust auto-unboxing, (see all the Java Puzzlers books!)
   // so I'd probably add an explicit unbox here
   int robIndexi = robIndex.intValue();

   int timer = fakeRob.exeCountDown(robIndexi); // decrement first then return timer
   if (timer == 0) {// check if instr finished execution
      toBeRemoved.add(robIndex);
      // all that other stuff here...
   }
}

// remove everything now
executeList.removeAll(toBeRemoved);

Perhaps if you put what it is you're doing inside the loop it would help. 也许如果你把它在循环中的内容放在循环中它会有所帮助。 If you're trying to remove an element from the list, you need to call listItr.remove() to do it. 如果您尝试从列表中删除元素,则需要调用listItr.remove()来执行此操作。 In general, you should not be calling any functions in the loop which modify the list (ie. add(), set(), etc...). 通常,您不应该在循环中调用任何修改列表的函数(即add(),set()等等)。

The following code would trigger this 以下代码将触发此操作

Iterator<Integer> it = executeList.iterator();
while (it.hasNext()) {
  Integer i = it.next();
  executeList.remove(i);
}

The proper way to do it would be: 正确的方法是:

Iterator<Integer> it = executeList.iterator();
while (it.hasNext()) {
  Integer i = it.next();
  it.remove();
}

Also other threads (as mentioned above) could be the issue. 其他线程(如上所述)可能是问题。 Remember, iterators are backed by the list itself in all the java supplied collections. 请记住,迭代器由所有java提供的集合中的列表本身支持。 So if another thread modifies the list while you'e iterating, you'll run into this. 因此,如果另一个线程在您迭代时修改列表,您将遇到此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迭代ArrayList时Java ConcurrentModificationException - Java ConcurrentModificationException when iterating ArrayList 迭代Arraylist时的ConcurrentModificationException(不删除) - ConcurrentModificationException while iterating through Arraylist (not removing) 迭代并从 ArrayList 中删除元素时如何避免 java.util.ConcurrentModificationException - How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList 如何在迭代时仅避免ArrayList中的ConcurrentModificationException? - How do I avoid ConcurrentModificationException in ArrayList ONLY when iterating? 在LinkedList上迭代时出现ConcurrentModificationException - ConcurrentModificationException when iterating on LinkedList 遍历集合,在循环中删除对象时避免 ConcurrentModificationException - Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 通过HashMap迭代时出现ConcurrentModificationException - ConcurrentModificationException while iterating through HashMap Java:遍历ArrayList时出现NoSuchElementException - Java: NoSuchElementException when iterating through ArrayList 迭代map时出现ConcurrentModificationException - ConcurrentModificationException when iterating over map 当我在ArrayList上迭代时出现ConcurrentModificationException - ConcurrentModificationException when I iterate on ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM