- 浏览: 58978 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
java.lang.String:
好了,知道了!
垃圾收集器与内存分配策略 -
java.lang.String:
能说一下,什么是新生代,什么是老年代吗?
垃圾收集器与内存分配策略
ConcurrentHashMap
has been pitched as a simple alternative for HashMap
, eliminating the need for a synchronized blocks. I had some simple event counting code that created count records on the fly. Although I could have used synchronized blocks for safety I used ConcurrentHashMap
for this situation, partly for efficiency but mostly for the exercise. Going through this made me realize how carefully ConcurrentHashMap
must be used for your code to work correctly and efficiently.
When using a HashMap
, the standard idiom to add a value if it doesn’t exist is to use code that looks something like this:
synchronized (this) { Record rec = records.get(id); if (rec == null) { rec = new Record(id); records.put(id, rec); } return rec; }
If you were to simply replace HashMap
with ConcurrentHashMap
and remove the synchronized
keyword your code would be exposed to a race condition. If a new Record
was put into the map just after the call to get
returned null the put
operation would overwrite the value. You could addsynchronized
back in but this defeats the purpose of using ConcurrentHashMap
.
To safely create values on demand you must use putIfAbsent
(and avoid making extra calls to get
in the process).
First check to see if a value with the key already exists in the map and use this value if it does. Otherwise, create a new value for the map and add it with putIfAbsent
. putIfAbsent
returns any existing value if there is one, otherwise null (this is why ConcurrentHashMap
can’t contain null values).
private ConcurrentMap<String, Record> records = new ConcurrentHashMap<String, Record>(); private Record getOrCreate(String id) { Record rec = records.get(id); if (rec == null) { // record does not yet exist Record newRec = new Record(id); rec = records.putIfAbsent(id, newRec); if (rec == null) { // put succeeded, use new value rec = newRec; } } return rec; }
If putIfAbsent
does return a value, it’s the one that must be used. It may have already been used by other threads at this point. The new value created must be abandoned. Although it sounds wasteful this case should happen very infrequently.
I’ve seen other code on the net that ignores the return value ofputIfAbsent
and makes another call to get
at the end to figure out which value made it into the map (the new value created or a value from another thread). Although this will work it introduces an unnecessary lookup.
发表评论
-
Attacking memory problems on Android
2012-04-23 22:38 1408Attacking memory problems on ... -
ViewPager and Fragments
2012-03-25 20:11 2274Android: ViewPager and Fragm ... -
Genie Effect AS
2012-03-21 23:13 1032package { import flash.di ... -
Image Processing in Android
2012-03-13 23:44 0While working on WallSwitch, I ... -
How to avoid OutOfMemory Error / OOM (The Ugly Truth Revealed)
2012-03-13 23:38 857ERROR/dalvikvm-heap(4204): 6 ... -
Reduce Bitmap size using BitmapFactory.Options.inSampleSize
2012-03-13 23:32 1110In the post "Load ImageVie ... -
解決在 Android 中使用 Bitmap 造成 Out of memory 的方法~
2012-03-13 23:30 1020因為Android本身的限制,在2.x版中使用 Bitm ... -
android BitmapFactory的OutOfMemoryError: bitmap size exceeds VM budget解决方案
2012-03-13 23:28 773使用android提供的BitmapFactory解码一 ... -
About Bitmap allocated
2012-03-13 23:08 1101Bitmap data is not allocated ... -
The Java Thread Pool
2012-03-03 00:55 1269A thread pool helps to const ... -
Multithreading For Performance
2012-03-02 23:23 949A good practice in creating ... -
Android Animations 3D flip
2012-03-02 23:24 1497In this post we are going to ... -
SQLITE Best practice
2012-02-15 23:02 942Recently I’ve been doing qui ... -
NDK Findclass
2012-02-09 23:24 2922I’m a big fan of the new nat ... -
UPnP中Control Point的基本流程
2012-01-10 22:43 48591.搜索目前网络上的UPnP设备。Control Poi ... -
浅析UPNP协议
2012-01-10 22:13 2151摘要:文章介绍了UPnP结 ... -
DLNA OPEN SOURCE
2012-01-04 22:55 2356DLNA Open Source Projects ... -
Linux DLNA
2012-01-04 22:40 2815Linux DLNA 2011-08-22 黑日白月 ... -
EffectiveJava 读书笔记2 对于所有对象都通用的方法
2011-11-21 17:37 0第8条:覆盖equals时请遵守通用约定 防止覆盖equal ... -
EffectiveJava 读书笔记1 创建和销毁对象
2011-11-20 11:50 934第1条:考虑用静态工厂 ...
相关推荐
java源码剖析-ConcurrentHashMap
ConcurrentHashMap源码剖析
Java利用ConcurrentHashMap实现本地缓存demo; 基本功能有缓存有效期、缓存最大数、缓存存入记录、清理线程、过期算法删除缓存、LRU算法删除、获取缓存值等功能。 复制到本地项目的时候,记得改包路径哦~
源码分析见我博文:http://blog.csdn.net/wabiaozia/article/details/50684556
ConcurrentHashMap可以做到读取数据不加锁,并且其内部的结构可以让其在进行写操作的时候能够将锁的粒度保持地尽量地小,允许多个修改操作并发进行,其关键在于使用了锁分离技术。它使用了多个锁来控制对hash表的...
ConcurrentHashMap具体是怎么实现线程安全的呢,肯定不可能是每个方法加synchronized,那样就变成了HashTable。
解析concurrenthashmap的源码,学习多线程的思想
这时候ConcurrentHashMap达到容量扩容而忽略了ReservationNode情况,调用put的时候在synchronized(f)没有对ReservationNode处理,所以会出现死循环。 在jdk1.8和1.9中对比 http://gee.cs.oswego.edu/cgi- bin/...
程序员面试加薪必备_ConcurrentHashMap底层原理与源码分析深入详解
java本地缓存ConcurrentHashMap
ConcurrentHashMap是J.U.C(java.util.concurrent包)的重要成员,它是HashMap的一个线程安全的、支持高效并发的版本。在默认理想状态下,ConcurrentHashMap可以支持16个线程执行并发写操作及任意数量线程的读操作。...
ConcurrentHashMap之实现细节
concurrenthashmap1.7的源码分析
Java——并发容器之ConcurrentHashMap;Java——并发容器之ConcurrentHashMap;Java——并发容器之ConcurrentHashMap;Java——并发容器之ConcurrentHashMap;Java——并发容器之ConcurrentHashMap;Java——并发...
java7-8中的 HashMap和ConcurrentHashMap全解析
ConcurrentHashMap的实现原理(纯干货)
Java并发编程之ConcurrentHashMap Java并发编程之ConcurrentHashMap.pdf
ConcurrentHashMap是一个线程安全的HashTable,它的主要功能是提供了一组和HashTable功能相同但是线程安全的方法。ConcurrentHashMap可以做到读取数据不加锁,并且其内部的结构可以让其在进行写操作的时候能够将锁的...
ConcurrentHashMap的实现原理(JDK1.7和JDK1.8),并说明了在jdk1.7与jdk1.8的不同实现原理
HashMap& ConcurrentHashMap 深度解析