- 浏览: 143754 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
august_000:
很有道理,我已经亲自测试过了:
public class ...
单例模式之线程安全解析 -
Chris_bing:
一个单例有这么多名堂,最后那个内部类的解决方案很有创意啊,受教 ...
单例模式之线程安全解析
原文地址:http://www.cnblogs.com/sunwufan/archive/2012/05/21/2511747.html
当考虑到列表中需要修改的元素很多时,可以考虑上一篇博文:
通过CopyOnWriteArrayList,在遍历过程中,对list元素进行删除和添加操作
方法一(效率不高):
这个问题是说,你不能在对一个List进行遍历的时候将其中的元素删除掉 解决办法是,你可以先将要删除的元素用另一个list装起来,等遍历结束再remove掉 可以这样写
List delList = new ArrayList();//用来装需要删除的元素
for(Information ia:list)
if(ia.getId()==k){
n++;
delList.add(ia);
}
list.removeAll(delList);//遍历完成后执行删除
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
方法二:同步操作list时,一边迭代remove,一边新增.那么会报错 java.util.ConcurrentModificationException
查看api发现vector有个好方法可以解决这个错误.
首先是用arraylist
// private Vector list;//两种list方式
private List list;
public void init(){
// list = new Vector();
list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
}
public void removeIt(){//iterator
Iterator it = list.iterator();
for(int i=0;it.hasNext();i++){
String a = (String)it.next();
System.out.println(a);
if(a.equals("c")){
//list.remove(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
it.remove();
System.out.println("remove c");
}
}
}
// public void removeEm(){//enumeration
// Enumeration e = list.elements();
// for(int i=0;e.hasMoreElements();i++){
// String a = (String)e.nextElement();
// System.out.println(a);
// if(a.equals("c")){
// try {
// Thread.sleep(1000);
// } catch (InterruptedException ee) {
// }
// list.remove(a);
// System.out.println("remove c");
// }
// }
// }
public void add(){//先用迭代,再添加
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
list.add("c");
System.out.println("add c");
}
public void run() {
removeIt();
// removeEm();
}
public static void main(String[] args) {
TestConcurrentModificationException t = new TestConcurrentModificationException();
t.init();
t.start();
t.add();
}
运行结果:
a
b
c
add c
Exception in thread "Thread-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at test.TestConcurrentModificationException.removeIt(TestConcurrentModificationException.java:33)
at test.TestConcurrentModificationException.run(TestConcurrentModificationException.java:69)
其次是vector
private Vector list;//两种list方式
// private List list;
public void init(){
list = new Vector();
// list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
}
public void removeIt(){//iterator
Iterator it = list.iterator();
for(int i=0;it.hasNext();i++){
String a = (String)it.next();
System.out.println(a);
if(a.equals("c")){
//list.remove(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
it.remove();
System.out.println("remove c");
}
}
}
public void removeEm(){//enumeration
Enumeration e = list.elements();
for(int i=0;e.hasMoreElements();i++){
String a = (String)e.nextElement();
System.out.println(a);
if(a.equals("c")){
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
}
list.remove(a);
System.out.println("remove c");
}
}
}
public void add(){//先用迭代,再添加
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
list.add("c");
System.out.println("add c");
}
public void run() {
// removeIt();
removeEm();
}
public static void main(String[] args) {
TestConcurrentModificationException t = new TestConcurrentModificationException();
t.init();
t.start();
t.add();
}
运行结果:
a
b
c
add c
remove c
e
f
c
remove c
api上说,
由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是快速失败的:如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代 器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生 不确定行为的风险。Vector 的 elements 方法返回的 Enumeration 不是 快速失败的。
注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测 bug。
当然如果使用其他的集合,那就没有什么好办法了.只有存放起来,每次取一个list对象,并不是实时.
当考虑到列表中需要修改的元素很多时,可以考虑上一篇博文:
通过CopyOnWriteArrayList,在遍历过程中,对list元素进行删除和添加操作
方法一(效率不高):
这个问题是说,你不能在对一个List进行遍历的时候将其中的元素删除掉 解决办法是,你可以先将要删除的元素用另一个list装起来,等遍历结束再remove掉 可以这样写
List delList = new ArrayList();//用来装需要删除的元素
for(Information ia:list)
if(ia.getId()==k){
n++;
delList.add(ia);
}
list.removeAll(delList);//遍历完成后执行删除
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
方法二:同步操作list时,一边迭代remove,一边新增.那么会报错 java.util.ConcurrentModificationException
查看api发现vector有个好方法可以解决这个错误.
首先是用arraylist
// private Vector list;//两种list方式
private List list;
public void init(){
// list = new Vector();
list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
}
public void removeIt(){//iterator
Iterator it = list.iterator();
for(int i=0;it.hasNext();i++){
String a = (String)it.next();
System.out.println(a);
if(a.equals("c")){
//list.remove(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
it.remove();
System.out.println("remove c");
}
}
}
// public void removeEm(){//enumeration
// Enumeration e = list.elements();
// for(int i=0;e.hasMoreElements();i++){
// String a = (String)e.nextElement();
// System.out.println(a);
// if(a.equals("c")){
// try {
// Thread.sleep(1000);
// } catch (InterruptedException ee) {
// }
// list.remove(a);
// System.out.println("remove c");
// }
// }
// }
public void add(){//先用迭代,再添加
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
list.add("c");
System.out.println("add c");
}
public void run() {
removeIt();
// removeEm();
}
public static void main(String[] args) {
TestConcurrentModificationException t = new TestConcurrentModificationException();
t.init();
t.start();
t.add();
}
运行结果:
a
b
c
add c
Exception in thread "Thread-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at test.TestConcurrentModificationException.removeIt(TestConcurrentModificationException.java:33)
at test.TestConcurrentModificationException.run(TestConcurrentModificationException.java:69)
其次是vector
private Vector list;//两种list方式
// private List list;
public void init(){
list = new Vector();
// list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
}
public void removeIt(){//iterator
Iterator it = list.iterator();
for(int i=0;it.hasNext();i++){
String a = (String)it.next();
System.out.println(a);
if(a.equals("c")){
//list.remove(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
it.remove();
System.out.println("remove c");
}
}
}
public void removeEm(){//enumeration
Enumeration e = list.elements();
for(int i=0;e.hasMoreElements();i++){
String a = (String)e.nextElement();
System.out.println(a);
if(a.equals("c")){
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
}
list.remove(a);
System.out.println("remove c");
}
}
}
public void add(){//先用迭代,再添加
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
list.add("c");
System.out.println("add c");
}
public void run() {
// removeIt();
removeEm();
}
public static void main(String[] args) {
TestConcurrentModificationException t = new TestConcurrentModificationException();
t.init();
t.start();
t.add();
}
运行结果:
a
b
c
add c
remove c
e
f
c
remove c
api上说,
由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是快速失败的:如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代 器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生 不确定行为的风险。Vector 的 elements 方法返回的 Enumeration 不是 快速失败的。
注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测 bug。
当然如果使用其他的集合,那就没有什么好办法了.只有存放起来,每次取一个list对象,并不是实时.
发表评论
-
(转)Spring声明式事务配置详解
2013-11-11 16:45 1778Spring声明式事务功能应该是大家应用Spring中使用的最 ... -
(转)Java几款性能分析工具的对比
2013-11-01 14:26 971来源:http://www.javaweb.cc/langua ... -
HashMap的遍历
2013-11-01 14:15 8181、新建一个HashMap Map<Integer , ... -
java将汉子转换成汉语拼音
2013-11-01 13:51 894import net.sourceforge.pinyin4j ... -
Spring加载properties文件的两种方式
2013-10-31 10:05 0在spring中可以通过下面的方式将配置文件中的项注入到配置中 ... -
动态加载spring .xml中的bean对象
2013-10-30 15:27 0在java中如何取得spring.xml中加载的bean对象? ... -
(转)Spring mvc+hibernate+freemarker(实战)
2013-10-28 19:40 913http://zz563143188.iteye.com/bl ... -
(转)Java之美[从菜鸟到高手演变]系列之博文阅读导航
2013-10-28 17:00 1656Java之美[从菜鸟到高手演变]系列之博文阅读导航 http: ... -
(转)openssl 制作证书和签名java方法
2013-10-28 15:03 0Win32OpenSSL_Light-0_9_8k.exe ... -
(转)面向接口编程详解
2013-10-25 12:34 5老文章,自己学习。 面向接口编程详解(一) http://w ... -
(转)Java架构师之路:JAVA程序员必看的15本书
2013-10-08 18:24 0作为Java程序员来说,最 ... -
Java获取CPU ID和磁盘ID
2013-09-25 20:18 0原文链接:http://www.rgagnon.com/jav ... -
Java编程之备用程序段
2013-09-25 14:57 0记录一些程序段,供日常备用,持续增加中…… 1. Java中 ... -
(转)Java编程之AOP
2013-09-25 14:45 0链接: http://hw19886200.iteye.com ... -
Java编程之性能优化
2013-09-25 14:28 727最近的机器内存又爆满 ... -
使用axis2传输附件
2013-09-25 10:40 1005服务端Service文件: import java.io. ... -
Java应用程序项目打包成exe文件
2013-09-23 09:16 01.程序打包 首先将程序打成Jar包,打包命 ... -
(转)Java事务处理类型及差异总结
2013-09-18 13:54 1567一、什么是Java事务 ... -
JAVA文件_加密
2013-09-13 16:09 0import java.awt.*; import java ... -
(转)Java 实现Rsa 加密
2013-09-13 16:08 0import java.io.*; public class ...
相关推荐
java.util.ConcurrentModificationException 异常问题详解1
主要介绍了出现java.util.ConcurrentModificationException 问题及解决办法的相关资料,需要的朋友可以参考下
axis1.4补丁包,解决jdk1.8高并发报ConcurrentModificationException问题,该jar包重新编译jar包的一个class文件,线上环境通过
java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: ...例如以下程序(转
NULL 博文链接:https://chenlinbo.iteye.com/blog/832335
今天小编就为大家分享一篇关于Java源码解析ArrayList及ConcurrentModificationException,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
基本涵盖Axis 1.4 开发所需要的jar包
Map在遍历时候通常 现获得其键值的集合Set,然后用迭代器Iterator来对Map进行遍历。
Spring数据mongodb测试 在Collections.synchronizedList或Collections.synchronizedSet上测试spring数据mongodb ConcurrentModificationException
axis1.4 spring3.0 集成 实现 web service 服务端, axis1.4 客户端认证,授权,访问日志记录,集成spring 解决 PHP 调用web service 无法认证,和解析soap 模板
fastJson的全部资料,包括源码、开发需要用到的jar包和html格式的文档。
这里面包含了大部分的软件测试的专业术语,希望对你有用
java.util.ConcurrentModificationException: mutation occurred during iteration [error] scala.collection.mutable.MutationTracker$.checkMutations(MutationTracker.scala:43) [error] scala.collection....
Iterator遍历中 ConcurrentModificationException异常
想必大家都有参加过面试的或多或少的经历吧,不知道考官都会问啥米刁钻古怪的问题呢?下面的考题是我搜集的一些针对java的面试题目,还不少,大概分为[基础类][应用类][其他类],仅供参考,大家还可以说说自己的面试...
Axis1.4快速发布服务以及客服端详解(资源中的axis是axis1.4自带的,lib也是它自带的)
Gradle 4.8.1 已发布,主要解决了以下 6 个问题: 1、 Maven Central 不再支持旧版 TLS 实现。如果 Gradle 在 JDK 7 上运行,会使依赖关系解析失败。 2、Gradle 4.8 破坏了 artifactory 和 bintray 插件的兼容性。 ...
避免在循环中修改集合,可能导致ConcurrentModificationException。 异常处理: 不要忽视异常,合理捕获并处理它们。 不要过度使用try-catch,应尽量抛出业务异常给上层处理。 使用finally块进行资源清理。 并发...