java - 多線程并發(fā)情況下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)) {//這一句會(huì)存在并發(fā)問題locks.put(lockName, new String('我是值'));System.out.println('加了一次'); } return locks.get(lockName);}@Overridepublic void run() { getLock(this.key);};public static void main(String[] args) { for (int i = 0; i < 20; i++) {new TestContain().start();; }}}
輸出結(jié)果:
加了一次加了一次加了一次
表明了Map.containsKey() 在多線程的情況下會(huì)判斷不準(zhǔn)確。
這是為什么呢? 有什么方法改進(jìn)呢?
問題解答
回答1: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來保證更新操作只進(jìn)行一次,而是用putIfAbsent來保證。
回答2:ConcurrentMap保證的是單次操作的原子性,而不是多次操作。
你的getLock函數(shù)中包含了多次操作,ConcurrentMap沒法擴(kuò)大它的同步范圍,你需要自己實(shí)現(xiàn)getLock的鎖。
回答3:使用putIfAbsent方法。
相關(guān)文章:
1. javascript - 關(guān)于<a>元素與<input>元素的JS事件運(yùn)行問題2. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效3. MySQL中的enum類型有什么優(yōu)點(diǎn)?4. java - 為什么第一個(gè)線程已經(jīng)釋放了鎖,第二個(gè)線程卻不行?5. mysql - 記得以前在哪里看過一個(gè)估算時(shí)間的網(wǎng)站6. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風(fēng)格檢查怎么辦。。。7. 大家好,我想請(qǐng)問一下怎么做搜索欄能夠搜索到自己網(wǎng)站的內(nèi)容。8. python - 啟動(dòng)Eric6時(shí)報(bào)錯(cuò):’qscintilla_zh_CN’ could not be loaded9. html - vue項(xiàng)目中用到了elementUI問題10. mysql - 查詢字段做了索引為什么不起效,還有查詢一個(gè)月的時(shí)候數(shù)據(jù)都是全部出來的,如果分拆3次的話就沒問題,為什么呢。
