类 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>
AConcurrentHashMap
that usessoft
or weak references for bothkeys
andvalues
.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 asConcurrentHashMap
with the exception thatnull
values andnull
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.
- 从以下版本开始:
- 3.2
- 作者:
- Phillip Webb, Juergen Hoeller
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static class
ConcurrentReferenceHashMap.Entry<K,V>
A single map entry.protected static interface
ConcurrentReferenceHashMap.Reference<K,V>
A reference to anConcurrentReferenceHashMap.Entry
contained in the map.protected class
ConcurrentReferenceHashMap.ReferenceManager
Strategy class used to manageConcurrentReferenceHashMap.Reference
s.static class
ConcurrentReferenceHashMap.ReferenceType
Various reference types supported by this map.protected static class
ConcurrentReferenceHashMap.Restructure
The types of restructuring that can be performed.protected class
ConcurrentReferenceHashMap.Segment
A 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 newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity)
Create a newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)
Create a newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
Create a newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType)
Create a newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel)
Create a newConcurrentReferenceHashMap
instance.ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType)
Create a newConcurrentReferenceHashMap
instance.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 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.void
clear()
boolean
containsKey(Object key)
protected ConcurrentReferenceHashMap.ReferenceManager
createReferenceManager()
Factory method that returns theConcurrentReferenceHashMap.ReferenceManager
.Set<Map.Entry<K,V>>
entrySet()
V
get(Object key)
protected int
getHash(Object o)
Get the hash for a given object, apply an additional hash function to reduce collisions.protected float
getLoadFactor()
V
getOrDefault(Object key, V defaultValue)
protected ConcurrentReferenceHashMap.Reference<K,V>
getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure)
Return aConcurrentReferenceHashMap.Reference
to theConcurrentReferenceHashMap.Entry
for the specifiedkey
, ornull
if not found.protected ConcurrentReferenceHashMap.Segment
getSegment(int index)
protected int
getSegmentsSize()
boolean
isEmpty()
void
purgeUnreferencedEntries()
Remove any entries that have been garbage collected and are no longer referenced.V
put(K key, V value)
V
putIfAbsent(K key, V value)
V
remove(Object key)
boolean
remove(Object key, Object value)
V
replace(K key, V value)
boolean
replace(K key, V oldValue, V newValue)
int
size()
从类继承的方法 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 newConcurrentReferenceHashMap
instance.
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity)
Create a newConcurrentReferenceHashMap
instance.- 参数:
initialCapacity
- the initial capacity of the map
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)
Create a newConcurrentReferenceHashMap
instance.- 参数:
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 newConcurrentReferenceHashMap
instance.- 参数:
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 newConcurrentReferenceHashMap
instance.- 参数:
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 newConcurrentReferenceHashMap
instance.- 参数:
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 newConcurrentReferenceHashMap
instance.- 参数:
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(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
public V getOrDefault(Object key, V defaultValue)
- 指定者:
getOrDefault
在接口中ConcurrentMap<K,V>
- 指定者:
getOrDefault
在接口中Map<K,V>
containsKey
public boolean containsKey(Object key)
- 指定者:
containsKey
在接口中Map<K,V>
- 覆盖:
containsKey
在类中AbstractMap<K,V>
getReference
protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure)
Return aConcurrentReferenceHashMap.Reference
to theConcurrentReferenceHashMap.Entry
for the specifiedkey
, ornull
if not found.- 参数:
key
- the key (can benull
)restructure
- types of restructure allowed during this call- 返回:
- the reference, or
null
if not found
putIfAbsent
public V putIfAbsent(K key, 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 << shift
to obtain a value)