类 ConcurrentReferenceHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
cn.taketoday.util.ConcurrentReferenceHashMap<K,V>
- 类型参数:
K- the key typeV- the value type
- 所有已实现的接口:
ConcurrentMap<K,,V> Map<K,V>
A
ConcurrentHashMap that uses soft or
weak references for both keys and values.
This class can be used as an alternative to
Collections.synchronizedMap(new WeakHashMap<K, Reference<V>>()) in order to
support better performance when accessed concurrently. This implementation follows the
same design constraints as ConcurrentHashMap with the exception that
null values and null keys are supported.
NOTE: The use of references means that there is no guarantee that items placed into the map will be subsequently available. The garbage collector may discard references at any time, so it may appear that an unknown thread is silently removing entries.
If not explicitly specified, this implementation will use soft entry references.
- 从以下版本开始:
- 4.0
- 作者:
- Phillip Webb, Juergen Hoeller, TODAY 2021/9/11 12:49
-
嵌套类概要
嵌套类修饰符和类型类说明private static interfaceAllows a task access toConcurrentReferenceHashMap.Segmententries.protected static final classA single map entry.private classInternal entry iterator implementation.private classInternal entry-set implementation.protected static interfaceA reference to anConcurrentReferenceHashMap.Entrycontained in the map.protected classStrategy class used to manageReferences.static enumVarious reference types supported by this map.protected static enumThe types of restructuring that can be performed.protected final classA single segment used to divide the map to allow better concurrent performance.private static final classInternalConcurrentReferenceHashMap.Referenceimplementation forSoftReferences.private classA task that can berunagainst aConcurrentReferenceHashMap<K,.V>.Segment private static enumVarious options supported by aTask.private static final classInternalConcurrentReferenceHashMap.Referenceimplementation forWeakReferences.从类继承的嵌套类/接口 java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
字段概要
字段修饰符和类型字段说明private static final intprivate static final intprivate static final floatprivate static final ConcurrentReferenceHashMap.ReferenceTypeLate binding entry set.private final floatWhen the average number of references per table exceeds this value resize will be attempted.private static final intprivate static final intprivate final ConcurrentReferenceHashMap.ReferenceTypeThe reference type: SOFT or WEAK.private final ConcurrentReferenceHashMap<K,V>.Segment[] Array of segments indexed using the high order bits from the hash.private final intThe shift value used to calculate the size of the segments array and an index from the hash. -
构造器概要
构造器构造器说明Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity) Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance. -
方法概要
修饰符和类型方法说明protected static intcalculateShift(int minimumValue, int maximumValue) Calculate a shift value that can be used to create a power-of-two value between the specified maximum and minimum values.voidclear()booleancontainsKey(Object key) private static ConcurrentReferenceHashMap.Reference[]createReferenceArray(int size) protected ConcurrentReferenceHashMap<K,V>.ReferenceManager Factory method that returns theConcurrentReferenceHashMap<K,.V>.ReferenceManager private <T> TentrySet()private static <K,V> ConcurrentReferenceHashMap.Reference<K, V> findInChain(ConcurrentReferenceHashMap.Reference<K, V> ref, Object key, int hash) private static ConcurrentReferenceHashMap.ReferencefindInChain(Object key, int hash, ConcurrentReferenceHashMap.Reference[] references) protected intGet the hash for a given object, apply an additional hash function to reduce collisions.private static intgetIndex(int hash, ConcurrentReferenceHashMap.Reference[] references) protected final floatgetOrDefault(Object key, V defaultValue) protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure) Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.protected final ConcurrentReferenceHashMap<K,V>.Segment getSegment(int index) private ConcurrentReferenceHashMap<K,V>.Segment getSegmentForHash(int hash) protected final intbooleanisEmpty()voidRemove any entries that have been garbage collected and are no longer referenced.private VputIfAbsent(K key, V value) booleanbooleanintsize()从类继承的方法 java.util.AbstractMap
clone, containsValue, equals, hashCode, keySet, putAll, toString, values从接口继承的方法 java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll
-
字段详细资料
-
DEFAULT_INITIAL_CAPACITY
private static final int DEFAULT_INITIAL_CAPACITY- 另请参阅:
-
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTOR- 另请参阅:
-
DEFAULT_CONCURRENCY_LEVEL
private static final int DEFAULT_CONCURRENCY_LEVEL- 另请参阅:
-
DEFAULT_REFERENCE_TYPE
-
MAXIMUM_CONCURRENCY_LEVEL
private static final int MAXIMUM_CONCURRENCY_LEVEL- 另请参阅:
-
MAXIMUM_SEGMENT_SIZE
private static final int MAXIMUM_SEGMENT_SIZE- 另请参阅:
-
segments
Array of segments indexed using the high order bits from the hash. -
loadFactor
private final float loadFactorWhen the average number of references per table exceeds this value resize will be attempted. -
referenceType
The reference type: SOFT or WEAK. -
shift
private final int shiftThe shift value used to calculate the size of the segments array and an index from the hash. -
entrySet
Late binding entry set.
-
-
构造器详细资料
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap()Create a newConcurrentReferenceHashMapinstance. -
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the map
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the maploadFactor- the load factor. When the average number of references per table exceeds this value resize will be attempted
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the mapconcurrencyLevel- the expected number of threads that will concurrently write to the map
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the mapreferenceType- the reference type used for entries (soft or weak)
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the maploadFactor- the load factor. When the average number of references per table exceeds this value, resize will be attempted.concurrencyLevel- the expected number of threads that will concurrently write to the map
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance.- 参数:
initialCapacity- the initial capacity of the maploadFactor- the load factor. When the average number of references per table exceeds this value, resize will be attempted.concurrencyLevel- the expected number of threads that will concurrently write to the mapreferenceType- the reference type used for entries (soft or weak)
-
-
方法详细资料
-
getLoadFactor
protected final float getLoadFactor() -
getSegmentsSize
protected final int getSegmentsSize() -
getSegment
-
createReferenceManager
Factory method that returns theConcurrentReferenceHashMap<K,. This method will be called once for eachV>.ReferenceManager ConcurrentReferenceHashMap<K,.V>.Segment - 返回:
- a new reference manager
-
getHash
Get the hash for a given object, apply an additional hash function to reduce collisions. This implementation uses the same Wang/Jenkins algorithm asConcurrentHashMap. Subclasses can override to provide alternative hashing.- 参数:
o- the object to hash (may be null)- 返回:
- the resulting hash code
-
get
-
getOrDefault
- 指定者:
getOrDefault在接口中ConcurrentMap<K,V> - 指定者:
getOrDefault在接口中Map<K,V>
-
containsKey
- 指定者:
containsKey在接口中Map<K,V> - 覆盖:
containsKey在类中AbstractMap<K,V>
-
getReference
@Nullable protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(@Nullable Object key, ConcurrentReferenceHashMap.Restructure restructure) Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.- 参数:
key- the key (can benull)restructure- types of restructure allowed during this call- 返回:
- the reference, or
nullif not found
-
put
-
putIfAbsent
- 指定者:
putIfAbsent在接口中ConcurrentMap<K,V> - 指定者:
putIfAbsent在接口中Map<K,V>
-
put
-
remove
-
remove
-
replace
-
replace
-
clear
public void clear() -
purgeUnreferencedEntries
public void purgeUnreferencedEntries()Remove any entries that have been garbage collected and are no longer referenced. Under normal circumstances garbage collected entries are automatically purged as items are added or removed from the Map. This method can be used to force a purge, and is useful when the Map is read frequently but updated less often. -
size
public int size() -
isEmpty
public boolean isEmpty() -
entrySet
-
doTask
-
getSegmentForHash
-
calculateShift
protected static int calculateShift(int minimumValue, int maximumValue) Calculate a shift value that can be used to create a power-of-two value between the specified maximum and minimum values.- 参数:
minimumValue- the minimum valuemaximumValue- the maximum value- 返回:
- the calculated shift (use
1 << shiftto obtain a value)
-
createReferenceArray
-
findInChain
@Nullable private static ConcurrentReferenceHashMap.Reference findInChain(@Nullable Object key, int hash, ConcurrentReferenceHashMap.Reference[] references) -
getIndex
-
findInChain
@Nullable private static <K,V> ConcurrentReferenceHashMap.Reference<K,V> findInChain(ConcurrentReferenceHashMap.Reference<K, V> ref, @Nullable Object key, int hash)
-