This commit is contained in:
Looly 2023-04-02 19:09:22 +08:00
parent f0fb98c584
commit 4dd0ed53d5
2 changed files with 16 additions and 24 deletions

View File

@ -20,7 +20,6 @@ import cn.hutool.core.collection.iter.IterUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.reflect.ConstructorUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.JdkUtil;
import cn.hutool.core.util.ObjUtil;
import java.util.*;
@ -1281,20 +1280,22 @@ public class MapUtil extends MapGetUtil {
* @return
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
*/
public static <K, V> V computeIfAbsent(final Map<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {
if (JdkUtil.IS_JDK8) {
V value = map.get(key);
if (null == value) {
map.putIfAbsent(key, mappingFunction.apply(key));
value = map.get(key);
// 判空后调用依旧无法解决死循环问题
// Issue2349Test
//value = map.computeIfAbsent(key, mappingFunction);
public static <K, V> V computeIfAbsentForJdk8(final Map<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {
V value = map.get(key);
if (null == value) {
value = mappingFunction.apply(key);
final V res = map.putIfAbsent(key, mappingFunction.apply(key));
if(null != res){
// issues#I6RVMY
// 如果旧值存在说明其他线程已经赋值成功putIfAbsent没有执行返回旧值
return res;
}
return value;
} else {
return map.computeIfAbsent(key, mappingFunction);
// 如果旧值不存在说明赋值成功返回当前值
// Dubbo的解决方式判空后调用依旧无法解决死循环问题
// Issue2349Test
//value = map.computeIfAbsent(key, mappingFunction);
}
return value;
}
}

View File

@ -84,16 +84,7 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
@Override
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
if (JdkUtil.IS_JDK8) {
V value = get(key);
if (null == value) {
putIfAbsent(key, mappingFunction.apply(key));
value = get(key);
// 判空后调用依旧无法解决死循环问题
// Issue2349Test
//value = map.computeIfAbsent(key, mappingFunction);
}
return value;
return MapUtil.computeIfAbsentForJdk8(this, key, mappingFunction);
} else {
return super.computeIfAbsent(key, mappingFunction);
}