聚合资讯    词典    My blog    Tag   
hi! | 关于
专注于Java技术、开源项目、项目管理

标签 - 分类 : 全部 | UNIX | 随笔 | 数据库 | Java技术 | 网摘文章
关于ConcurrentModificationException异常

在Java API里已经解释了这个ConcurrentModificationException异常的来历:

当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。

例如,某个线程在 Collection 上进行迭代时,通常不允许另一个线程修改该 Collection。通常在这些情况下,迭代的结果是不明确的。如果检测到这种行为,一些迭代器实现(包括 JRE 提供的所有通用 collection 实现)可能选择抛出此异常。执行该操作的迭代器称为快速失败 迭代器,因为迭代器很快就完全失败,而不会冒着在将来某个时间任意发生不确定行为的风险。

注意,此异常不会始终指出对象已经由不同 线程并发修改。如果单线程发出违反对象协定的方法调用序列,则该对象可能抛出此异常。例如,如果线程使用快速失败迭代器在 collection 上迭代时直接修改该 collection,则迭代器将抛出此异常。

注意,迭代器的fail-fast快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。快速失败操作会尽最大努力抛出 ConcurrentModificationException。因此,为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的做法,正确做法是:ConcurrentModificationException 应该仅用于检测 bug。

 

         当使用 fail-fast iterator 对 Collection 或 Map 进行迭代操作过程中尝试直接修改 Collection / Map 的内容时,即使是在单线程下运行,  java.util.ConcurrentModificationException 异常也将被抛出。

   Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭 代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。

  所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。

  有意思的是如果你的 Collection / Map 对象实际只有一个元素的时候, ConcurrentModificationException 异常并不会被抛出。这也就是为什么在 javadoc 里面指出: it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.

下面的代码没有问题:

  1. import java.util.*;  
  2.   
  3. public class TryIteratorRemove {  
  4.     public static void main(String [] args){  
  5.         Collection<String> myCollection = new ArrayList<String>(10);  
  6.           
  7.         myCollection.add("123");  
  8.         myCollection.add("456");  
  9.         myCollection.add("789");  
  10.           
  11.         int i=0;  
  12.           
  13.         for(Iterator it = myCollection.iterator();it.hasNext();) {  
  14.             String myObject = (String)it.next();  
  15.             System.out.println(myObject);  
  16.               
  17.             i++;  
  18.   
  19.             if(i==1){  
  20.                 //myCollection.remove(myObject);  //这行代码有问题,会抛出ConcurrentModificationException
  21.                 it.remove();  
  22.             }  
  23.         }  
  24.   
  25.         System.out.println("After remove,the size of myCollection is: " +  
  26.                        myCollection.size()+" \n and its content is: ");  
  27.   
  28.         for(String s : myCollection){  
  29.             System.out.println(s);  
  30.         }  
  31.     }  


 
标签 : ,

虽然在Quartz上有配置Quartz集群Clustering ,但是在Spring中使用Quartz任务调度并支持集群系统却有些问题,下面介绍解决办法:

标签 : , ,

阅读全文...

FORMATTING DECIMAL NUMBERS
格式化数字,科学计数法
Internet Explorer - User-Agent test and override registry scripts
存储空间不足 无法完成此操作:
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform" /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\Post Platform" /f
reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform" /f
reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\Post Platform" /f
Open Source Native XML Database
The 7 Stages of Scaling Web Apps | High Scalability
【原】Java Reflection 之 Method - pengpenglin - BlogJava
Java反射 Reflection
标签 : ,

阅读全文...

IBM WebSphere Developer Technical Journal: The top Java EE best practices
IBM WebSphere Developer Technical Journal: The top 10 (more or less) J2EE best practices
Top 10 Concepts That Every Software Engineer Should Know - ReadWriteWeb
南方网-广州市人口准入基本条件(二)
南方网-广州市人口准入基本条件(一)
1 Minute HOWTO Guides
ubuntu,fedora,centos,freebsd如何升级安装软件包,设置操作系统语言
Clean up your Web pages with HTML TIDY
weeker.org » Blog Archive » Ubuntu 源列表
squid3.0反向代理 apache+squid_蒜头网
lighttpd+tomcat+squid3.0_蒜头网
高并发高流量网站架构 - 技术门户 | ITPUB |
使用 Nginx 提升网站访问速度
Nginx是一个web 服务器
Java performance tuning tips
Java性能调优
Ana's Lair: crt1.o: No such file: No such file or directory
解决下面的问题: C compiler cannot create executables /usr/bin/ld: crt1.o: No such file: No such file or directory
Linux下./configure错误详解-王琬的BLOG-搜狐博客
解决下面的问题: C compiler cannot create executables /usr/bin/ld: crt1.o: No such file: No such file or directory
每个项目最重要的十件事 - 软件 - JavaEye新闻
MG4J: Managing Gigabytes for Java™
lucene的第二选择,一个搜索引擎
memcached-tool
memcached monitor 监视
十个最好的Java性能故障排除工具 - Java - JavaEye新闻
敏捷质疑: 持续集成 - 切尔斯基 - BlogJava
SpringSource Team Blog » Dynamic DataSource Routing
Spring+Hibernate多数据源解决方案
Horizontal Database Partitioning with Spring and Hibernate - ... - JavaEye技术网站
Spring+Hibernate框架下,多数据源Datasource,多sessionFactory的水平数据库分区分割解决方案
spring之多SessionFactory - Spring - Java - JavaEye论坛
Spring+Hibernate框架下,多数据源Datasource,多sessionFactory的水平数据库分区分割解决方案
Oracle10g学习笔记 | 专注.NET 、JAVA技术
yanfs: yanfs
文件系统,远程访问驱动器
站长日志 - 面向网站站长的专业网络开发、网站运营博客
css 参考
标签 : ,

质检总局建议停止食用以前购买的无法确定是否含三聚氰胺的奶粉

2008年9月21日,质检总局官方网站发布的关于乳制品安全相关问题答复新闻中,第三点《关于查询消费者已购买的婴儿配方奶粉是否合格的问题》指出

打消家长顾虑的最好办法,是停止食用以前购买的奶粉,孩子的健康毕竟比几袋奶粉贵重多了”。

阅读全文...

Tapestry5必须有一个"module builder class",很典型地,经常是叫"AppModule",AppModule经常用来定义一些新服务、覆盖原始服务、或者为服务更改配置。常用的是让Tapestry5支持UTF-8的request Encoding、忽略一些路径过滤来支持Servlet和其他Servlet Filter的开发等。下面的代码可以加在AppModule中:


Tapestry5通过在web.xml下定义tapestry.app-package指向的Java包里寻找AppModule,也就是<filter-name>加上"Module"字符。

下面是官方文档里Tapsetry IoC 配置

Tapestry IoC Configuration

Most other configuration occurs inside your application's module builder class. The application module builder will often define new services, provide overrides of services, or make contributions to service configurations.

Tapestry looks for a module builder class in the services package (under the root package). It capitalizes the <filter-name> and appends "Module". In the previous example, the module builder class would be org.example.myapp.services.AppModule.

If such a class exists, it is added to the IoC Registry. It is not an error for your application to not have a module, though any non-trivial application will have a module.

阅读全文...

“专业软件工程师”,是指具有规范化的价值观和知识体系,规范化的工作习惯和职业纪律,职业化的工作作风和流程,当然也具有相应的技能和经验。

标签 : ,