readWriteLock并发map与concurrentHashMap对比
来源:互联网 发布:软件项目保密管理制度 编辑:程序博客网 时间:2024/06/05 08:49
测试子下用readLock与WriteLock实现的加锁map与concurrentHashMap,结果相差不是特别大
实现代码:
public class ReadWriteHashMap<K, V> {
private Map<K, V> mapContainer;
private ReadWriteLock lock = new ReentrantReadWriteLock();
private Lock readLock = lock.readLock();
private Lock writeLock = lock.writeLock();
public static final int count=1000000;
public ReadWriteHashMap() {
mapContainer = new HashMap<K, V>();
}
public V put(K key, V value) {
writeLock.lock();
try {
return mapContainer.put(key, value);
} finally {
writeLock.unlock();
}
}
public V get(K key){
readLock.lock();
try{
return mapContainer.get(key);
}finally {
readLock.unlock();
}
}
@Test
public void testWriteOpOfReadWriteMap() throws Exception {
Long oldTime=System.currentTimeMillis();
ReadWriteHashMap<String, String> map=new ReadWriteHashMap<String, String>();
for(int i=0;i<count;i++){
map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
Long cost=System.currentTimeMillis()-oldTime;
System.out.println("readWriteHashMap write cost:"+cost);
}
@Test
public void testReadOpOfReadWriteMap() throws Exception {
ReadWriteHashMap<String, String> map=new ReadWriteHashMap<String, String>();
String k="d";
for(int i=0;i<count;i++){
if(i==0){
k=UUID.randomUUID().toString();
map.put(k, UUID.randomUUID().toString());
}else{
map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
}
Long oldTime=System.currentTimeMillis();
map.get(k);
for(int i=0;i<count;i++){
map.get(String.valueOf(i));
}
Long cost=System.currentTimeMillis()-oldTime;
System.out.println("readWriteHashMap read cost:"+cost);
}
//concurrentHashMap
@Test
public void testWriteOpOfConcurrentHashMap() throws Exception {
Long oldTime=System.currentTimeMillis();
Map<String, String> map=new ConcurrentHashMap<String, String>();
for(int i=0;i<count;i++){
map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
Long cost=System.currentTimeMillis()-oldTime;
System.out.println("concurrentHashmap write cost:"+cost);
}
@Test
public void testReadOpOfConcurrentHashMap() throws Exception {
Map<String, String> map=new ConcurrentHashMap<String, String>();
String k="d";
for(int i=0;i<count;i++){
if(i==0){
k=UUID.randomUUID().toString();
map.put(k, UUID.randomUUID().toString());
}else{
map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
}
Long oldTime=System.currentTimeMillis();
map.get(k);
for(int i=0;i<count;i++){
map.get(String.valueOf(i));
}
Long cost=System.currentTimeMillis()-oldTime;
System.out.println("concurrentHashmap read cost:"+cost);
}
}
1.
10万数据
readWriteHashMap write cost:2305
readWriteHashMap read cost:36
100万
readWriteHashMap write cost:18605
readWriteHashMap read cost:293
2.
10万数据
readWriteHashMap read cost:35
readWriteHashMap write cost:1646
100万
readWriteHashMap write cost:20522
readWriteHashMap read cost:308
3.
10万数据
readWriteHashMap read cost:72
readWriteHashMap write cost:1884
100d万
readWriteHashMap write cost:20522
readWriteHashMap read cost:308
4.
10万数据
readWriteHashMap write cost:2512
readWriteHashMap read cost:34
ConcurrentHashMap如下:
1.
10万数据
concurrentHashmap write cost:2492
concurrentHashmap read cost:21
100万数据
concurrentHashmap write cost:21998concurrentHashmap read cost:373
2.
10万数据
concurrentHashmap read cost:19
concurrentHashmap write cost:1646
100万数据
concurrentHashmap write cost:22475
concurrentHashmap read cost:423
3.
10万数据
concurrentHashmap write cost:2408
concurrentHashmap read cost:17
100万
concurrentHashmap write cost:22239
concurrentHashmap read cost:408
4.
10万数据
concurrentHashmap write cost:2385
concurrentHashmap read cost:36
100万
concurrentHashmap write cost:20524
concurrentHashmap read cost:319
- readWriteLock并发map与concurrentHashMap对比
- JDK容器与并发—Map—ConcurrentHashMap
- HahMap 与 ConcurrentHashMap 对比
- ReadWriteLock与synchronize的性能对比
- CopyOnWriteArrayList----ReadWriteLock---ConcurrentHashMap
- Map的并发处理(ConcurrentHashMap)
- Java多线程/并发06、线程锁Lock与ReadWriteLock
- Java并发ReadWriteLock接口
- java并发实践--ConcurrentHashMap与CAS
- Java 并发实践 — ConcurrentHashMap 与 CAS
- 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)
- 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)
- Java_并发线程_Lock、ReadWriteLock
- unordered_map 与 map 的对比
- Java多线程与并发库高级应用之读写锁ReadWriteLock
- (多线程与并发)面试题03--java中读写锁ReadWriteLock
- 并发编程--读写锁ReadWriteLock和ReentrantReadWriteLock写锁与读锁(二)
- [JAVA学习笔记-86]ConcurrentHashMap与synchronizedMap的对比
- PL/SQL_处理例外2(非预定义异常、自定义异常)
- SSH的端口转发:本地转发Local Forward和远程转发Remote Forward
- SIPp之认证注册
- 关于ffmpeg的例子muxing.c
- 防御DDoS攻击实用指南
- readWriteLock并发map与concurrentHashMap对比
- 能触控才是真的好 热卖Win8触控本大搜罗
- 迭代器的初识
- hdu 1026 Ignatius and the Princess I(优先队列+BFS)
- poj1083:moving tables
- Java 8简明教程
- QT VS检测内存泄漏
- 图解Arraylist内存分配
- 线性表