- 浏览: 935382 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (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 17071.3 Persistence layers and alte ... -
<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)
2008-04-06 13:55 2447Q 16:谈谈你对Java Collectio ... -
写博一月后的收获与反思(1)
2008-04-05 17:26 1680写博一月后的收获与反思开始安下心来写博到现在,细想一下也有一个 ... -
<Java.JavaEE面试整理>(9)--抽象类与接口有什么区别?以及如何选择?
2008-04-03 15:17 1933"abstract class" or & ... -
<Java.JavaEE面试整理>(8) --基于什么考虑,Java 1.4中引入Assert???
2008-04-03 11:44 1452Q 11: What is design by contrac ... -
JavaPersistenceWithHibernate读书笔记(4)
2008-04-03 10:41 21631.2.4 Problems relating to asso ... -
JavaPersistenceWithHibernate读书笔记(3)
2008-04-02 17:25 17731.2.2 The problem of subtypes ... -
<Java.JavaEE面试整理>(7) --Polymorphism之深入理解(二)
2008-04-02 14:31 1536Q. Why would you prefer code re ... -
<Java.JavaEE面试整理>(6) --Polymorphism之深入理解(一)
2008-04-02 12:14 2089Q 10:请谈谈你对多态,继承,封装和动态绑定(dynamic ... -
<Java.JavaEE面试整理>(5)
2008-04-01 08:49 1931Q 09: 请谈谈你对'is a'和'has a'的理解?也就 ... -
JavaPersistenceWithHibernate读书笔记(2)
2008-03-31 19:24 1208JavaPersistenceWithHibernate读书笔 ... -
<Java.JavaEE面试整理>(4)
2008-03-31 18:54 1457Q 06:Java中构造方法与其它常规方法有什么区别?要是你没 ... -
<Java.JavaEE面试整理>(3)
2008-03-30 16:31 1894Q 04: 怎么用Java里的Pack ... -
JavaPersistenceWithHibernate读书笔记(1)
2008-03-30 14:14 1811Part 1: Getting started with Hi ... -
<Java.JavaEE面试整理>(2)
2008-03-29 15:14 1845Java.J2EE.Job.Interview.Compani ...
相关推荐
在本段内容中,涉及到JavaEE面试中常见的知识点,包括Java Web技术、集合框架、异常处理、多线程、设计模式、数据库操作等方面。以下是详细的知识点梳理: Servlet生命周期:Servlet的生命周期包括初始化(init)、...
**Java高级工程师岗位笔试题目** **一、选择题(每题2分,共20分)** 1. 下列哪个类是所有Java类的父类(除了Object类本身),即使...4. 下列哪项是Java中的集合框架(Collections Framework)的一部分,用于存储键值
cmd-bat-批处理-脚本-Progress bar 1.zip
该资源是小红书 2024 年度Java 编程开发面试题,内容非常详细,适合应届毕业生和准备寻求更高发展的Java工程师,希望给你们带来帮助。
内容概要:本文详细介绍了基于RISC-V指令集的五级流水线CPU设计及其验证过程。首先,文章阐述了RISC-V指令集的特点及其在CPU设计中的优势,接着深入解析了每个流水线阶段(取指、解码、执行、访存、写回)的Verilog源代码实现。此外,提供了汇编验证代码用于测试CPU的功能,并附带详细的说明文档和PPT,确保设计的完整性和易理解性。最后,在Vivado平台上进行了全面的仿真和实际硬件测试,验证了设计的正确性和性能。 适合人群:从事嵌入式系统设计、CPU架构研究及相关领域的工程师和技术人员。 使用场景及目标:①理解和掌握RISC-V指令集在五级流水线CPU设计中的应用;②学习Verilog语言在CPU硬件设计中的具体实现方法;③通过汇编验证代码测试CPU功能,确保设计的可靠性。 其他说明:本文不仅提供了完整的Verilog源代码和汇编验证代码,还包括详细的说明文档和PPT,有助于读者更好地理解和实践CPU设计过程。
本程序实现了51单片机与手机之间的字符及数字通信功能,且代码中配有详尽的注释说明。关于通信原理的详细阐述,可在我的其他相关文章中查阅。
cmd-bat-批处理-脚本-run dialogue.zip
内容概要:本文详细介绍了多智能体编队技术,特别是针对4智能体和8智能体的点对点转换分布式模型预测控制。首先概述了多智能体编队的概念及其广泛应用,如无人驾驶、无人机编队等。接着深入探讨了分布式模型预测控制的方法论,强调每个智能体依据自身模型和邻近智能体信息进行预测并制定控制策略,从而提升系统灵活性和鲁棒性。随后阐述了点对点转换的具体机制,即智能体间通过高效的信息交换实现状态间的平滑过渡。最后展示了简化的Python代码示例来解释这一过程,并提供了相关领域的权威参考文献。 适合人群:对多智能体系统、分布式控制系统感兴趣的科研人员和技术开发者。 使用场景及目标:适用于希望深入了解多智能体编队控制理论的研究者以及从事无人驾驶、无人机编队等相关项目的技术人员。目标在于掌握分布式模型预测控制的基本原理及其在实际工程中的应用。 其他说明:文中提供的代码仅为概念验证性质,实际部署时还需考虑更多因素如网络延迟、数据同步等。此外,附带的参考文献为读者进一步学习提供了宝贵的资料来源。
2023年系统分析师真题及解析
IMG_20250521_201207.jpg
内容概要:本文探讨了利用鲸鱼算法(Whale Optimization Algorithm)对光伏和风电项目的选址和定容进行优化的方法。鲸鱼算法是一种新颖的智能算法,它模仿座头鲸的捕食行为,具有较少的参数调整需求和强大的寻优能力。文中详细介绍了该算法的核心机制,如气泡网攻击策略,并展示了如何将其应用于新能源项目的选址定容问题中。具体来说,通过定义合适的目标函数来衡量不同方案的表现,包括网损、节点电压偏差和投资成本等因素。此外,还讨论了如何通过调整权重系数来平衡各个目标之间的关系,从而获得最佳解决方案。最终,通过对实验结果的分析,证明了鲸鱼算法在处理此类多维度优化问题上的优越性能。 适合人群:从事新能源规划、电力系统工程及相关领域的研究人员和技术人员。 使用场景及目标:适用于需要对光伏和风电项目进行科学合理的选址和定容决策的情境下,旨在提高能源利用效率的同时降低成本,确保电网稳定性和可靠性。 其他说明:文中提供了具体的Python代码示例,帮助读者更好地理解和实现鲸鱼算法的应用。同时强调了在实际操作过程中应注意的一些关键因素,如数据预处理方法的选择以及参数设置的影响等。
内容概要:本文详细介绍了威纶通标准精美模板,一套专为A2触摸屏程序开发提供的可直接套用的界面模板。模板涵盖了多个实用功能界面,如配方管理、报警记录、操作记录、登录、设备使用说明、参数设置、系统设置、权限设置、趋势显示、电机设置、IO监控、工位用时、文档设置和维修界面。每个界面均经过精心设计,确保界面清新整洁,不带复杂的宏指令,便于操作和维护。此外,模板还支持XY曲线、树状图、数据统计等功能,能够灵活配置和调用。这套模板不仅适用于快速开发,也为新手和在校生提供了宝贵的学习资源。 适用人群:工业自动化领域的开发人员、工程师、新手和在校学生。 使用场景及目标:① 开发人员可以通过直接套用或复制模板,快速完成A2触摸屏程序开发;② 新手和在校生可以利用模板学习触摸屏程序的设计和实现,掌握工业自动化领域的关键技能。 其他说明:模板中的功能和界面设计充分考虑了工业自动化的需求,确保了系统的稳定性和实用性。
一种三元锂电池析锂特性以及检测方法研究.zip
大规模无线传感 器网络中稀疏信号的数据收集策略.pdf
cmd-bat-批处理-脚本-One_Click_StockPrice.zip
cmd-bat-批处理-脚本-installed-package-contents.zip
2025年网络媒体项目解决方案.docx
该数据集为2010-2023年中国A股上市公司管理层情感语调的年度面板数据,覆盖45,320条样本,数据源自年报及半年报的"管理层讨论与分析"部分。通过构建中文金融情感词典(融合《知网情感分析用词典》与L&M金融词汇表),采用文本分析方法计算情感语调指标,包括:正面/负面词汇数量、文本相似度、情感语调1((积极词-消极词)/总词数)和情感语调2((积极词-消极词)/(积极词+消极词))。同时包含盈利预测偏差、审计意见类型等衍生指标,可用于研究信息披露质量、市场反应及代理问题。该数据复刻了《管理世界》《财经研究》等期刊的变量构建方法,被应用于分析语调操纵对债券市场的影响,学术常用度与稀缺度较高。
cmd-bat-批处理-脚本-green.zip
数据文档 背景描述 心脏病是全球主要的健康威胁之一,也是导致死亡的主要原因。及早识别心脏病风险因素和预测可能的心脏问题对于预防和治疗至关重要。该数据集收集了与心脏健康相关的多种生理指标和实验室检查结果,旨在帮助开发能够区分心脏病阳性和阴性患者的预测模型。 通过分析这些数据,医疗专业人员和研究人员可以更好地理解不同因素(如年龄、性别、血压、血糖和心肌标志物)对心脏病发展的影响,从而制定更精准的诊断和治疗方案。 数据说明 字段 说明 Age 患者年龄 Gender 性别(1=男性,0=女性) Heart rate 心率(每分钟心跳次数) Systolic blood pressure 收缩压(毫米汞柱) Diastolic blood pressure 舒张压(毫米汞柱) Blood sugar 血糖水平(毫克/分升) CK-MB 肌酸激酶同工酶水平(心肌损伤标志物) Troponin 肌钙蛋白水平(心肌损伤特异性标志物) Result 诊断结果(positive=患有心脏病,negative=未患心脏病) 问题描述 该数据集适用于多种分析和预测场景,可以帮助解决以下问题: 心脏病风险预测: 基于生理指标和生化标志物预测个体患心脏病的风险。 关键指标识别: 确定对心脏病诊断最有预测价值的生理和生化指标。 人口统计学分析: 研究年龄和性别与心脏病发生率之间的