ConcurrentHashMap一般只被看作并发效率更高的Map,用来替换其余线程安全的Map容器,好比Hashtable和Collections.synchronizedMap。实际上,线程安全的容器,特别是Map,应用场景没有想象中的多,不少状况下一个业务会涉及容器的多个操做,即复合操做,并发执行时,线程安全的容器只能保证自身的数据不被破坏,但没法保证业务的行为是否正确。缓存
举个例子:统计文本中单词出现的次数,把单词出现的次数记录到一个Map中,代码以下:安全
1 |
private final Map<String, Long> wordCounts = new ConcurrentHashMap<>(); |
3 |
public long increase(String word) { |
4 |
Long oldValue = wordCounts.get(word); |
5 |
Long newValue = (oldValue == null ) ? 1L : oldValue + 1 ; |
6 |
wordCounts.put(word, newValue); |
若是多个线程并发调用这个increase()方法,increase()的实现就是错误的,由于多个线程用相同的word调用时,极可能会覆盖相互的结果,形成记录的次数比实际出现的次数少。并发
除了用锁解决这个问题,另一个选择是使用ConcurrentMap接口定义的方法:ide
1 |
public interface ConcurrentMap<K, V> extends Map<K, V> { |
2 |
V putIfAbsent(K key, V value); |
3 |
boolean remove(Object key, Object value); |
4 |
boolean replace(K key, V oldValue, V newValue); |
5 |
V replace(K key, V value); |
这是个被不少人忽略的接口,也常常见有人错误地使用这个接口。ConcurrentMap接口定义了几个基于 CAS(Compare and Set)操做,很简单,但很是有用,下面的代码用ConcurrentMap解决上面问题:google
01 |
private final ConcurrentMap<String, Long> wordCounts = new ConcurrentHashMap<>(); |
03 |
public long increase(String word) { |
04 |
Long oldValue, newValue; |
06 |
oldValue = wordCounts.get(word); |
07 |
if (oldValue == null ) { |
08 |
// Add the word firstly, initial the value as 1 |
10 |
if (wordCounts.putIfAbsent(word, newValue) == null ) { |
14 |
newValue = oldValue + 1 ; |
15 |
if (wordCounts.replace(word, oldValue, newValue)) { |
代码有点复杂,主要由于ConcurrentMap中不能保存value为null的值,因此得同时处理word不存在和已存在两种状况。spa
上面的实现每次调用都会涉及Long对象的拆箱和装箱操做,很明显,更好的实现方式是采用AtomicLong,下面是采用AtomicLong后的代码:线程
01 |
private final ConcurrentMap<String, AtomicLong> wordCounts = new ConcurrentHashMap<>(); |
03 |
public long increase(String word) { |
04 |
AtomicLong number = wordCounts.get(word); |
06 |
AtomicLong newNumber = new AtomicLong( 0 ); |
07 |
number = wordCounts.putIfAbsent(word, newNumber); |
12 |
return number.incrementAndGet(); |
这个实现仍然有一处须要说明的地方,若是多个线程同时增长一个目前还不存在的词,那么极可能会产生多个newNumber对象,但最终只有一个newNumber有用,其余的都会被扔掉。对于这个应用,这不算问题,建立AtomicLong的成本不高,并且只在添加不存在词是出现。但换个场景,好比缓存,那么这极可能就是问题了,由于缓存中的对象获取成本通常都比较高,并且一般缓存都会常常失效,那么避免重复建立对象就有价值了。下面的代码演示了怎么处理这种状况:code
01 |
private final ConcurrentMap<String, Future<ExpensiveObj>> cache = new ConcurrentHashMap<>(); |
03 |
public ExpensiveObj get( final String key) { |
04 |
Future<ExpensiveObj> future = cache.get(key); |
06 |
Callable<ExpensiveObj> callable = new Callable<ExpensiveObj>() { |
08 |
public ExpensiveObj call() throws Exception { |
09 |
return new ExpensiveObj(key); |
12 |
FutureTask<ExpensiveObj> task = new FutureTask<>(callable); |
14 |
future = cache.putIfAbsent(key, task); |
23 |
} catch (Exception e) { |
25 |
throw new RuntimeException(e); |
解决方法其实就是用一个Proxy对象来包装真正的对象,跟常见的lazy load原理相似;使用FutureTask主要是为了保证同步,避免一个Proxy建立多个对象。注意,上面代码里的异常处理是不许确的。对象
最后再补充一下,若是真要实现前面说的统计单词次数功能,最合适的方法是Guava包中AtomicLongMap;通常使用ConcurrentHashMap,也尽可能使用Guava中的MapMaker或cache实现。接口