All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hutool.core.map.SafeConcurrentHashMap Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.core.map;

import cn.hutool.core.util.JdkUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * 安全的ConcurrentHashMap实现
* 此类用于解决在JDK8中调用{@link ConcurrentHashMap#computeIfAbsent(Object, Function)}可能造成的死循环问题。
* 方法来自Dubbo,见:issues#2349
*

* 相关bug见:@see https://bugs.openjdk.java.net/browse/JDK-8161372 * * @param 键类型 * @param 值类型 */ public class SafeConcurrentHashMap extends ConcurrentHashMap { private static final long serialVersionUID = 1L; // region == 构造 == /** * 构造,默认初始大小(16) */ public SafeConcurrentHashMap() { super(); } /** * 构造 * * @param initialCapacity 预估初始大小 */ public SafeConcurrentHashMap(int initialCapacity) { super(initialCapacity); } /** * 构造 * * @param m 初始键值对 */ public SafeConcurrentHashMap(Map m) { super(m); } /** * 构造 * * @param initialCapacity 初始容量 * @param loadFactor 增长系数 */ public SafeConcurrentHashMap(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } /** * 构造 * * @param initialCapacity 初始容量 * @param loadFactor 增长系数 * @param concurrencyLevel 并发级别,即Segment的个数 */ public SafeConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { super(initialCapacity, loadFactor, concurrencyLevel); } // endregion == 构造 == @Override public V computeIfAbsent(K key, Function mappingFunction) { if (JdkUtil.IS_JDK8) { return MapUtil.computeIfAbsentForJdk8(this, key, mappingFunction); } else { return super.computeIfAbsent(key, mappingFunction); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy