Package org.springframework.util
Class ConcurrentReferenceHashMap<K,V>
- java.lang.Object
- java.util.AbstractMap<K,V>
- org.springframework.util.ConcurrentReferenceHashMap<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
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.
- Since:
- 3.2
- Author:
- Phillip Webb, Juergen Hoeller
Nested Class Summary
Nested Classes Modifier and Type Class Description 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 manageConcurrentReferenceHashMap.References.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.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
Constructor Summary
Constructors Constructor Description 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.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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()Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, keySet, putAll, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll
Constructor Detail
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap()
Create a newConcurrentReferenceHashMapinstance.
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity)
Create a newConcurrentReferenceHashMapinstance.- Parameters:
initialCapacity- the initial capacity of the map
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)
Create a newConcurrentReferenceHashMapinstance.- Parameters:
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.- Parameters:
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.- Parameters:
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.- Parameters:
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.- Parameters:
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)
Method Detail
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.- Returns:
- 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.- Parameters:
o- the object to hash (may be null)- Returns:
- the resulting hash code
getOrDefault
public V getOrDefault(Object key, V defaultValue)
- Specified by:
getOrDefaultin interfaceConcurrentMap<K,V>- Specified by:
getOrDefaultin interfaceMap<K,V>
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>- Overrides:
containsKeyin classAbstractMap<K,V>
getReference
protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(Object key, ConcurrentReferenceHashMap.Restructure restructure)
Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.- Parameters:
key- the key (can benull)restructure- types of restructure allowed during this call- Returns:
- the reference, or
nullif not found
putIfAbsent
public V putIfAbsent(K key, V value)
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V>- Specified by:
putIfAbsentin interfaceMap<K,V>
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()
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.- Parameters:
minimumValue- the minimum valuemaximumValue- the maximum value- Returns:
- the calculated shift (use
1 << shiftto obtain a value)