您的位置:首页 > 编程语言 > Java开发

修改集合抛出java.util.ConcurrentModificationException

2016-09-28 10:51 627 查看

一、问题引出

在对集合进行迭代的时候,会想动态对集合做修改(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);重新运行,就会解决这个问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐