首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java ConcurrentModificationException

Java ConcurrentModificationException
EN

Stack Overflow用户
提问于 2013-02-21 11:56:26
回答 4查看 2.2K关注 0票数 6

删除倒数第二个元素时,没有ConcurrentModificationException

代码语言:javascript
复制
List<String> myList1 = new ArrayList<String>();
Collections.addAll(myList1, "str1","str2","str3","str4","str5");
for(String element : myList1){//no ConcurrentModificationException here
if(element.equalsIgnoreCase("str4"))
    myList1.remove("str4");
}
System.out.println(myList1);

但是,当删除其他元素时,会出现一个ConcurrentModificationException

代码语言:javascript
复制
List<String> myList2 = new ArrayList<String>();
Collections.addAll(myList2, "str1","str2","str3","str4","str5");
for(String element : myList2){//ConcurrentModificationException here
if(element.equalsIgnoreCase("str1"))
    myList2.remove("str1");
}
System.out.println(myList2);

原因何在?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-21 12:26:23

我也看到了同样的事情,

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Launcher 
{
    public static void main(String[] args) 
    {
        doThis();
        doThat();
    }

    private static void doThis()
    {
        System.out.println("dothis");
        try
        {
            List<String> myList1 = new ArrayList<String>();
            Collections.addAll(myList1, "str1","str2","str3","str4","str5");
            for(String element : myList1){//no ConcurrentModificationException here
            if(element.equalsIgnoreCase("str4"))
                myList1.remove("str4");
            }
            System.out.println(myList1);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void doThat()
    {
        System.out.println("dothat");
        try
        {
            List<String> myList2 = new ArrayList<String>();
            Collections.addAll(myList2, "str1","str2","str3","str4","str5");
            for(String element : myList2){//ConcurrentModificationException here
            if(element.equalsIgnoreCase("str1"))
                myList2.remove("str1");
            }
            System.out.println(myList2);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

它输出,

代码语言:javascript
复制
dothis
[str1, str2, str3, str5]
dothat
java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
    at java.util.AbstractList$Itr.next(Unknown Source)
    at com.foo.Launcher.doThat(Launcher.java:41)
    at com.foo.Launcher.main(Launcher.java:12)

我找到了。

票数 3
EN

Stack Overflow用户

发布于 2013-02-21 12:03:32

Java使用modCount(修改计数)和expectedCount来测试列表是否有修改。

代码语言:javascript
复制
final void checkForComodification() {
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

在这两种情况下,删除后的modCount为6,但expectedModCount为5。

问题出在hasNext()。

代码语言:javascript
复制
public boolean hasNext() {
    return cursor != size;
}

列表使用光标和大小来检查是否有下一个元素。hasNext()发生在checkForComodification之前,因为checkForComodification()是在next()方法中调用的。

代码语言:javascript
复制
    public boolean hasNext() {
        return cursor != size;
    }

    @SuppressWarnings("unchecked")
    public E next() {
        checkForComodification();
        int i = cursor;
        if (i >= size)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1;
        return (E) elementData[lastRet = i];
    }

因此,当您删除倒数第二个元素时,cursor=4和size=4也会被删除。hasNext()返回false。跳出循环并打印结果。

票数 3
EN

Stack Overflow用户

发布于 2013-02-21 12:25:01

javac为for-each构建的实际代码是

代码语言:javascript
复制
    Iterator<String> i = myList1.iterator();
    while(i.hasNext()) {
        String element = i.next();
        if (element.equalsIgnoreCase("str4"))
            myList1.remove("str4");
    }

这是ArrayList Iterator.hasNext的实现

代码语言:javascript
复制
    public boolean hasNext() {
        return cursor != size;
    }

正如我们所看到的,hasNext()不检查并发修改,所以当我们删除倒数第二个元素时,循环结束而没有注意到问题。

实际上,奇怪的是next()remove()会检查并发修改,而hasNext()不会。快速失效迭代器应该检测bug,但是我们的bug没有被注意到。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14994277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档