Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
looly authored Apr 2, 2023
1 parent f32da50 commit 725fe23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ public static <K, V> V computeIfAbsent(ConcurrentMap<K, V> map, K key, Function<
if (JRE.JAVA_8.isCurrentVersion()) {
V v = map.get(key);
if (null == v) {
v = map.computeIfAbsent(key, func);
// issue#11986 lock bug
// v = map.computeIfAbsent(key, func);

// this bug fix methods maybe cause `func.apply` multiple calls.
value = func.apply(key);
final V res = map.putIfAbsent(key, value);
if(null != res){
// if pre value present, means other thread put value already, and putIfAbsent not effect
// return exist value
return res;
}
// if pre value is null, means putIfAbsent effected, return current value
}
return v;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public void testComputeIfAbsent() {
ifAbsent = ConcurrentHashMapUtils.computeIfAbsent(map, "mxsm", k -> "mxsm1");
assertEquals("mxsm1", ifAbsent);
}

@Test
public void issue11986Test(){
// https://github.com/apache/dubbo/issues/11986
final ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
// // map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
ConcurrentHashMapUtils.computeIfAbsent(map, "AaAa", key->map.computeIfAbsent("BBBB",key2->42));

assertEquals(2, map.size());
assertEquals(Integer.valueOf(42), map.get("AaAa"));
assertEquals(Integer.valueOf(42), map.get("BBBB"));
}
}

0 comments on commit 725fe23

Please sign in to comment.