类 ConcurrentReferenceHashMap<K,V>
- java.lang.Object
- java.util.AbstractMap<K,V>
- org.springframework.util.ConcurrentReferenceHashMap<K,V>
- 类型参数:
K- the key typeV- the value type
- 所有已实现的接口:
ConcurrentMap<K,V>,Map<K,V>
public class ConcurrentReferenceHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
AConcurrentHashMapthat usessoftor weak references for bothkeysandvalues.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 asConcurrentHashMapwith the exception thatnullvalues andnullkeys 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.
- 从以下版本开始:
- 3.2
- 作者:
- Phillip Webb, Juergen Hoeller
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static classConcurrentReferenceHashMap.Entry<K,V>A single map entry.protected static interfaceConcurrentReferenceHashMap.Reference<K,V>A reference to anConcurrentReferenceHashMap.Entrycontained in the map.protected classConcurrentReferenceHashMap.ReferenceManagerStrategy class used to manageReferences.static classConcurrentReferenceHashMap.ReferenceTypeVarious reference types supported by this map.protected static classConcurrentReferenceHashMap.RestructureThe types of restructuring that can be performed.protected classConcurrentReferenceHashMap.SegmentA single segment used to divide the map to allow better concurrent performance.从类继承的嵌套类/接口 java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
构造器概要
构造器 构造器 说明 ConcurrentReferenceHashMap()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)protected ConcurrentReferenceHashMap.ReferenceManagercreateReferenceManager()Factory method that returns theConcurrentReferenceHashMap.ReferenceManager.Set<Map.Entry<K,V>>entrySet()Vget(Object key)protected intgetHash(Object o)Get the hash for a given object, apply an additional hash function to reduce collisions.protected floatgetLoadFactor()VgetOrDefault(Object key, V defaultValue)protected ConcurrentReferenceHashMap.Reference<K,V>getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure)Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.protected ConcurrentReferenceHashMap.SegmentgetSegment(int index)protected intgetSegmentsSize()booleanisEmpty()voidpurgeUnreferencedEntries()Remove any entries that have been garbage collected and are no longer referenced.Vput(K key, V value)VputIfAbsent(K key, V value)Vremove(Object key)booleanremove(Object key, Object value)Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)intsize()从类继承的方法 java.util.AbstractMap
clone, containsValue, equals, hashCode, keySet, putAll, toString, values
从接口继承的方法 java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll
构造器详细资料
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
protected final ConcurrentReferenceHashMap.Segment getSegment(int index)
createReferenceManager
protected ConcurrentReferenceHashMap.ReferenceManager createReferenceManager()
Factory method that returns theConcurrentReferenceHashMap.ReferenceManager. This method will be called once for eachConcurrentReferenceHashMap.Segment.- 返回:
- a new reference manager
getHash
protected int getHash(@Nullable Object o)
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
getOrDefault
@Nullable public V getOrDefault(@Nullable Object key, @Nullable V defaultValue)
- 指定者:
getOrDefault在接口中ConcurrentMap<K,V>- 指定者:
getOrDefault在接口中Map<K,V>
containsKey
public boolean containsKey(@Nullable Object key)
- 指定者:
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
putIfAbsent
@Nullable public V putIfAbsent(@Nullable K key, @Nullable V value)
- 指定者:
putIfAbsent在接口中ConcurrentMap<K,V>- 指定者:
putIfAbsent在接口中Map<K,V>
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.
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)