K
- the key typeV
- the value typepublic class ConcurrentReferenceHashMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.util.concurrent.ConcurrentMap<K,V>
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.
Modifier and Type | Class and Description |
---|---|
private class |
ConcurrentReferenceHashMap.Entries
Allows a task access to
ConcurrentReferenceHashMap.Segment entries. |
protected static class |
ConcurrentReferenceHashMap.Entry<K,V>
A single map entry.
|
private class |
ConcurrentReferenceHashMap.EntryIterator
Internal entry iterator implementation.
|
private class |
ConcurrentReferenceHashMap.EntrySet
Internal entry-set implementation.
|
protected static interface |
ConcurrentReferenceHashMap.Reference<K,V>
A reference to an
ConcurrentReferenceHashMap.Entry contained in the map. |
protected class |
ConcurrentReferenceHashMap.ReferenceManager
Strategy class used to manage
ConcurrentReferenceHashMap.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.
|
private static class |
ConcurrentReferenceHashMap.SoftEntryReference<K,V>
Internal
ConcurrentReferenceHashMap.Reference implementation for SoftReference s. |
private class |
ConcurrentReferenceHashMap.Task<T>
A task that can be
run against a ConcurrentReferenceHashMap.Segment . |
private static class |
ConcurrentReferenceHashMap.TaskOption
Various options supported by a
Task . |
private static class |
ConcurrentReferenceHashMap.WeakEntryReference<K,V>
Internal
ConcurrentReferenceHashMap.Reference implementation for WeakReference s. |
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_CONCURRENCY_LEVEL |
private static int |
DEFAULT_INITIAL_CAPACITY |
private static float |
DEFAULT_LOAD_FACTOR |
private static ConcurrentReferenceHashMap.ReferenceType |
DEFAULT_REFERENCE_TYPE |
private java.util.Set<java.util.Map.Entry<K,V>> |
entrySet
Late binding entry set.
|
private float |
loadFactor
When the average number of references per table exceeds this value resize will be attempted.
|
private static int |
MAXIMUM_CONCURRENCY_LEVEL |
private static int |
MAXIMUM_SEGMENT_SIZE |
private ConcurrentReferenceHashMap.ReferenceType |
referenceType
The reference type: SOFT or WEAK.
|
private ConcurrentReferenceHashMap.Segment[] |
segments
Array of segments indexed using the high order bits from the hash.
|
private int |
shift
The shift value used to calculate the size of the segments array and an index from the hash.
|
Constructor and Description |
---|
ConcurrentReferenceHashMap()
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity)
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity,
ConcurrentReferenceHashMap.ReferenceType referenceType)
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity,
float loadFactor)
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel)
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel,
ConcurrentReferenceHashMap.ReferenceType referenceType)
Create a new
ConcurrentReferenceHashMap instance. |
ConcurrentReferenceHashMap(int initialCapacity,
int concurrencyLevel)
Create a new
ConcurrentReferenceHashMap instance. |
Modifier and Type | Method and Description |
---|---|
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(java.lang.Object key) |
protected ConcurrentReferenceHashMap.ReferenceManager |
createReferenceManager()
Factory method that returns the
ConcurrentReferenceHashMap.ReferenceManager . |
private <T> T |
doTask(java.lang.Object key,
ConcurrentReferenceHashMap.Task<T> task) |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
V |
get(java.lang.Object key) |
protected int |
getHash(java.lang.Object o)
Get the hash for a given object, apply an additional hash function to reduce
collisions.
|
protected float |
getLoadFactor() |
protected ConcurrentReferenceHashMap.Reference<K,V> |
getReference(java.lang.Object key,
ConcurrentReferenceHashMap.Restructure restructure)
Return a
ConcurrentReferenceHashMap.Reference to the ConcurrentReferenceHashMap.Entry for the specified key ,
or null if not found. |
protected ConcurrentReferenceHashMap.Segment |
getSegment(int index) |
private ConcurrentReferenceHashMap.Segment |
getSegmentForHash(int hash) |
protected int |
getSegmentsSize() |
void |
purgeUnreferencedEntries()
Remove any entries that have been garbage collected and are no longer referenced.
|
V |
put(K key,
V value) |
private V |
put(K key,
V value,
boolean overwriteExisting) |
V |
putIfAbsent(K key,
V value) |
V |
remove(java.lang.Object key) |
boolean |
remove(java.lang.Object key,
java.lang.Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
int |
size() |
clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
finalize, getClass, notify, notifyAll, wait, wait, wait
private static final int DEFAULT_INITIAL_CAPACITY
private static final float DEFAULT_LOAD_FACTOR
private static final int DEFAULT_CONCURRENCY_LEVEL
private static final ConcurrentReferenceHashMap.ReferenceType DEFAULT_REFERENCE_TYPE
private static final int MAXIMUM_CONCURRENCY_LEVEL
private static final int MAXIMUM_SEGMENT_SIZE
private final ConcurrentReferenceHashMap.Segment[] segments
private final float loadFactor
private final ConcurrentReferenceHashMap.ReferenceType referenceType
private final int shift
public ConcurrentReferenceHashMap()
ConcurrentReferenceHashMap
instance.public ConcurrentReferenceHashMap(int initialCapacity)
ConcurrentReferenceHashMap
instance.initialCapacity
- the initial capacity of the mappublic ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)
ConcurrentReferenceHashMap
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 attemptedpublic ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel)
ConcurrentReferenceHashMap
instance.initialCapacity
- the initial capacity of the mapconcurrencyLevel
- the expected number of threads that will concurrently
write to the mappublic ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType)
ConcurrentReferenceHashMap
instance.initialCapacity
- the initial capacity of the mapreferenceType
- the reference type used for entries (soft or weak)public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
ConcurrentReferenceHashMap
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 mappublic ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType)
ConcurrentReferenceHashMap
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)protected final float getLoadFactor()
protected final int getSegmentsSize()
protected final ConcurrentReferenceHashMap.Segment getSegment(int index)
protected ConcurrentReferenceHashMap.ReferenceManager createReferenceManager()
ConcurrentReferenceHashMap.ReferenceManager
.
This method will be called once for each ConcurrentReferenceHashMap.Segment
.protected int getHash(java.lang.Object o)
ConcurrentHashMap
. Subclasses can override to provide alternative hashing.o
- the object to hash (may be null)public V get(java.lang.Object key)
public boolean containsKey(java.lang.Object key)
protected final ConcurrentReferenceHashMap.Reference<K,V> getReference(java.lang.Object key, ConcurrentReferenceHashMap.Restructure restructure)
ConcurrentReferenceHashMap.Reference
to the ConcurrentReferenceHashMap.Entry
for the specified key
,
or null
if not found.key
- the key (can be null
)restructure
- types of restructure allowed during this callnull
if not foundpublic V remove(java.lang.Object key)
public boolean remove(java.lang.Object key, java.lang.Object value)
public void clear()
public void purgeUnreferencedEntries()
public int size()
private <T> T doTask(java.lang.Object key, ConcurrentReferenceHashMap.Task<T> task)
private ConcurrentReferenceHashMap.Segment getSegmentForHash(int hash)
protected static int calculateShift(int minimumValue, int maximumValue)
minimumValue
- the minimum valuemaximumValue
- the maximum value1 << shift
to obtain a value)