ConcurrentModificationException的情况
ConcurrentModificationException
一个不该犯的低级错误,今天的代码突然抛了一个concurrentModificationException错误,
Iterator的一个基本概念没有掌握导致的这个错误,就是在Iterator的实现类
比如Hashtable里面的内部类
private class Enumerator<T> implements Enumeration<T>, Iterator<T>
会在next,或者remove的时候检查当前集合是否会在修改状态,如果是的话
就会抛出 ConcurrentModificationException,而他自己remove则是使用了同步的方法
而且同步了modCount;expectedModCount;
public T next() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return nextElement();
}
public void remove() {
if (!iterator)
throw new UnsupportedOperationException();
if (lastReturned == null)
throw new IllegalStateException("Hashtable Enumerator");
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
synchronized(Hashtable.this) {
Entry[] tab = Hashtable.this.table;
int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index], prev = null; e != null;
prev = e, e = e.next) {
if (e == lastReturned) {
modCount++;
expectedModCount++;
if (prev == null)
tab[index] = e.next;
else
prev.next = e.next;
count--;
lastReturned = null;
return;
}
}
throw new ConcurrentModificationException();
}
}
}
而自己在next的同时,修改了这个集合,导致了这个错误的出现
在Map或者Collection的时候,不要用它们的API直接修改集合的内容,如果要修改可以用Iterator的remove()方法,例如:
public void setReparation( Reparation reparation ) {
for (Iterator it = this.reparations.iterator();it.hasNext();){//reparations为Collection
Reparation repa = (Reparation)it.next();
if (repa.getId() == reparation.getId()){
this.reparations.remove(repa);
this.reparations.add(reparation);
}
}
}
如上写会在运行期报ConcurrentModificationException,可以如下修改:
public void setReparation( Reparation reparation ) {
boolean flag = false;
for (Iterator it = this.reparations.iterator();it.hasNext();){//reparations为Collection
Reparation repa = (Reparation)it.next();
if (repa.getId() == reparation.getId()){
it.remove();
flag = true;
break;
}
}
if(flag){
this.reparations.add(reparation);
}
}
ConcurrentModificationException的情况相关推荐
- ArrayList线程不安全三种解决情况
在多线程高并发的情况下arrayList会抛出concurrentModificationException 解决情况 1.vector 2. Collections.synchronizedList ...
- 终于,我读懂了所有Java集合——map篇
首先,红黑树细节暂时撸不出来,所以没写,承诺年前一定写 HashMap (底层是数组+链表/红黑树,无序键值对集合,非线程安全) 基于哈希表实现,链地址法. loadFactor默认为0.75,thr ...
- Python2.0 模块大全
博客 学院 下载 更多 写博客 qq779488143 Python 模块大全(很详细!) 转载 2014年12月12日 20:47:18 标签: Python / 模块 / 教程 3694 转载:. ...
- 一种隐蔽性较高的Java ConcurrentModificationException异常场景
前言 在使用Iterator遍历容器类的过程中,如果对容器的内容进行增加和删除,就会出现ConcurrentModificationException异常.该异常的分析和解决方案详见博文<Jav ...
- java.util.ConcurrentModificationException(并发修改错...
为什么80%的码农都做不了架构师?>>> public class ConcurrentModificationExceptionextends RuntimeExceptio ...
- 遍历Collection,避免在循环中删除对象时避免ConcurrentModificationException
我们都知道,由于ConcurrentModificationException您无法执行以下操作: for (Object i : l) {if (condition(i)) {l.remove(i) ...
- fail-fast(快速失败/报错机制)-ConcurrentModificationException
2019独角兽企业重金招聘Python工程师标准>>> 一.fail-fast机制(快速报错机制) 这是<Java编程思想>中关于快速报错机制的描述 Java容器有一种保 ...
- 遍历并批量删除容器中元素出现ConcurrentModificationException原因及处置
在以下四种遍历过程中,前两种会抛出ConcurrentModificationException,而后两种方法是正确的. Department类: package com.sitinspring; i ...
- List中subList方法抛出异常java.util.ConcurrentModificationException原理分析
1.首先从测试代码开始: public class Test {public static void main(String[] args) {List<Integer> list = n ...
最新文章
- 马尔科夫决策过程基本概念详解
- python 均值漂移
- 模仿王者荣耀的实时阴影
- 运行javac编译报错:仅当显式请求注释处理时才接受类名称“xxxxxx”
- as3 字符处理函数
- 项目关键路径与项目最长路径有可能不同
- mysql linq 事务_一步一步学Linq to sql(七):并发与事务
- 图像分割——超像素(Superpixels)分割(Matlab)
- matlab 上三角矩阵变为对称矩阵,已知上/下三角矩阵如何快速将对称阵补全
- linux手机拍照翻译软件,拍图识字翻译软件-拍图识字翻译app下载v1.1.3-Linux公社
- Android 消息机制之 MessageQueue 消息队列
- 给大家推荐一些好用的国内外免费图床(可外链的免费相册)
- 揭露SAP培训机构的套路
- tds for mysql_PostgreSQL9.3安装tds_fdw扩展
- 雨夜深思——开发平台(二)
- 单频点单输入功率只含基波X模型的提取与验证
- 苹果手机计算机软件不见了怎么办,苹果通讯录怎么不见了?苹果通讯录没了怎么办...
- 干货深挖!从写简历,到面试、谈薪酬的那些技巧和防坑指南
- U盘文件突然不见却占内存 解决方案
- The AudioContext was not allowed to start. It must be resumed after a user gesture on the page
热门文章
- 快速搭建LAMP环境
- 如何提高程序员的生产率 (1)
- 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)...
- 最长公共子序列 (nyoj36) [动态规划]
- RAID入门一页通,最全的RAID技术、原理图解
- 如今黑帽查找引擎优化的难点
- centos6+nagios3.3.1+nrpe2.12
- C#中sql备份与还原
- 桌面虚拟化之应用程序的整合
- Glomosim安装和ParseC的使用等相关链接