- 浏览: 922471 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (322)
- Hibernate研究&源码 (27)
- Server (10)
- Coder碎语 (64)
- EnglishMulling (11)
- About XML (1)
- persistence (12)
- Core Java & OO (23)
- Java EE (6)
- JavaScript/JSON/Ajax/ext... (22)
- 我的读书笔记 (16)
- Source Codes Study (29)
- workFlow/jBPM (22)
- OFBiz: Open For Business (1)
- 项目积累 (21)
- srcStudy_acegi (1)
- Cache/Ehcache... (9)
- Java Test/JUnit.. (7)
- maven/ant (2)
- 设计模式 (1)
- SOA/cxf/ws-security (2)
- Android (4)
- 云计算/Hadoop (2)
- 加密/签名 (1)
- 正则表达式 (1)
- htmlparser (1)
- 操作系统 (5)
- DB (1)
最新评论
-
天使建站:
这里这篇文章更详细 还有完整的实例演示:js跳出循环 ...
jQuery中each的break和continue -
heshifk:
刚刚我也遇到同样的问题,然后就在纠结为什么不能直接使用brea ...
jQuery中each的break和continue -
masuweng:
不错写的.
集万千宠爱于一身的SessionImpl:get研究(四): Hibernate源码研究碎得(8) -
muzi131313:
这个老是忘,做一下笔记还是挺好的
jQuery中each的break和continue -
lg068:
data = data.replace("\n&qu ...
项目小经验: eval与回车符
<Java.JavaEE面试整理>(12) 对Collections FrameWork的理解(二)
++++++++++++++++++++++++++++++++++++++++++
真是抱歉,没有把那个<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)(以下简称"理解一")这样核心部分的内容放到频道首页来让更多的朋友看到,而只是把个英文原文放到这里.
应该说"理解一"更为值得关注.
为了答谢大家的支持,鄙人会将这段时间对这个Collection Framework的理解心得整理出来与大家交流.
++++++++++++++++++++++++++++++++++++++++++
这个话题所涉及到的问题很重要,先把英文的原话写在这时在,等过些天有时间了,再结合iterator设计模式好好研究这个问题.
Q. 为什么用Itetator时,会抛出ConcurrentModificationException这样的异常呢?
Why do you get a ConcurrentModificationException when using an iterator? CO
Problem: The java.util Collection classes are fail-fast, which means that if one thread changes a collection while another
thread is traversing it through with an iterator the iterator.hasNext() or iterator.next() call will throw
ConcurrentModificationException. Even the synchronized collection wrapper classes SynchronizedMap and
SynchronizedList are only conditionally thread-safe, which means all individual operations are thread-safe but compound
operations where flow of control depends on the results of previous operations may be subject to threading issues.
Collection<String> myCollection = new ArrayList<String>(10);
myCollection.add("123");
myCollection.add("456");
myCollection.add("789");
for (Iterator it = myCollection.iterator(); it.hasNext();) {
String myObject = (String)it.next();
System.out.println(myObject);
if (someConditionIsTrue) {
myCollection.remove(myObject); //can throw ConcurrentModificationException in single as
//well as multi-thread access situations.
}
}
Solutions 1-3: for multi-thread access situation:
Solution 1: You can convert your list to an array with list.toArray() and iterate on the array. This approach is not
recommended if the list is large.
Solution 2: You can lock the entire list while iterating by wrapping your code within a synchronized block. This approach
adversely affects scalability of your application if it is highly concurrent.
Solution 3: If you are using JDK 1.5 then you can use the ConcurrentHashMap and CopyOnWriteArrayList classes,
which provide much better scalability and the iterator returned by ConcurrentHashMap.iterator() will not throw
ConcurrentModificationException while preserving thread-safety. (这个倒是不错!)
Solution 4: for single-thread access situation:
Use:
it.remove(); // removes the current object via the Iterator “it” which has a reference to
// your underlying collection “myCollection”. Also can use solutions 1-3.
Avoid:
myCollection.remove(myObject); // avoid by-passing the Iterator. When it.next() is called, can throw the exception
// ConcurrentModificationException
Note: If you had used any Object to Relational (OR) mapping frameworks like Hibernate, you may have encountered this
exception “ConcurrentModificationException” when you tried to remove an object from a collection such as a java.util Set
with the intention of deleting that object from the underlying database. This exception is not caused by Hibernate but
rather caused by your java.util.Iterator (i.e. due to your it.next() call). You can use one of the solutions given above.
Q. What is a list iterator?
The java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e.
forward and or backward) and modify the list during iteration.
What are the benefits of the Java Collections Framework? Collections framework provides flexibility, performance,
and robustness.
. Polymorphic algorithms – sorting, shuffling, reversing, binary search etc.
. Set algebra - such as finding subsets, intersections, and unions between objects.
. Performance - collections have much better performance compared to the older Vector and Hashtable classes with
the elimination of synchronization overheads.
. Thread-safety - when synchronization is required, wrapper implementations are provided for temporarily
synchronizing existing collection objects. For J2SE 5.0 use java.util.concurrent package.
. Immutability - when immutability is required wrapper implementations are provided for making a collection
immutable.
. Extensibility - interfaces and abstract classes provide an excellent starting point for adding functionality and
features to create specialized object collections.
Q. What are static factory methods? CO
Some of the above mentioned features like searching, sorting, shuffling, immutability etc are achieved with
java.util.Collections class and java.util.Arrays utility classes. The great majority of these implementations are provided
via static factory methods in a single, non-instantiable (i.e. private constrctor) class. Speaking of static factory
methods, they are an alternative to creating objects through constructors. Unlike constructors, static factory methods are
not required to create a new object (i.e. a duplicate object) each time they are invoked (e.g. immutable instances can be
cached) and also they have a more meaningful names like valueOf, instanceOf, asList etc. For example:
Instead of:
String[] myArray = {"Java", "J2EE", "XML", "JNDI"};
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
You can use:
String[] myArray = {"Java", "J2EE", "XML", "JNDI"};
System.out.println(Arrays.asList(myArray)); //factory method Arrays.asList(…)
For example: The following static factory method (an alternative to a constructor) example converts a boolean primitive
value to a Boolean wrapper object.
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.FALSE)
}
发表评论
-
JavaPersistenceWithHibernate读书笔记(6)--持久层与另外可用替代方案
2008-04-06 17:21 16661.3 Persistence layers and alte ... -
<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)
2008-04-06 13:55 2402Q 16:谈谈你对Java Collectio ... -
写博一月后的收获与反思(1)
2008-04-05 17:26 1649写博一月后的收获与反思开始安下心来写博到现在,细想一下也有一个 ... -
<Java.JavaEE面试整理>(9)--抽象类与接口有什么区别?以及如何选择?
2008-04-03 15:17 1903"abstract class" or & ... -
<Java.JavaEE面试整理>(8) --基于什么考虑,Java 1.4中引入Assert???
2008-04-03 11:44 1412Q 11: What is design by contrac ... -
JavaPersistenceWithHibernate读书笔记(4)
2008-04-03 10:41 21351.2.4 Problems relating to asso ... -
JavaPersistenceWithHibernate读书笔记(3)
2008-04-02 17:25 17531.2.2 The problem of subtypes ... -
<Java.JavaEE面试整理>(7) --Polymorphism之深入理解(二)
2008-04-02 14:31 1497Q. Why would you prefer code re ... -
<Java.JavaEE面试整理>(6) --Polymorphism之深入理解(一)
2008-04-02 12:14 2061Q 10:请谈谈你对多态,继承,封装和动态绑定(dynamic ... -
<Java.JavaEE面试整理>(5)
2008-04-01 08:49 1896Q 09: 请谈谈你对'is a'和'has a'的理解?也就 ... -
JavaPersistenceWithHibernate读书笔记(2)
2008-03-31 19:24 1181JavaPersistenceWithHibernate读书笔 ... -
<Java.JavaEE面试整理>(4)
2008-03-31 18:54 1434Q 06:Java中构造方法与其它常规方法有什么区别?要是你没 ... -
<Java.JavaEE面试整理>(3)
2008-03-30 16:31 1858Q 04: 怎么用Java里的Pack ... -
JavaPersistenceWithHibernate读书笔记(1)
2008-03-30 14:14 1779Part 1: Getting started with Hi ... -
<Java.JavaEE面试整理>(2)
2008-03-29 15:14 1816Java.J2EE.Job.Interview.Compani ...
相关推荐
├<阶段1 java语言基础> │ ├<1-1-Java基础语法> │ ├<1-2 -面向对象和封装> │ └<1-3-Java语言高级> ├<阶段2 JavaWeb·> │ ├<01 HTML和CSS> │ ├<02 JavaScript> │ ├<03 BootStrap> │ ├<04 XML> │ ├...
### IntelliJ IDEA 12 创建 Maven 管理的 JavaEE Web 项目 #### 一、简介 本篇文章将详细介绍如何使用 IntelliJ IDEA 12 来创建一个由 Maven 管理的 JavaEE Web 项目。这不仅适用于初学者了解整个项目创建流程,...
一个变态的jar包,按照我当前的工程环境,应该在weblogic8底下,但就是找不到,所以只好在网上找了,记录在这里。 包含的类: org.apache.cactus.server.ServletTestRedirector
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.19</version> </dependency> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> ...
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring...
02.<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ...
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!-- 默认找的文件名 --> </listener> <!-- Context Configuration locations for Spring XML files --> <context...
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.23</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc...
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web...
JavaEE是企业级应用开发的重要框架,它基于Java平台,为构建分布式、多层的Web应用程序提供了全面的解决方案。这份“javaee面试题集锦”涵盖了广泛的JavaEE相关知识点,对于求职者或开发者来说,是提升技能和准备...
Java和JavaEE技术面试题是求职者准备面试的重要参考资料,涵盖了广泛的Java基础知识、JSP、Servlet、EJB、数据库、设计模式等多个领域。以下是对这些知识点的详细解释: 1. **面向对象的特征**:封装、继承、多态是...
<servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> ...
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.23</version> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> ...
《从Java走向JavaEE》这本书的附带代码资源旨在帮助初学者逐步理解并掌握Java企业级应用开发。Java和JavaEE是两个不同的概念,Java是一种广泛使用的编程语言,而JavaEE(Java Platform, Enterprise Edition)是Java...
JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...
【描述】:这是一份精心整理的JAVA和JAVAEE面试题集锦,包含了我在多次面试过程中遇到的各类问题,旨在为准备面试的朋友提供全面且实用的参考资料。这份文档将帮助你深入了解Java编程语言以及Java Enterprise ...
<name>Java Maven Hello World</name> <dependencies> <!-- 添加必要的依赖,例如Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>...
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.x.x.RELEASE</version> </dependency> <!-- 其他相关依赖 --> </dependencies> ``` 接下来,我们需要配置Struts...
JavaEE面试宝典是每一位Java开发者在求职过程中必备的参考资料,涵盖了从基础知识到高级技术的全方位面试准备。这里,我们将深入探讨其中的关键知识点,帮助你更好地理解和掌握。 首先,我们从基础开始。Java语言的...
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet....