【Developer Log】Thread-safe
2016-04-01 14:29
316 查看
在web中采用多线程,多线程读写某个Collection,就会涉及thread-safe的问题,如果collection在操作的使用采用synchronize,那么这就是线程安全,但这是要付出性能代价的,也可以不适用thread-safe,但需要根据需求自己考虑是否加上同步。
在http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/中有对何为线程安全有详细的解析,增删entry是就涉及线程安全,仅仅修改key所对应的value不属于考虑范围(这可能和原子操作有关,无需担心)。
What does the thread safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally to avoid an inconsistent
view of the contents. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)
对于List,Vector是线程安全,但是ArrayList不是。在javadoc中是这样描述Vector:
As of the Java 2 platform v1.2, this class was retrofitted to implement theList interface, making it a member of theJava Collections Framework. Unlike the new collection implementations,
is synchronized. If a thread-safe implementation is not needed, it is recommended to useArrayList in place of
对于Map,Hashtable是线程安全的,但是HashMap不是。但是使用古老的Hashtable有时性能也真不好,而且Hashtable不允许null作为值,而HashMap允许。我们可以通过下面两种方式来实现。例如对于替代
Map<String, Integer> threadSafeMap = new Hashtable<String, Integer>();
方式1:采用Collections.synchronizedMap
方式2:采用在Java 1.5引入的ConcurrentHashMap
根据http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/的测试数据,ConcurrentHashMap的效率更高。因此,在我们需要thread-safe的使用,应该尽可能地使用ConcurrentHashMap。
相关链接:开发日志
在http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/中有对何为线程安全有详细的解析,增删entry是就涉及线程安全,仅仅修改key所对应的value不属于考虑范围(这可能和原子操作有关,无需担心)。
What does the thread safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally to avoid an inconsistent
view of the contents. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)
对于List,Vector是线程安全,但是ArrayList不是。在javadoc中是这样描述Vector:
As of the Java 2 platform v1.2, this class was retrofitted to implement theList interface, making it a member of theJava Collections Framework. Unlike the new collection implementations,
Vector
is synchronized. If a thread-safe implementation is not needed, it is recommended to useArrayList in place of
Vector.
对于Map,Hashtable是线程安全的,但是HashMap不是。但是使用古老的Hashtable有时性能也真不好,而且Hashtable不允许null作为值,而HashMap允许。我们可以通过下面两种方式来实现。例如对于替代
Map<String, Integer> threadSafeMap = new Hashtable<String, Integer>();
方式1:采用Collections.synchronizedMap
Map<String,Integer>threadSafeMap = Collections.synchronizedMap(new HashMap<String, Integer>());
方式2:采用在Java 1.5引入的ConcurrentHashMap
Map<String,Integer> threadSafeMap = new ConcurrentHashMap<String, Integer>();
根据http://www.asjava.com/core-java/thread-safe-hash-map-in-java-and-their-performance-benchmark/的测试数据,ConcurrentHashMap的效率更高。因此,在我们需要thread-safe的使用,应该尽可能地使用ConcurrentHashMap。
相关链接:开发日志
相关文章推荐
- js生成二维码实例(真实有效)
- 通过js对cookie的使用手册
- HTML —— HTML教程|HTML手册|HTML基础|HTML语法
- JSON的解析以及创建
- 火狐浏览器angular跳转layer弹窗兼容问题
- fis3前端工程构建工具使用小结
- JavaScript常用对象Array(1)
- javaScript生成32位随机数
- javascript笔记——闭包
- mac和window下的html开发工具
- HTML5<meta name="viewport"/>标签常见属性及说明
- 【H5适配二】Meta标签中的format-detection属性及含义
- extjs两行tbar的问题
- css3基本特效
- HTML学习(七)——框架
- Dynamics CRM2016 Web API之Use custom FetchXML
- Dynamics CRM2016 Web API之Use custom FetchXML
- Dynamics CRM2016 Web API之Use custom FetchXML
- Dynamics CRM2016 Web API之Use custom FetchXML
- HTML <a> 标签