原文链接:http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/
在Java中,HashMap是一个非常有用的数据结构。几乎每一个Java应用都会使用到它。我之前的博文中有介绍过如何实现一个线程安全的缓存,在这个例子中,我就使用到了HashMap。然而,需要注意的是,HashMap本身并不是一个线程安全的Collection类。
常见问题
ConcurrentHashMap和Collections.synchronizedMap(Map)分别是什么?ConcurrentHashMap和Collections.synchronizedMap(Map)在性能上有什么区别?ConcurrentHashMap和Collections.synchronizedMap(Map)的优劣对比?关于HashMap和ConcurrentHashMap的常见面试问题
在这篇文章中,将涵盖以上的问题,并解释我们应该如何将HashMap变得线程安全。
原因
Map是一个通过键值对的形势来存储元素的容器,其中,要求key保持唯一,并且每一个key唯一对应一个value。如果你在设计一个高度并行化的程序,并且需要在不同的线程中去读取或者修改一个Map对象,那么使用线程安全的Map则是一个理想的选择。最典型的一个例子就是生产者-消费者模式,生产者不断的修改Map而消费者同时也在读取Map中的值。
那么对于Map来说,什么是线程安全呢?简单的来说,就是当多个线程同时在使用一个Map的时候,至少有一个线程对Map的结构进行了修改,那么必须保证这个修改被立即同步到其他线程中去,避免其他线程获取到错误的值。
解决方案
有两种方法可以解决HashMap的线程安全问题:
Java的Collections库中的synchronizedMap()方法使用ConcurrentHashMap
译者注:其实还有第三种方法,使用Hashtable。不过Hashtable是Java 1.1提供的旧有类,从性能上和使用上都不如其他的替代类,因此已经不推荐使用
Map<String, String> normalMap =
new Hashtable<String, String>();
synchronizedHashMap = Collections.synchronizedMap(
new HashMap<String, String>());
concurrentHashMap =
new ConcurrentHashMap<String, String>();
12345678
ConcurrentHashMap
当你程序需要高度的并行化的时候,你应该使用ConcurrentHashMap尽管没有同步整个Map,但是它仍然是线程安全的读操作非常快,而写操作则是通过加锁完成的在对象层次上不存在锁(即不会阻塞线程)锁的粒度设置的非常好,只对哈希表的某一个key加锁ConcurrentHashMap不会抛出ConcurrentModificationException,即使一个线程在遍历的同时,另一个线程尝试进行修改。ConcurrentHashMap会使用多个锁
SynchronizedHashMap
会同步整个对象每一次的读写操作都需要加锁对整个对象加锁会极大降低性能这相当于只允许同一时间内至多一个线程操作整个Map,而其他线程必须等待它有可能造成资源冲突(某些线程等待较长时间)SynchronizedHashMap会返回Iterator,当遍历时进行修改会抛出异常
示例
创建类CrunchifyConcurrentHashMapVsSynchronizedHashMap.java实例化每一个对象(HashTable, SynchronizedMap 和 ConcurrentHashMap)添加/读取5*50万行数据到Map中测试开始和结束的时间使用ExecutorService来开启5个线程,同时读写
package crunchify.com.tutorials;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Crunchify.com
*
*/
public class CrunchifyConcurrentHashMapVsSynchronizedMap {
public final static int THREAD_POOL_SIZE =
5;
public static Map<String, Integer> crunchifyHashTableObject =
null;
public static Map<String, Integer> crunchifySynchronizedMapObject =
null;
public static Map<String, Integer> crunchifyConcurrentHashMapObject =
null;
public static void main(String[] args)
throws InterruptedException {
crunchifyHashTableObject =
new Hashtable<String, Integer>();
crunchifyPerformTest(crunchifyHashTableObject);
crunchifySynchronizedMapObject = Collections.synchronizedMap(
new HashMap<String, Integer>());
crunchifyPerformTest(crunchifySynchronizedMapObject);
crunchifyConcurrentHashMapObject =
new ConcurrentHashMap<String, Integer>();
crunchifyPerformTest(crunchifyConcurrentHashMapObject);
}
public static void crunchifyPerformTest(
final Map<String, Integer> crunchifyThreads)
throws InterruptedException {
System.out.println(
"Test started for: " + crunchifyThreads.getClass());
long averageTime =
0;
for (
int i =
0; i <
5; i++) {
long startTime = System.nanoTime();
ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
for (
int j =
0; j < THREAD_POOL_SIZE; j++) {
crunchifyExServer.execute(
new Runnable() {
@SuppressWarnings(
"unused")
@Override
public void run() {
for (
int i =
0; i <
500000; i++) {
Integer crunchifyRandomNumber = (
int) Math.ceil(Math.random() *
550000);
Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
}
}
});
}
crunchifyExServer.shutdown();
crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
long entTime = System.nanoTime();
long totalTime = (entTime - startTime) /
1000000L;
averageTime += totalTime;
System.out.println(
"2500K entried added/retrieved in " + totalTime +
" ms");
}
System.out.println(
"For " + crunchifyThreads.getClass() +
" the average time is " + averageTime /
5 +
" ms\n");
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
结果如下:
Test started
for:
class java.util.Hashtable
500K entried added/retrieved
in 1432 ms
500K entried added/retrieved
in 1425 ms
500K entried added/retrieved
in 1373 ms
500K entried added/retrieved
in 1369 ms
500K entried added/retrieved
in 1438 ms
For class java.util.Hashtable the average
time 1407 ms
Test started
for:
class java.util.Collections$SynchronizedMap
500K entried added/retrieved
in 1431 ms
500K entried added/retrieved
in 1460 ms
500K entried added/retrieved
in 1387 ms
500K entried added/retrieved
in 1456 ms
500K entried added/retrieved
in 1406 ms
For class java.util.Collections$SynchronizedMap the average
time 1428 ms
Test started
for:
class java.util.concurrent.ConcurrentHashMap
500K entried added/retrieved
in 413 ms
500K entried added/retrieved
in 351 ms
500K entried added/retrieved
in 427 ms
500K entried added/retrieved
in 337 ms
500K entried added/retrieved
in 339 ms
For class java.util.concurrent.ConcurrentHashMap the average
time 373 ms <== Much faster