之前项目中,有用到过Collections.synchronizedMap(),后面发现当并发数很多的时候,出现其他请求等待情况,因为synchronizedMap会锁住所有的资源,后面通过查阅资料,发现了ConcurrentHashMap ,可以比较完美

的解决这个问题,简单来说,ConcurrentHashMap 提高性能的方式是对资源进行hash 分块,一个快一把锁,这样就提高了读取和写入的效率,后面看了下api,在 ConcurrentHashMap 下,还有ConcurrentSkipListMap。

由于项目时间问题,当时没有研究,乘着现在空闲之余,先简单说下 ConcurrentHashMap 与Collections.synchronizedMap()的不同,及其为什么还要有ConcurrentSkipListMap。

另外就是ConcurrentHashMap 与hashtable的区别(如果有简单看下ConcurrentHashMap 的实现机制,就知道两者最本质的区别了)

http://www.pixelstech.net/article/index.php?id=1394026282

ConcurrentHashMap and Collections.synchronizedMap() both provide thread-safe operations of collections of data. They are used in multithreaded programs to provide both thread safety and performance improvements. In many cases, we can use either of them.

But the realization of thread safety is different for these two implementations. ConcurrentHashMap will create an HashEntry[] array internally to store the elements passed in from a Map, while Collections.synchronizedMap() will return a SynchronizedMap.

The main difference between these two is that ConcurrentHashMap will lock only portion of the data which are being updated while other portion of data can be accessed by other threads. However, Collections.synchronizedMap() will lock all the data while updating, other threads can only access the data when the lock is released. If there are many update operations and relative small amount of read operations, you should choose ConcurrentHashMap.

Also one other difference is that ConcurrentHashMap will not preserve the order of elements in the Map passed in. It is similar to HashMap when storing data. There is no guarantee that the element order is preserved. While Collections.synchronizedMap(0 will preserve the elements order of the Map passed in. For example, if you pass a TreeMap to ConcurrentHashMap, the elements order in the ConcurrentHashMap may not be the same as the order in the TreeMap, but Collections.synchronizedMap() will preserve the order.

Furthermore, ConcurrentHashMap can guarantee that there is no ConcurrentModificationException thrown while one thread is updating the map and another thread is traversing the iterator obtained from the map. However, Collections.synchronizedMap() is not guaranteed on this. If we obtain an Iterator from Collections.synchronizedMap() by calling map.keySet().iterator() and then traverse the iterator, at the same time if another thread is trying to updating the map by calling map.put(K,V), we will get a ConcurrentModificationException.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Map<String,String> map = Collections.synchronizedMap(new TreeMap<String,String>());
 
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
 
Set<Entry<String,String>> entries = map.entrySet();
 
Iterator<Entry<String,String>> iter = entries.iterator();
 
while(iter.hasNext()){
    System.out.println(iter.next()); //Will throw ConcurrentModificationException
    map.remove("key2"); 
}

Now I am wondering whether there is one object which can preserve the insertion order of elements like Collections.synchronizedMap() and also doesn't throw ConcurrentModificationException like ConcurrentHashMap. Fortunately since 1.6 there is a class called ConcurrentSkipListMap which can fulfill these two requirements, from the documentation, we can find that ConcurrentSkipListMap will not throw ConcurrentModificationException and also it will preserve the insertion order of the Map passed in. The only drawback it may have is its performance.

You can also check the difference between ConcurrentHashMap and Hashtable for more understanding about ConcurrentHashMap.

http://www.pixelstech.net/article/index.php?id=1384783590

Both ConcurrentHashMap and Hashtable are Collection classes for storing key value pairs and they both provide fast element search with a supplied key. They have much in common. However, we will not discuss the similarities between them here, instead we will focus on the differences between them.

ConcurrentHashMap and Hashtable are both thread safe. But the mechanism for thread safe is different between them. Hashtable is synchronized, it utilizes the synchronization mechanism; while ConcurrentHashMap uses segmentation to lock the data, it uses concurrent locks operations for thread safety instead of synchronized.

Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.

So if you want to store large amount of data in a multithreaded program, you should consider to choose ConcurrentHashMap.

有链接,英文也比较简单,先mark到这里把,有时间在整理成中文

最新文章

  1. Android pop3与imap方式接收邮件(javamail)
  2. 【GIT】Github上传本地代码详解
  3. mac下 ssh免密码登陆设置
  4. it精英的艰辛路程
  5. WCF入门教程三[WCF的宿主]
  6. 使用curl操作openstack swift
  7. 转:找不到include xgpio.h;Unresolved include xgpio.h
  8. maven私服nexus搭建(windows)
  9. 如何使用MVP+Dagger2+RxJava+Retrofit开发(1)
  10. 怎么修改无法启动的docker容器的配置?
  11. ES6的Map如何遍历
  12. 团队作业4——beta冲刺
  13. 基于Redisson实现分布式锁
  14. Eclipse Mars-Ant无法使用jre1.6的问题
  15. Intellij Idea出现 unable to establish loopback connection
  16. 文本编辑工具(sublime text 2)
  17. 生成html报告并整合自动发动邮件功能
  18. linux rabbitmq 安装
  19. ibus拼音安装_ubuntu10.04
  20. 【Maven】Snapshot和Release版本的区别

热门文章

  1. [Algorithm] Binary tree: Level Order Traversal
  2. 数据结构 - 2-路插入排序 具体解释 及 代码(C++)
  3. Google想出了一个决定人员晋升的算法,然后就没有然后了......
  4. thick置备和 thin置备,克隆,模板和快照
  5. 1049: 贝贝的车牌问题(car)
  6. sqlalchemy 获取计数 count
  7. SQL Server中获取最新插入的自增ID
  8. Maven构建应用程序常用配置(转)
  9. Tomcat访问日志浅析 (转)
  10. HTML5开发之获取设备的地理坐标