ConcurrentModificationException

ConcurrentModificationException

本文介绍了从ArrayList中删除元素时发生ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,Java抛出ConcurrentModificationException。 Anyidea为什么?

Java is throwing ConcurrentModificationException when i am running following code. Anyidea why is that ?

ArrayList<String> list1 = new ArrayList<String>();
list1.add("Hello");
list1.add("World");
list1.add("Good Evening");

for (String s : list1){
        list1.remove(2);
    System.out.println(s);
}


推荐答案

的文档,您会发现 p>

If you take a look at documentation of ConcurrentModificationException you will find that

例如,通常不允许一个线程修改
a集合,而另一个线程正在迭代

...

请注意,这个异常并不总是表示一个对象有
被不同的线程同时修改。如果单个线程
发出一系列违反
对象的方法调用,那么该对象可能会抛出此异常。 例如,如果
线程在使用fail-fast迭代器迭代
集合时直接修改集合,则迭代器将抛出此
异常

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

重要的是这个例外是我们不能保证它会一直被抛出,如文档

Important thing about this exception is that we can't guarantee it will always be thrown as stated in documentation

也可从文档

(强调我)

操纵Collection(在你的情况下List)的内容,而通过增强型for循环遍历它的内容,因为你不是通过迭代器for-each在内部使用。

So you can't manipulate content of Collection (in your case List) while iterating over it via enhanced for loop because you are not doing it via iterator for-each is using internally.

要解决它,只需要自己的迭代器并在你的循环中使用它。要从集合中删除元素,请使用 remove ,如下例所示

To solve it just get your own Iterator and use it in your loop. To remove elements from collection use remove like in this example

Iterator<String> it = list1.iterator();
int i=0;
while(it.hasNext()){
    String s = it.next();
    i++;
    if (i==2){
        it.remove();
        System.out.println("removed: "+ s);
    }
}

这篇关于从ArrayList中删除元素时发生ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:48