修改集合抛出java.util.ConcurrentModificationException
2016-09-28 10:51
676 查看
一、问题引出
在对集合进行迭代的时候,会想动态对集合做修改(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);重新运行,就会解决这个问题。
相关文章推荐
- 深入分析集合并发修改异常(源码分析)java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException的解决办法 大家应该都知道, 在java中, 在对一些集合迭代的过程中对集合进行一些修改的操作, 比如说add,re
- 遍历集合时删除元素,抛出java.util.ConcurrentModificationException的解决办法
- 在Map或者Collection的时候,不要用它们的API直接修改集合的内容(否则会出现 java.util.ConcurrentModificationException 异常)
- 使用迭代器遍历集合时,当集合中的数据发生变化是会抛出java.util.ConcurrentModificationException异常
- java.util.ConcurrentModificationException ,遍历集合并同时修改集合,并发造成的异常解决办法
- 遍历集合时删除元素,抛出java.util.ConcurrentModificationException的解决办法
- 多线程报错 : Exception in thread "Thread-3" java.util.ConcurrentModificationException 并发修改异常
- Java.util.ConcurrentModificationException的理解与修改
- Android并发修改异常:java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException:并发修改异常!
- Java集合之ConcurrentModificationException(并发修改异常)分析
- 集合遍历 java.util.ConcurrentModificationException
- 遍历Map时抛出java.util.ConcurrentModificationException异常的解决办法
- java 并发操作list集合导致java.util.ConcurrentModificationException
- java.util.ConcurrentModificationException(并发修改错...
- java.util.ConcurrentModificationException 与集合删除
- java.util.ConcurrentModificationException 集合remove异常
- protostuff序列化集合的问题(反序列化时的java.util.ConcurrentModificationException)
- java 并发操作list集合导致java.util.ConcurrentModificationException