Class ConcurrentReferenceHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
org.apache.avro.util.springframework.ConcurrentReferenceHashMap<K,V>
- Type Parameters:
K
- the key typeV
- 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
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static final class
A single map entry.protected static interface
A reference to anConcurrentReferenceHashMap.Entry
contained in the map.protected class
Strategy class used to manageReferences
.static enum
Various reference types supported by this map.protected static enum
The types of restructuring that can be performed.protected final class
A single segment used to divide the map to allow better concurrent performance.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorDescriptionCreate 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. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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<K,
V>.ReferenceManager Factory method that returns theConcurrentReferenceHashMap<K,
.V>.ReferenceManager entrySet()
protected int
Get the hash for a given object, apply an additional hash function to reduce collisions.protected final float
getOrDefault
(Object key, V defaultValue) 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.protected final ConcurrentReferenceHashMap<K,
V>.Segment getSegment
(int index) protected final int
boolean
isEmpty()
void
Remove any entries that have been garbage collected and are no longer referenced.putIfAbsent
(K key, V value) boolean
boolean
int
size()
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 Details
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap()Create a newConcurrentReferenceHashMap
instance. -
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity) Create a newConcurrentReferenceHashMap
instance.- Parameters:
initialCapacity
- the initial capacity of the map
-
ConcurrentReferenceHashMap
public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor) Create a newConcurrentReferenceHashMap
instance.- 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 newConcurrentReferenceHashMap
instance.- 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 newConcurrentReferenceHashMap
instance.- 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 newConcurrentReferenceHashMap
instance.- 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 newConcurrentReferenceHashMap
instance.- 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 Details
-
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 - Returns:
- 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.- Parameters:
o
- the object to hash (may be null)- Returns:
- the resulting hash code
-
get
-
getOrDefault
- Specified by:
getOrDefault
in interfaceConcurrentMap<K,
V> - Specified by:
getOrDefault
in interfaceMap<K,
V>
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
getReference
@Nullable protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(@Nullable Object key, ConcurrentReferenceHashMap.Restructure restructure) Return aConcurrentReferenceHashMap.Reference
to theConcurrentReferenceHashMap.Entry
for the specifiedkey
, ornull
if not found.- Parameters:
key
- the key (can benull
)restructure
- types of restructure allowed during this call- Returns:
- the reference, or
null
if not found
-
put
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,
V> - Specified by:
putIfAbsent
in interfaceMap<K,
V>
-
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
-
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 << shift
to obtain a value)
-