HashMap非线程安全,即任一时刻可以有多个线程同时写 HashMap,可能会导致数据的不一致。
如果需要满足线程安全,可以用 Collections 的 synchronizedMap 方法使HashMap 具有线程安全的能力,或者使用 ConcurrentHashMap。
synchronizedMap与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的写入与读取的性能都更优
ConcurrentHashMap的写入时间大约比SynchronizedMap的写入时间 快2倍
ConcurrentHashMap的读取时间大约比SynchronizedMap的读取时间 快3倍
接下来从源码进行解释:
👇SynchronizedMap.put() &SynchronizedMap.get() 原码
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);}
}
👆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.get()原码
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方法