ConcurrentHashMap
来源:互联网 发布:手机淘宝社区 编辑:程序博客网 时间:2024/06/08 05:27
ConcurrentHashMap通常只被看做并发效率更高的Map,用来替换其他线程安全的Map容器,比如Hashtable和Collections.synchronizedMap。实际上,线程安全的容器,特别是Map,应用场景没有想象中的多,很多情况下一个业务会涉及容器的多个操作,即复合操作,并发执行时,线程安全的容器只能保证自身的数据不被破坏,但无法保证业务的行为是否正确。
举个例子:统计文本中单词出现的次数,把单词出现的次数记录到一个Map中,代码如下:
1
2
3
4
5
6
7
8
private
final
Map<String, Long> wordCounts =
new
ConcurrentHashMap<>();
public
long
increase(String word) {
Long oldValue = wordCounts.get(word);
Long newValue = (oldValue ==
null
) ? 1L : oldValue +
1
;
wordCounts.put(word, newValue);
return
newValue;
}
除了用锁解决这个问题,另外一个选择是使用ConcurrentMap接口定义的方法:
1
2
3
4
5
6
public
interface
ConcurrentMap<K, V>
extends
Map<K, V> {
V putIfAbsent(K key, V value);
boolean
remove(Object key, Object value);
boolean
replace(K key, V oldValue, V newValue);
V replace(K key, V value);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private
final
ConcurrentMap<String, Long> wordCounts =
new
ConcurrentHashMap<>();
public
long
increase(String word) {
Long oldValue, newValue;
while
(
true
) {
oldValue = wordCounts.get(word);
if
(oldValue ==
null
) {
// Add the word firstly, initial the value as 1
newValue = 1L;
if
(wordCounts.putIfAbsent(word, newValue) ==
null
) {
break
;
}
}
else
{
newValue = oldValue +
1
;
if
(wordCounts.replace(word, oldValue, newValue)) {
break
;
}
}
}
return
newValue;
}
上面的实现每次调用都会涉及Long对象的拆箱和装箱操作,很明显,更好的实现方式是采用AtomicLong,下面是采用AtomicLong后的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
private
final
ConcurrentMap<String, AtomicLong> wordCounts =
new
ConcurrentHashMap<>();
public
long
increase(String word) {
AtomicLong number = wordCounts.get(word);
if
(number ==
null
) {
AtomicLong newNumber =
new
AtomicLong(
0
);
number = wordCounts.putIfAbsent(word, newNumber);
if
(number ==
null
) {
number = newNumber;
}
}
return
number.incrementAndGet();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private
final
ConcurrentMap<String, Future<ExpensiveObj>> cache =
new
ConcurrentHashMap<>();
public
ExpensiveObj get(
final
String key) {
Future<ExpensiveObj> future = cache.get(key);
if
(future ==
null
) {
Callable<ExpensiveObj> callable =
new
Callable<ExpensiveObj>() {
@Override
public
ExpensiveObj call()
throws
Exception {
return
new
ExpensiveObj(key);
}
};
FutureTask<ExpensiveObj> task =
new
FutureTask<>(callable);
future = cache.putIfAbsent(key, task);
if
(future ==
null
) {
future = task;
task.run();
}
}
try
{
return
future.get();
}
catch
(Exception e) {
cache.remove(key);
throw
new
RuntimeException(e);
}
}
最后再补充一下,如果真要实现前面说的统计单词次数功能,最合适的方法是Guava包中AtomicLongMap;一般使用ConcurrentHashMap,也尽量使用Guava中的MapMaker或cache实现。
0 0
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- ConcurrentHashMap
- concurrenthashmap
- ConcurrentHashMap
- ConcurrentHashMap
- IOS_代码实现树形导航
- MVC初步理解
- c++引用返回值
- 【android ndk】macos环境下Android Studio中利用gradle编译jni模块及配置
- 归并排序算法 C代码实现(算法)
- ConcurrentHashMap
- 由浅入深理解Activity的诞生
- ASN.1学习笔记
- Vim学习笔记1---基本操作
- ubuntu安装samba实现与windows文件共享
- xcode 学习总结
- LeetCode_Maximum Subarray | Maximum Product Subarray
- Trie树的简单应用
- 2015周赛4