修改集合抛出java.util.ConcurrentModificationException

一、问题引出

在对集合进行迭代的时候,会想动态对集合做修改(add/remove)操作,如下:

  for(JobItem i:jobItemList){
	if(item.getJobId() == i.getJobId()){
		jobItemList.remove(i);
	}
  }

运行程序,会抛出异常

 

java.util.ConcurrentModificationException
	<span style="color:#ff0000;">at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
	at java.util.AbstractList$Itr.next(Unknown Source)
	at com.my.threadpool.ExecuteThreadJob.delItem(ExecuteThreadJob.java:95)
	at com.my.threadpool.ExecuteThreadJob.pauseThreadJob(ExecuteThreadJob.java:32)
	at com.wallet.job.ThreadPoolTest.test(ThreadPoolTest.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)</span>

从异常信息可以发现,异常出现在checkForComodification()方法中。

 

二、原因分析:

查看ArrayList的源码中的iterator()这个方法,在其父类AbstractList中找到了iterator()方法的具体实现:

 

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

接着看Itr的具体实现,在AbstractList类中找到了Itr类的具体实现:

 

 

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 = modCount;
        } catch (IndexOutOfBoundsException e) {
        throw new ConcurrentModificationException();
        }
    }
 
    final void checkForComodification() {
        if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
    }
}

expectedModCount:表示对ArrayList修改次数的期望值,它的初始值为modCount,每次调用add()方法或者remove()方法就会对modCount进行加1操作。通过分析可以看出,如果modCount不等于expectedModCount,则抛出ConcurrentModificationException异常。在执行remove方法时,导致modCount和expectedModCount的值不一致。注意,像使用for-each进行迭代实际上也会出现这种问题。

 

三、解决方案

怎么解决这个问题呢?定义一个辅助collection,用来保存删除的对象,然后调用removeAll()方法。

 

List<JobItem> list = new ArrayList<JobItem>();
for(JobItem i:jobItemList){
     if(item.getJobId() == i.getJobId()){
	list.add(i);
    }
}
jobItemList.removeAll(list);

重新运行,就会解决这个问题。
 

 



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值