java - 多线程并发情况下Map.containsKey() 判断有问题
本文介绍了java - 多线程并发情况下Map.containsKey() 判断有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
有下面一段代码:
package test;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class TestContain extends Thread{
private final String key = "key";
private final static ConcurrentMap<String, Object> locks = new ConcurrentHashMap<>();
private static Object getLock(String lockName) {
if (!locks.containsKey(lockName)) {
//这一句会存在并发问题
locks.put(lockName, new String("我是值"));
System.out.println("加了一次");
}
return locks.get(lockName);
}
@Override
public void run() {
getLock(this.key);
};
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new TestContain().start();;
}
}
}
输出结果:
加了一次
加了一次
加了一次
表明了Map.containsKey() 在多线程的情况下会判断不准确。
这是为什么呢? 有什么方法改进呢?
解决方案
ConcurrentHashMap
的doc上有一段
Retrieval operations (including <tt>get</tt>) generally do not
block, so may overlap with update operations (including
<tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
of the most recently completed update operations holding
upon their onset.
里面的get方法并不加锁,get方法只是拿到最新完成update的值。
所以题主方法中的locks.containsKey(lockName)
没有锁来保证线程安全的。而且感觉ConcurrentHashMap
的使用场景并不是用containsKey
来保证更新操作只进行一次,而是用putIfAbsent
来保证。
这篇关于java - 多线程并发情况下Map.containsKey() 判断有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文