mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.iter.TransIter;
|
|||||||
import cn.hutool.core.lang.func.SerSupplier;
|
import cn.hutool.core.lang.func.SerSupplier;
|
||||||
import cn.hutool.core.lang.mutable.Mutable;
|
import cn.hutool.core.lang.mutable.Mutable;
|
||||||
import cn.hutool.core.lang.mutable.MutableObj;
|
import cn.hutool.core.lang.mutable.MutableObj;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.map.WeakConcurrentMap;
|
import cn.hutool.core.map.WeakConcurrentMap;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@@ -101,7 +102,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
|
|||||||
}
|
}
|
||||||
if (null == v && null != supplier) {
|
if (null == v && null != supplier) {
|
||||||
//每个key单独获取一把锁,降低锁的粒度提高并发能力,see pr#1385@Github
|
//每个key单独获取一把锁,降低锁的粒度提高并发能力,see pr#1385@Github
|
||||||
final Lock keyLock = keyLockMap.computeIfAbsent(key, k -> new ReentrantLock());
|
final Lock keyLock = MapUtil.computeIfAbsent(this.keyLockMap, key, k -> new ReentrantLock());
|
||||||
keyLock.lock();
|
keyLock.lock();
|
||||||
try {
|
try {
|
||||||
// 双重检查,防止在竞争锁的过程中已经有其它线程写入
|
// 双重检查,防止在竞争锁的过程中已经有其它线程写入
|
||||||
|
@@ -1263,4 +1263,19 @@ public class MapUtil extends MapGetUtil {
|
|||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 方法来自MyBatis,解决使用ConcurrentHashMap.computeIfAbsent导致的死循环问题。<br>
|
||||||
|
* A temporary workaround for Java 8 specific performance issue JDK-8161372 .<br>
|
||||||
|
* This class should be removed once we drop Java 8 support.
|
||||||
|
*
|
||||||
|
* @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<K, V> mappingFunction) {
|
||||||
|
final V value = map.get(key);
|
||||||
|
if (value != null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return map.computeIfAbsent(key, mappingFunction);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -133,7 +133,7 @@ public class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V>, Iterab
|
|||||||
@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) {
|
||||||
this.purgeStaleKeys();
|
this.purgeStaleKeys();
|
||||||
return this.raw.computeIfAbsent(ofKey(key, this.lastQueue), kWeakKey -> mappingFunction.apply(key));
|
return MapUtil.computeIfAbsent(this.raw, ofKey(key, this.lastQueue), kWeakKey -> mappingFunction.apply(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user