java.util.ConcurrentModificationException详解

解决办法:

1、采用迭代器来迭代非线程安全的容器。
2、可以采用线程安全的容器。

问题导入

经常在迭代集合元素时,会想对集合做修改(add/remove)操作,类似下面这段代码:

Iterator<Integer> it = list.iterator()
while (it.hasNext()) {
    Integer val = it.next();
    if (val == 5) {
        // 注意:这里仍然是调用list的删除方法。
        list.remove(val);
        // 正确删除:it.remove();
    }
}

运行这段代码,会抛出异常java.util.ConcurrentModificationException。

解决办法

(以ArrayList来讲解)在ArrayList中,它的修改操作(add/remove)都会对modCount这个字段+1,modCount表示修改的次数,也可以看做版本号,每次集合中的元素被修改后,都会+1(即使溢出)。

// AbstractList的源码定义
protected transient int modCount = 0;
// ArrayList的remove()
public E remove(int index) {
        rangeCheck(index);
        // 修改次数加1,但是并没有重新给expectedModCount 赋值
        modCount++;   
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

接下来再看看AbsrtactList中iteraor方法。


public Iterator<E> iterator() {
    return new Itr();
}

它返回一个内部类,这个类实现了iterator接口,代码如下:


private class Itr implements Iterator<E> {
    int cursor = 0;

    int lastRet = -1;

    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size();
    }

    public E next() {
        checkForComodification();
        try {
            E next = get(cursor);
            lastRet = cursor++;
            return next;
        } catch (IndexOutOfBoundsException e) {
            checkForComodification();
            throw new NoSuchElementException();
        }
    }

    public void remove() {
        if (lastRet == -1)
            throw new IllegalStateException();
        checkForComodification();

        try {
            AbstractList.this.remove(lastRet);
            if (lastRet < cursor)
                cursor--;
            lastRet = -1;
            // 修改expectedModCount 的值,重新赋值,保持一致。
            expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
            throw new ConcurrentModificationException();
        }
    }

    final void checkForComodification() {
        // 检查当前版本号与期望版本号是否相同
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
 }

在内部类Itr中,有一个字段expectedModCount ,初始化时等于modCount,即当我们调用list.iterator()返回迭代器时,该字段被初始化为等于modCount。在类Itr中next/remove方法都有调用checkForComodification()方法,在该方法中检测modCount == expectedModCount,如果不相当则抛出异常ConcurrentModificationException。

前面说过,在集合的修改操作(add/remove)中,都对modCount进行了+1。
在看看刚开始提出的那段代码,在迭代过程中,执行list.remove(val),使得modCount+1,当下一次循环时,执行 it.next(),checkForComodification方法发现modCount != expectedModCount,则抛出异常。

【解决办法】

如果想要在迭代的过程中,执行删除元素操作怎么办?
再来看看内部类Itr的remove()方法,在删除元素后,有这么一句expectedModCount = modCount,同步修改expectedModCount 的值。所以,如果需要在使用迭代器迭代时,删除元素,可以使用迭代器提供的remove方法。对于add操作,则在整个迭代器迭代过程中是不允许的。 其他集合(Map/Set)使用迭代器迭代也是一样。

HashMap的问题

Fail-Fast 机制
我们知道 java.util.HashMap 不是线程安全的,因此如果在使用迭代器的过程中有其他线程修改了map,那么将抛出ConcurrentModificationException,这就是所谓fail-fast策略。这一策略在源码中的实现是通过 modCount 域,modCount 顾名思义就是修改次数,对HashMap 内容的修改都将增加这个值,那么在迭代器初始化过程中会将这个值赋给迭代器的 expectedModCount。在迭代过程中,判断 modCount 跟 expectedModCount 是否相等,如果不相等就表示已经有其他线程修改了 Map。

所以在这里和大家建议,当大家遍历那些非线程安全的数据结构时,尽量使用迭代器

Java中增强for循环的原理(字节码中理解)

public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);

        for (int val : list) {
            System.out.println(val);
            list.remove((Integer)3);
        }
    }

结果会抛异常:
Exception in thread “main” java.util.ConcurrentModificationException

public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/util/ArrayList
       3: dup
       4: invokespecial #3                  // Method java/util/ArrayList."<init>":()V
       7: astore_1
       8: aload_1
       9: iconst_1
      10: invokestatic  #4                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      13: invokeinterface #5,  2            // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      18: pop
      19: aload_1
      20: iconst_2
      21: invokestatic  #4                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      24: invokeinterface #5,  2            // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      29: pop
      30: aload_1
      31: iconst_3
      32: invokestatic  #4                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      35: invokeinterface #5,  2            // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      40: pop
      41: aload_1
      42: iconst_4
      43: invokestatic  #4                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      46: invokeinterface #5,  2            // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      51: pop
      52: aload_1
      // 底层字节码显示,for循环仍然调用的Iterator的
      53: invokeinterface #6,  1            // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      58: astore_2
      59: aload_2
      // 调用Iterator的hashNext(),判断是否有下一个元素
      60: invokeinterface #7,  1            // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          102
      68: aload_2
      // 调用Iterator的next(),得到下一个元素
      69: invokeinterface #8,  1            // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      74: checkcast     #9                  // class java/lang/Integer
      // 拆箱操作
      77: invokevirtual #10                 // Method java/lang/Integer.intValue:()I
      80: istore_3
      81: getstatic     #11                 // Field java/lang/System.out:Ljava/io/PrintStream;
      84: iload_3
      85: invokevirtual #12                 // Method java/io/PrintStream.println:(I)V
      88: aload_1
      89: iconst_3
      90: invokestatic  #4                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      // 删除元素使用的list.remove(),会抛出异常。
      93: invokeinterface #13,  2           // InterfaceMethod java/util/List.remove:(Ljava/lang/Object;)Z
      98: pop
      99: goto          59
     102: return
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值