之前项目中,有用到过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到这里把,有时间在整理成中文

ConcurrentHashMap vs Collections.synchronizedMap()不同的更多相关文章

  1. HashTable、HashMap、ConcurrentHashMap、Collections.synchronizedMap()区别

    Collections.synchronizedMap()和Hashtable一样,实现上在调用map所有方法时,都对整个map进行同步,而ConcurrentHashMap的实现却更加精细,它对Ha ...

  2. What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

    来自:http://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-coll ...

  3. 测试HashTable、Collections.synchronizedMap和ConcurrentHashMap的性能

        对于map的并发操作有HashTable.Collections.synchronizedMap和ConcurrentHashMap三种,到底性能如何呢? 测试代码: package com. ...

  4. Collections.synchronizedMap()、ConcurrentHashMap、Hashtable之间的区别

    为什么要比较Hashtable.SynchronizedMap().ConcurrentHashMap之间的关系?因为常用的HashMap是非线程安全的,不能满足在多线程高并发场景下的需求. 那么为什 ...

  5. Collections.synchronizedMap()与ConcurrentHashMap的区别

    前面文章提到Collections.synchronizedMap()与ConcurrentHashM两者都提供了线程同步的功能.那两者的区别在哪呢?我们们先来看到代码例子.    下面代码实现一个线 ...

  6. ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点

    ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...

  7. Collections.synchronizedMap()与ConcurrentHashMap区别

    Collections.synchronizedMap()与ConcurrentHashMap主要区别是:Collections.synchronizedMap()和Hashtable一样,实现上在调 ...

  8. Map m = Collections.synchronizedMap(new HashMap())

    Collections.synchronizedMap(new HashMap())让你创建的new HashMap()支持多线程数据的同步.保证多线程访问数据的一致性 来源:http://www.b ...

  9. Collections.synchronizedMap 详解

    众所周知,HashMap 本身非线程安全的,但是当使用 Collections.synchronizedMap(new HashMap()) 进行包装后就返回一个线程安全的Map. 怎么实现的呢?今天 ...

随机推荐

  1. 单元测试Mockito中的Mock和Spy

    转载:https://blog.csdn.net/qq_30141957/article/details/81273829 项目中,有些函数需要处理某个服务的返回结果,而在对函数单元测试的时候,又不能 ...

  2. hadoop环境搭建与測试

    搭建參看: http://blog.csdn.net/w13770269691/article/details/16883663/ 查看集群状态: [root@master bin]# hdfs df ...

  3. ORACLE 执行计划

    有关oracle 执行计划几个不错的连接 执行计划的一些概念介绍:http://database.51cto.com/art/200611/34273.htm执行计划的例子:http://www.or ...

  4. ES6...扩展运算符(数组或类数组对象)

    数组和类数组对象定义 数组:[] 类数组对象:只包含使用从零开始,且自然递增的整数做键名,并且定义了length表示元素个数的对象,我们就认为他是类数组对象. 数组使用 let foo_arr = [ ...

  5. UART,SPI,IIC的一点理解

    转自:http://bbs.21ic.com/icview-253715-1-1.html UART通用异步收发器,UART是通用的异步传输模式,在它这种基础上加上其他接口或者解码器就衍生出多种异步传 ...

  6. 查看postgre都有哪些语句占用CPU,以及对应的sql语句

    查看占用CPU最多的几个postgresql ps aux | grep postgres | sort -n -r -k | head - | awk '{print $2, $3}' 查看所有po ...

  7. POJ--2406Power Strings+KMP求字符串最小周期

    题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个 ...

  8. oracle批量更新之使用游标进行分批次更新的5种方式及速度比对

      1.情景展示 一共有22w条数据, 需要将A表的主键更新至B表的指定字段,如何快速完成更新? 2.解决方案 声明: 解决方案不只一种,该文章只介绍快速游标法及代码实现: 两张表的ID和ID_CAR ...

  9. C#和JAVA的RSA密钥、公钥转换

    C#的秘钥跟JAVA的密钥区别   RSA对于程序本身是没有区别的,其格式都是相同的.对于不同的程序来说,存储使用的语法(包装的类)会有所不同. RSA语法和语法标准有很多,大的类型大概分为ASN.1 ...

  10. 基于py3和pymysql查询某时间段的数据

    #python3 #xiaodeng #基于py3和pymysql查询某时间段的数据 import pymysql conn=pymysql.connect(user='root',passwd='r ...