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