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的情况相关推荐

  1. ArrayList线程不安全三种解决情况

    在多线程高并发的情况下arrayList会抛出concurrentModificationException 解决情况 1.vector 2. Collections.synchronizedList ...

  2. 终于,我读懂了所有Java集合——map篇

    首先,红黑树细节暂时撸不出来,所以没写,承诺年前一定写 HashMap (底层是数组+链表/红黑树,无序键值对集合,非线程安全) 基于哈希表实现,链地址法. loadFactor默认为0.75,thr ...

  3. Python2.0 模块大全

    博客 学院 下载 更多 写博客 qq779488143 Python 模块大全(很详细!) 转载 2014年12月12日 20:47:18 标签: Python / 模块 / 教程 3694 转载:. ...

  4. 一种隐蔽性较高的Java ConcurrentModificationException异常场景

    前言 在使用Iterator遍历容器类的过程中,如果对容器的内容进行增加和删除,就会出现ConcurrentModificationException异常.该异常的分析和解决方案详见博文<Jav ...

  5. java.util.ConcurrentModificationException(并发修改错...

    为什么80%的码农都做不了架构师?>>>    public class ConcurrentModificationExceptionextends RuntimeExceptio ...

  6. 遍历Collection,避免在循环中删除对象时避免ConcurrentModificationException

    我们都知道,由于ConcurrentModificationException您无法执行以下操作: for (Object i : l) {if (condition(i)) {l.remove(i) ...

  7. fail-fast(快速失败/报错机制)-ConcurrentModificationException

    2019独角兽企业重金招聘Python工程师标准>>> 一.fail-fast机制(快速报错机制) 这是<Java编程思想>中关于快速报错机制的描述 Java容器有一种保 ...

  8. 遍历并批量删除容器中元素出现ConcurrentModificationException原因及处置

    在以下四种遍历过程中,前两种会抛出ConcurrentModificationException,而后两种方法是正确的. Department类: package com.sitinspring; i ...

  9. List中subList方法抛出异常java.util.ConcurrentModificationException原理分析

    1.首先从测试代码开始: public class Test {public static void main(String[] args) {List<Integer> list = n ...

最新文章

  1. 马尔科夫决策过程基本概念详解
  2. python 均值漂移
  3. 模仿王者荣耀的实时阴影
  4. 运行javac编译报错:仅当显式请求注释处理时才接受类名称“xxxxxx”
  5. as3 字符处理函数
  6. 项目关键路径与项目最长路径有可能不同
  7. mysql linq 事务_一步一步学Linq to sql(七):并发与事务
  8. 图像分割——超像素(Superpixels)分割(Matlab)
  9. matlab 上三角矩阵变为对称矩阵,已知上/下三角矩阵如何快速将对称阵补全
  10. linux手机拍照翻译软件,拍图识字翻译软件-拍图识字翻译app下载v1.1.3-Linux公社
  11. Android 消息机制之 MessageQueue 消息队列
  12. 给大家推荐一些好用的国内外免费图床(可外链的免费相册)
  13. 揭露SAP培训机构的套路
  14. tds for mysql_PostgreSQL9.3安装tds_fdw扩展
  15. 雨夜深思——开发平台(二)
  16. 单频点单输入功率只含基波X模型的提取与验证
  17. 苹果手机计算机软件不见了怎么办,苹果通讯录怎么不见了?苹果通讯录没了怎么办...
  18. 干货深挖!从写简历,到面试、谈薪酬的那些技巧和防坑指南
  19. U盘文件突然不见却占内存 解决方案
  20. The AudioContext was not allowed to start. It must be resumed after a user gesture on the page

热门文章

  1. 快速搭建LAMP环境
  2. 如何提高程序员的生产率 (1)
  3. 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)...
  4. 最长公共子序列 (nyoj36) [动态规划]
  5. RAID入门一页通,最全的RAID技术、原理图解
  6. 如今黑帽查找引擎优化的难点
  7. centos6+nagios3.3.1+nrpe2.12
  8. C#中sql备份与还原
  9. 桌面虚拟化之应用程序的整合
  10. Glomosim安装和ParseC的使用等相关链接