ConcurrentModificationException

发生ConcurrentModificationException场景分析


  1. 循环list并删除list中的元素,code:
    public class ConcurrentModificationExceptionTest {
    public static void main(String[] args) {
    // 初始化包含0-9共10个Integer对象的ArrayList;
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 10; i++) {
    list.add(i);
    }
    for (int i : list) { // 删除第一个元素后,此处将出现ConcurrentModifactionException
    list.remove(i);
    }
    }
    }

    异常原因分析:for (int i : list) 代码会创建该list对象的iterator对象,并通过iterator.next()方法获取list的下一个元素,next()方法会同时调用checkForComodification()方法校验list对象的modCount值是否与iterator对象中的expectedModCount值相等,当不相等时则抛出ConcurrentModificationException异常。第一次执行remove后list对象的modCount值会+1,而iterator对象中expectedModCount值并未改变。故删除后第二次执行iterator.next()方法时,会出现ConcurrentModificationException异常。当需要循环并删除list中元素时,应采用iterator.remove()方法。
  2. 多线程操作同个list对象,code:
    public class ConcurrentModificationExceptionTest {
    static List<Integer> list = new ArrayList<Integer>();
    public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
    list.add(i);
    }
    new Thread() {
    public void run() {
    Iterator<Integer> it = list.iterator();
    while (it.hasNext()) {
    try {
    Thread.sleep(50L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(it.next());//出现ConcurrentModificationException异常
    }
    }
    }.start();
    for (int i = 10; i < 20; i++) {
    try {
    Thread.sleep(50L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    list.add(i);
    }
    }
    }

    异常原因分析:出现原因与场景1类似,thread0循环调用iterator.next()方法时,main线程正在修改list对象的modCount值,当modCount值与iterator的expectedModCount值不想等时出现ConcurrentModificationException。

java中非线程安全的Collection子类均采用这种机制来阻止多线程下对它的修改,这种机制称为Fast-Fail。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值