发生ConcurrentModificationException异常的原因分析
2014-10-09 19:08
537 查看
用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作。下面用List为例来说明为什么会报ConcurrentModificationException 这个异常,其它集合类似可以自己思考。
public static void main(String[] args){
List<String> set = new ArrayList<String>();
set.add(“a10001″);
set.add(“a10002″);
set.add(“a10003″);
set.add(“a10004″);
set.add(“a10005″);
set.add(“a10006″);
set.add(“a10007″);
List<String> del = new ArrayList<String>();
del.add(“a10003″);
del.add(“a10004″);
del.add(“a10005″);
for(String str : set)
{
if(del.contains(str))
{
set.remove(str);
}
}
}
Exception in thread “main” java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at com.debug.Debug.main(Debug.java:28)
大家都知道for(String str : set) 这句话实际上是用到了集合的iterator() 方法
在iterator的时候是产生了一个List的内部类Itr
JDK源码
在new Itr()时有一个关键性的操作
int expectedModCount = modCount;
再回头看一下List 的 remove方法
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size – index – 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null;
}
再看一下 iterator.next()操作
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
相信看到这儿大家已经应该明白了为什么会出现在这个异常了。
总结:
iterator时将expectedModCount = modCount 在remove()时 modCount++ 在next()时
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
一旦删除或添加元素后modCount,expectedModCount这两个值就会不一致,当next时就会报ConcurrentModificationException
了解了原由,解决方案就很简单了 在遍历时用一个集合存放要删除的对象 在遍历完后调用removeAll(Collection<?> c)就不会发生异常了。
转载自:http://www.strutshome.com/index.php/archives/569
public static void main(String[] args){
List<String> set = new ArrayList<String>();
set.add(“a10001″);
set.add(“a10002″);
set.add(“a10003″);
set.add(“a10004″);
set.add(“a10005″);
set.add(“a10006″);
set.add(“a10007″);
List<String> del = new ArrayList<String>();
del.add(“a10003″);
del.add(“a10004″);
del.add(“a10005″);
for(String str : set)
{
if(del.contains(str))
{
set.remove(str);
}
}
}
Exception in thread “main” java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at com.debug.Debug.main(Debug.java:28)
大家都知道for(String str : set) 这句话实际上是用到了集合的iterator() 方法
在iterator的时候是产生了一个List的内部类Itr
JDK源码
public Iterator<E> iterator() { return new Itr(); }
在new Itr()时有一个关键性的操作
int expectedModCount = modCount;
再回头看一下List 的 remove方法
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size – index – 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null;
}
再看一下 iterator.next()操作
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
相信看到这儿大家已经应该明白了为什么会出现在这个异常了。
总结:
iterator时将expectedModCount = modCount 在remove()时 modCount++ 在next()时
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
一旦删除或添加元素后modCount,expectedModCount这两个值就会不一致,当next时就会报ConcurrentModificationException
了解了原由,解决方案就很简单了 在遍历时用一个集合存放要删除的对象 在遍历完后调用removeAll(Collection<?> c)就不会发生异常了。
转载自:http://www.strutshome.com/index.php/archives/569
相关文章推荐
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用set时发生Java ConcurrentModificationException异常原因和解决方法
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 循环List删除元素抛ConcurrentModificationException异常原因分析
- 修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- Java ConcurrentModificationException 异常分析与解决方案
- ArrayList引起的ConcurrentModificationException 异常原因及解决方法
- java.util.ConcurrentModificationException异常分析
- java.util.ConcurrentModificationException 异常的分析
- ConcurrentModificationException 的异常分析
- 【转】Java ConcurrentModificationException异常原因和解决方法
- Java ConcurrentModificationException异常原因和解决方法
- ConcurrentModificationException异常原因
- Java ConcurrentModificationException异常原因和解决方法
- Java ConcurrentModificationException 异常分析与解决方案
- Java ConcurrentModificationException异常原因和解决方法
- Java ConcurrentModificationException异常原因和解决方法
- 一个ConcurrentModificationException的原因分析