HashMap是线程不安全的,只适用于单线程。因此在并发编程常用其对应的线程安全的类,常用的有Collections工具类的synchronizedMap创建的Map对象,是属于线程安全的;其次就是并发包下的ConcurrentHashMap类。两者由于实现原理稍有不同,因此在读与写的性能上也会有所差异。接下来通过编写测试程序对两者的读写性能分别做比较。
测试类如下:
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
public class MapTest {
private static ConcurrentHashMap<Integer,String> map = new ConcurrentHashMap();
private static Map<Integer,String> map2 = Collections.synchronizedMap(new HashMap<Integer,String>());
private static CountDownLatch cdl1 = new CountDownLatch(2);
private static CountDownLatch cdl2 = new CountDownLatch(2);
private static CountDownLatch cdl3 = new CountDownLatch(2);
private static CountDownLatch cdl4 = new CountDownLatch(2);
static class ConcurrentPutThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100000;i++) {
map.put(i, String.valueOf(i));
}
cdl1.countDown();
}
}
static class ConcurrentGetThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
int size = map.size();
for(int i=0;i<size;i++) {
map.get(i);
}
cdl2.countDown();
}
}
static class SynchronizedPutThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100000;i++) {
map2.put(i, String.valueOf(i));
}
cdl3.countDown();
}
}
static class SynchronizedGetThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
int size = map2.size();
for(int i=0;i<size;i++) {
map2.get(i);
}
cdl4.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
long start1 = System.currentTimeMillis();
new ConcurrentPutThread().start();
new ConcurrentPutThread().start();
cdl1.await();
long end1 = System.currentTimeMillis();
System.out.println("ConcurrentHashMap写入操作时间:"+(end1-start1));
long start2 = System.currentTimeMillis();
new SynchronizedPutThread().start();
new SynchronizedPutThread().start();
cdl3.await();
long end2 = System.currentTimeMillis();
System.out.println("SynchronizedMap写入操作时间为:"+(end2-start2));
long start3 = System.currentTimeMillis();
new ConcurrentGetThread().start();
new ConcurrentGetThread().start();
cdl2.await();
long end3 = System.currentTimeMillis();
System.out.println("ConcurrentHashMap读取操作时间为:"+(end3-start3));
long start4 = System.currentTimeMillis();
new SynchronizedGetThread().start();
new SynchronizedGetThread().start();
cdl4.await();
long end4 = System.currentTimeMillis();
System.out.println("SynchronizedMap读取操作时间为:"+(end4-start4));
}
}
测试结果如下:
#第一次测试结果
ConcurrentHashMap写入操作时间:68
SynchronizedMap写入操作时间为:110
ConcurrentHashMap读取操作时间为:10
SynchronizedMap读取操作时间为:36
#第二次测试结果
ConcurrentHashMap写入操作时间:65
SynchronizedMap写入操作时间为:124
ConcurrentHashMap读取操作时间为:7
SynchronizedMap读取操作时间为:37
#第三次测试结果
ConcurrentHashMap写入操作时间:50
SynchronizedMap写入操作时间为:110
ConcurrentHashMap读取操作时间为:12
SynchronizedMap读取操作时间为:30
#第四次测试结果
ConcurrentHashMap写入操作时间:60
SynchronizedMap写入操作时间为:157
ConcurrentHashMap读取操作时间为:7
SynchronizedMap读取操作时间为:30
#第五次测试结果
ConcurrentHashMap写入操作时间:60
SynchronizedMap写入操作时间为:120
ConcurrentHashMap读取操作时间为:10
SynchronizedMap读取操作时间为:30
根据以上五次测试结果大致可以知道:ConcurrentHashMap的写入时间大约小于SynchronizedMap的写入时间的一半,读取时间大约小于SynchronizedMap读取时间的三分之一。可以看出,相比较之下,ConcurrentHashMap的写入与读取的性能都更优,接下来从源码进行解释。
首先是put()方法SynchronizedMap的源码如下:
public int size() {
synchronized (mutex) {return m.size();}
}
public boolean isEmpty() {
synchronized (mutex) {return m.isEmpty();}
}
public boolean containsKey(Object key) {
synchronized (mutex) {return m.containsKey(key);}
}
public boolean containsValue(Object value) {
synchronized (mutex) {return m.containsValue(value);}
}
public V get(Object key) {
synchronized (mutex) {return m.get(key);}
}
public V put(K key, V value) {
synchronized (mutex) {return m.put(key, value);}
}
public V remove(Object key) {
synchronized (mutex) {return m.remove(key);}
}
public void putAll(Map<? extends K, ? extends V> map) {
synchronized (mutex) {m.putAll(map);}
}
public void clear() {
synchronized (mutex) {m.clear();}
}
以上可以看出SynchronizedMap的put封装了HashMap的put方法,并加上互斥锁保证了安全性。
而ConcurrentHashMap的put方法代码如下:
public V put(K key, V value) {
return putVal(key, value, false);
}
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable();
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);
return null;
}
可以看出,JDK1.8的ConcurrentHashMap取消了segments字段,采用了transient volatile HashEntry<K,V>[] table保存数据。这样对每个table数组元素加锁,见源代码中synchronized(f),因此可以减少并发冲突的概率,提高并发性能。所以ConcurrentHashMap的put并发性更好,因此相同工作下ConcurrentHashMap花费时间更少。
对于get方法,SynchronizedMap同样封装了HashMap的get方法并且加了同步锁。而ConcurrentHashMap代码如下:
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
从以上源码可以看出,ConcurrentHashMap的get方法采用了与HashMap一样的思路,并没有加锁,所以性能上优于SynchrinizedMap的get方法。