- 浏览: 136436 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (172)
- java (172)
- c# winform 关于窗体最大化时的是否全屏效果与是否遮盖任务栏 (1)
- c# winform 只运行一个程序实例 (1)
- Shiro权限框架 (1)
- Java字节码(.class文件)的代码解析 (1)
- Hibernate、Spring和Struts工作原理及使用理由 (1)
- 基本PKG处理命令 (1)
- 最近写的代码的小结 (1)
- Dual Palindromes (1)
- 编译TortoiseSVN1.7源代码笔记 (1)
- Android项目代码混淆 (1)
- 关于就业叨咕几句 (1)
- Java重定向System.out和System.err (1)
- IHS与WAS集成插件静默安装 (1)
- 专题地图概述 (1)
- 关于MapXtreme2004附带Sample不能运行的问题 (1)
- SQL注入漏洞 (1)
- 回顾过去 展望未来(写给自己) (1)
- DB2认证考试经验谈(700&&701) (1)
- 今天用到了RM格式文件的分割工具RealProducer (1)
- 搜索引擎还是很有发展前途的 (1)
- 泰利德破解,实现学校机房免费上网 (1)
- Arduino 引言:移动互联外设传感展望 (1)
- c/c++ static 用法总结(三版本合一) (1)
- JQuery与xml的组合谈 (1)
- Android开发之消息处理机制(一)——Handler (1)
- Android开发之PopupWindow (1)
- 解析BitmapData.getPixel32()返回值因何不准确 (1)
- 关于Ext引用js的顺序问题 (1)
- Js事件大全 (1)
- 使用JavaFX2.0编写国际象棋游戏 (1)
- Hibernate/JPA常见异常分析与解决 (1)
最新评论
-
amcucn:
这排版看得
Shiro权限框架 -
WAMING5:
这也太紧凑了,眼看花了
Shiro权限框架 -
longzhun:
大虾 ,看得出你很懒啊!发个贴 这样子怎么看!!!
Shiro权限框架 -
swanky_yao:
活干的不细!!
Spring+Hibernate框架下Mysql读写分离、主从数据库配 -
饶首建:
不错,不过用处比较少吧
批处理设ip和dns
工作中碰到个concurrentmodificationexception。代码如下:
list list = ...;for(iterator iter = list.iterator(); iter.hasnext();) { object obj = iter.next(); ... if(***) { list.remove(obj); }}<span style="color: #0000ff;"><br></span>在执行了remove方法之后,再去执行循环,iter.next()的时候,报java.util.concurrentmodificationexception(当然,如果remove的是最后一条,就不会再去执行next()操作了)<br><br>下面来看一下源码
public interface iterator<e> { boolean hasnext(); e next(); void remove();}public interface collection<e> extends iterable<e> { ... iterator<e> iterator(); boolean add(e o); boolean remove(object o); ...}<br>这里有两个remove方法<br><br>接下来来看看abstractlist
public abstract class abstractlist<e> extends abstractcollection<e> implements list<e> { //abstractcollection和list都继承了collection protected transient int modcount = 0; private class itr implements iterator<e> { //内部类itr 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); //执行remove对象的操作 if (lastret < cursor) cursor--; lastret = -1; expectedmodcount = modcount; //重新设置了expectedmodcount的值,避免了concurrentmodificationexception的产生 } catch(indexoutofboundsexception e) { throw new concurrentmodificationexception(); } } final void checkforcomodification() { if (modcount != expectedmodcount) //当expectedmodcount和modcount不相等时,就抛出concurrentmodificationexception throw new concurrentmodificationexception(); } } }remove(object o)在arraylist中实现如下: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++; //只增加了modcount ....}<span style="color: #0000ff;"><br><span style="color: #339966;"><br><strong style="color: #800000;">所以,产生concurrentmodificationexception的原因就是:<br>执行remove(object o)方法之后,modcount和expectedmodcount不相等了。然后当代码执行到next()方法时,判断了checkforcomodification(),发现两个数值不等,就抛出了该exception。<br>要避免这个exception,就应该使用remove()方法。</strong></span><br></span>这里我们就不看add(object o)方法了,也是同样的原因,但没有对应的add()方法。一般嘛,就另建一个list了<br><br><br>下面是网上的其他解释,更能从本质上解释原因:<br>iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 iterator 会马上抛出 java.util.concurrentmodificationexception 异常。<br>所以 iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 iterator 本身的方法 remove() 来删除对象, iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。
发表评论
-
Hibernate/JPA常见异常分析与解决
2012-02-07 17:24 14671. ClassNotFoundException: ... -
使用JavaFX2.0编写国际象棋游戏
2012-02-07 16:24 1058前面发布了不少Javafx2的教程,不觉手痒,想尝试一 ... -
Js事件大全
2012-02-04 15:34 745一般事件 事件 ... -
关于Ext引用js的顺序问题
2012-02-02 15:49 719今天自己搞了个ext的helloworld,所需的包都 ... -
解析BitmapData.getPixel32()返回值因何不准确
2012-02-02 12:29 955<div style="widt ... -
Android开发之PopupWindow
2012-01-31 13:48 1646<h1>Android开发之PopupWi ... -
Android开发之消息处理机制(一)——Handler
2012-01-11 15:19 1129<h1>Android开发之消息处理机制( ... -
JQuery与xml的组合谈
2011-12-21 17:18 769JQuery与xml的组合谈 今天谈的是XML,学ja ... -
c/c++ static 用法总结(三版本合一)
2011-12-21 10:34 783<span style="&qu ... -
Arduino 引言:移动互联外设传感展望
2011-12-20 14:08 1293作者:mznewfacer (Wolf Geek) ... -
泰利德破解,实现学校机房免费上网
2011-12-20 13:58 919泰利德破解,实现学校机房免费上网<br> ... -
搜索引擎还是很有发展前途的
2011-12-19 11:49 924相信大家已经听说,在头几天,搜狐推出了一个专业搜索门户 ... -
今天用到了RM格式文件的分割工具RealProducer
2011-12-19 09:19 669项目要加一些视频文件,这些视频都是几个老师对数据结构课 ... -
DB2认证考试经验谈(700&&701)
2011-12-16 17:17 1078很多人都通过了D ... -
回顾过去 展望未来(写给自己)
2011-12-15 16:19 1445本来想用“昨天今天明天”做标题来着,但是人家本山大叔说 ... -
SQL注入漏洞
2011-12-15 12:19 690CSDN上的ASP.NET电子杂志下载下来看,就看到了 ... -
关于MapXtreme2004附带Sample不能运行的问题
2011-12-15 10:54 645</span> -
专题地图概述
2011-12-14 12:49 800<p class="MsoNorma ... -
IHS与WAS集成插件静默安装
2011-12-14 12:04 934<span style="col ... -
Java重定向System.out和System.err
2011-12-13 14:49 1139<div>继承PrintStream类: ...
相关推荐
java.util.ConcurrentModificationException 异常问题详解1
java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util....
主要介绍了出现java.util.ConcurrentModificationException 问题及解决办法的相关资料,需要的朋友可以参考下
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
Exception in thread “main“ java.util.InputMismatchException
java.util.Date与java.sql.Date互转及字符串转换为日期时间格式.docx
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
详细介绍java.util.Date和java.sql.Date相互转换的多种方法总结,希望对大家有帮助
java并发工具包 java.util.concurrent中文版-带书签版
java.util包
java.util.pdf
世界范围内的时区列表。由 java.util.TimeZone 类导出
java.util包源码,pdf版,方便打印
这是我在编写struts2中遇到的问题,整理出来,包括截图,希望可以帮到大家
使用java.util.timer实现的简单定时任务,在实现简单一次性定时任务时,使用java.util.timer非常的简单易用,适合没有接触过quartz的新手急用。
java.util包总结,方便大家学习。请多指教。