readWriteLock併發map與concurrentHashMap對比
測試子下用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