java集合--java.util.ConcurrentModificationException异常
ConcurrentModificationException 异常:并发修改异常,当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。一个线程对collection集合迭代,另一个线程对Collection进行修改的时候, 就会出现上面的异常.
下面看一下代码:
package cn.itcast.p4.list.demo; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class ListIteratorException { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String>list = new ArrayList<String>();
list.add("abc1");
list.add("abc2");
list.add("abc3");
list.add("abc4");
Iterator it = list.iterator();
while(it.hasNext()){
String s = (String) it.next();
if(s.equals("abc2")){
list.remove(s);
}else{
System.out.println(s);
}
}
System.out.println(list);
} }
运行结果:
abc1
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at cn.itcast.p4.list.demo.ListIteratorException.main(ListIteratorException.java:22)
看list.iterator()方法的源码:
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0; /**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1; /**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
通过看API AbstractList.class 发现该类里面有一个成员变量: protected transient int modCount = 0;
是不可被序列化的变量,当对集合进行add,remove,removeRange,addAll等修改动作的时候,没操作一次,modCount加1。
用来记录对集合修改的次数。
*/
int expectedModCount = modCount;//上面因为翻译过来,为迭代器的expectedModCount值和List集合中的modCount 初始值一致 public boolean hasNext() {
return cursor != size();
} public E next() {
checkForComodification();//每调用一次next方法,都要通过此方法检测expectedModCount(迭代对象的后)
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();
}
}
//用来检测现在的List中的modCount与创建迭代器的时候初始expectedModCount值是否想相等,不相等,返回异常ConcurrentModificationException
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
可能大家对modCount还不明白,再看下AbstractList.Class里面的源代码:
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { protected transient int modCount = 0;
class SubList<E> extends AbstractList<E> { 。。。。。。。。省略很多代码。。。。。。。。。。。。。
public void add(int index, E element) {
if (index<0 || index>size)
throw new IndexOutOfBoundsException();
checkForComodification();
l.add(index+offset, element);
expectedModCount = l.modCount;
size++;
modCount++;
} public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
expectedModCount = l.modCount;
size--;
modCount++;
return result;
} }
}
分析上面的源代码:
通过看API AbstractList.class 发现该类里面有一个成员变量: protected transient int modCount = 0, 是不可被序列化的变量,当对集合
进行add,remove,removeRange,addAll等修改动作的时候,每修改一次,modCount加1。用来记录对集合修改的次数。
private class Itr 类里面有一个成员变量expectedModCount,初始值和List中的modCount一样,只要在迭代的过程中线程没有对List集合进行
上面的修改动作,modCount值就不会变。那么expectedModCount=modCount条件始终成立,checkForComodification检测集合是否被修改的
方法里面的if判断不成立,不会抛出异常。
但是在我们的程序中,执行到list.remove(s);时候,一个线程对集合进行it.next()遍历,一个线程对集合进行remove动作,这样当删除"abc2"对象后,
list对象的成员变量modCount的值+1,但是迭代对象Itr 里面的expectedModCount不变,所以两个值不相等,等再次调用next()方法后,
调用checkForComodification方法,if条件成立,这样就抛出了异常。
java集合--java.util.ConcurrentModificationException异常的更多相关文章
- java.util.ConcurrentModificationException 异常问题详解
环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修改就会出现java.util.ConcurrentModificationException异常, ...
- java.util.ConcurrentModificationException异常原因及解决方法
在java语言中,ArrayList是一个很常用的类,在编程中经常要对ArrayList进行删除操作,在使用remove方法对ArrayList进行删除操作时,报java.util.Concurren ...
- java.util.ConcurrentModificationException异常分析
Java在操作ArrayList.HashMap.TreeMap等容器类时,遇到了java.util.ConcurrentModificationException异常.以ArrayList为例,如下 ...
- Java 迭代器删除元素ConcurrentModificationException异常。
Java是不支持容器类在使用迭代器迭代过程中,使用如 list.remove(obj)方法删除元素.否则会抛出ava.util.ConcurrentModificationException异常.应该 ...
- Java处理java.util.ConcurrentModificationException异常
代码: public static void reduce(HashMap<String, Integer> hashMap, final Integer count) { Iterato ...
- java.util.ConcurrentModificationException异常排查
java.util.ConcurrentModificationException对于这个异常我们一般会认为是在遍历list的时候对这个list做了add,remove等修改操作造成的,最近在线上 ...
- java.util.ConcurrentModificationException 异常解决的方法及原理
近期在修程序的bug,发现后台抛出下面异常: Exception in thread "main" java.util.ConcurrentModificationExceptio ...
- java.util.ConcurrentModificationException异常;java.util.ConcurrentModificationException实战
写代码遇到这个问题,很多博客文章都是在反复的强调理论,而没有对应的实例,所以这里从实例出发,后研究理论: 一.错误产生情况 1 .字符型 (1)添加 public static void main(S ...
- java.util.ConcurrentModificationException异常的解决
问题复现: List<String> list = new ArrayList<>();list.add("11");list.add("55&q ...
随机推荐
- iOS Foundation框架简介 -1.常用结构体的用法和输出
1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...
- Asp.net 生成验证码
生成验证码一般来说大体有这么几步: 1.生成验证码字符串,一般由四个或更多随机字符拼凑而成: 2.填充图片背景,并绘制图片的背景噪音线: 3.将验证码绘制到图片中: 4.绘制前景噪点: 5.返回图片流 ...
- 8款实用的Jquery瀑布流插件
1.网友Null分享Jquery响应式瀑布流布局插件 首先非常感谢网友Null的无私分享,此作品是一款响应式瀑布流布局Jquery插件,网友Null增加了一个屏幕自适应和响应式,响应式就是支持智能手机 ...
- Gulpfile.js——编译、压缩、合并js和css文件
gulp 一个入门教程:http://www.ydcss.com/gulp API文档地址:http://www.gulpjs.com.cn/docs/api/ 我的一个Low版的gulpfile v ...
- 所有外包项目威客网站列表----来自程序员接私活网qxj.me
猪八戒 http://www.zhubajie.com/ 有佣金,建议别去坑死了 csto http://www.csto.com/ 开源中国众包 https://zb.osch ...
- 30个HTML5学习资源
早在几个星期前,Adobe就发布了Dreamweaver CS5 HTML5 Pack的预览版下载.众所周知,HTML5在互联网领域掀起了一场大论战,并让Adobe的日子很难熬.HTML5致力于为前端 ...
- python 获取 mac 地址 的代码
python 获取 mac 地址 的例子,有需要的朋友可以参考下. #!/bin/python import os import re def GetMac(): if os.name == ...
- jQuery之$(document).ready()使用介绍
学习jQuery的第一件事是:如果你想要一个事件运行在你的页面上,你必须在$(document).ready()里调用这个事件 学习jQuery的第一件事是:如果你想要一个事件运行在你的页面上,你必须 ...
- ASP.NET中利用Split实现对Checkbox的字符串分离放到DataTable里面
一.背景 昨天唐欢问了我一个问题: 现在有一个CheckBox和一个Label如下图: 要实现选中CheckBox,点击下面打印按钮的时候要做成这个样子的如下图: 简单的说就是档案编号作为表中的一个列 ...
- c 指针兼容性问题
指针兼容性问题: const指针不能赋值给非const指针. 非const指针可以赋值给const 指针,但前提是只是一层间接运算 Example: int *pt1; const *pt2; con ...