Class ConcurrentReferenceHashMap<K,V> 
java.lang.Object
java.util.AbstractMap<K,V>
 
org.springframework.util.ConcurrentReferenceHashMap<K,V> 
- Type Parameters:
- K- the key type
- V- the value type
- All Implemented Interfaces:
- 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.
- Since:
- 3.2
- Author:
- Phillip Webb, Juergen Hoeller, Brian Clozel
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected static final classA single map entry.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.Nested classes/interfaces inherited from class AbstractMapAbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> 
- 
Constructor SummaryConstructorsConstructorDescriptionCreate 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 SummaryModifier and TypeMethodDescriptionprotected 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(@Nullable Object key) protected ConcurrentReferenceHashMap<K,V>.ReferenceManager Factory method that returns theConcurrentReferenceHashMap<K,.V>.ReferenceManager entrySet()protected intGet the hash for a given object, apply an additional hash function to reduce collisions.protected final floatgetOrDefault(@Nullable Object key, @Nullable V defaultValue) protected final @Nullable ConcurrentReferenceHashMap.Reference<K, V> getReference(@Nullable Object key, ConcurrentReferenceHashMap.Restructure restructure) Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.protected final ConcurrentReferenceHashMap<K,V>.Segment getSegment(int index) protected final intbooleanisEmpty()voidRemove any entries that have been garbage collected and are no longer referenced.putIfAbsent(@Nullable K key, @Nullable V value) booleanbooleanintsize()Methods inherited from class AbstractMapclone, containsValue, equals, hashCode, keySet, putAll, toString, valuesMethods inherited from interface ConcurrentMapcompute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll
- 
Constructor Details- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap()Create a newConcurrentReferenceHashMapinstance.
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
 
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
- loadFactor- the load factor. When the average number of references per table exceeds this value resize will be attempted
 
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
- concurrencyLevel- the expected number of threads that will concurrently write to the map
 
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
- referenceType- the reference type used for entries (soft or weak)
 
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
- loadFactor- 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
 
- 
ConcurrentReferenceHashMappublic ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType) Create a newConcurrentReferenceHashMapinstance.- Parameters:
- initialCapacity- the initial capacity of the map
- loadFactor- 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
- referenceType- the reference type used for entries (soft or weak)
 
 
- 
- 
Method Details- 
getLoadFactorprotected final float getLoadFactor()
- 
getSegmentsSizeprotected final int getSegmentsSize()
- 
getSegment
- 
createReferenceManagerFactory method that returns theConcurrentReferenceHashMap<K,. This method will be called once for eachV>.ReferenceManager ConcurrentReferenceHashMap<K,.V>.Segment - Returns:
- a new reference manager
 
- 
getHashGet 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
 
- 
get
- 
getOrDefault- Specified by:
- getOrDefaultin interface- ConcurrentMap<K,- V> 
- Specified by:
- getOrDefaultin interface- Map<K,- V> 
 
- 
containsKey- Specified by:
- containsKeyin interface- Map<K,- V> 
- Overrides:
- containsKeyin class- AbstractMap<K,- V> 
 
- 
getReferenceprotected final @Nullable ConcurrentReferenceHashMap.Reference<K,V> getReference(@Nullable Object key, ConcurrentReferenceHashMap.Restructure restructure) Return aConcurrentReferenceHashMap.Referenceto theConcurrentReferenceHashMap.Entryfor the specifiedkey, ornullif not found.- Parameters:
- key- the key (can be- null)
- restructure- types of restructure allowed during this call
- Returns:
- the reference, or nullif not found
 
- 
put
- 
putIfAbsent- Specified by:
- putIfAbsentin interface- ConcurrentMap<K,- V> 
- Specified by:
- putIfAbsentin interface- Map<K,- V> 
 
- 
remove
- 
remove
- 
replace
- 
replace
- 
clear
- 
purgeUnreferencedEntriespublic 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
- 
isEmpty
- 
entrySet
- 
calculateShiftprotected 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 value
- maximumValue- the maximum value
- Returns:
- the calculated shift (use 1 << shiftto obtain a value)
 
 
-