Top Description Inners Fields Constructors Methods
org.springframework.util

public Class ConcurrentReferenceHashMap<K, V>

extends AbstractMap<K, V>
implements ConcurrentMap<K, V>
Class Inheritance
All Implemented Interfaces
java.util.concurrent.ConcurrentMap, java.util.Map
Type Parameters
<K>
the key type
<V>
the value type
Imports
java.lang.ref.ReferenceQueue, .SoftReference, .WeakReference, java.lang.reflect.Array, java.util.AbstractMap, .AbstractSet, .Collections, .EnumSet, .HashSet, .Iterator, .Map, .NoSuchElementException, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.ReentrantLock, org.springframework.lang.Nullable

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.

Authors
Phillip Webb, Juergen Hoeller, Brian Clozel
Since
3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface
protected static class
ConcurrentReferenceHashMap.Entry<
the key type
K
,
the value type
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<
the key type
K
,
the value type
V
>

A reference to an Entry contained in the map.

protected class
public static enum
ConcurrentReferenceHashMap.ReferenceType

Various reference types supported by this map.

protected static enum
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
private abstract class
ConcurrentReferenceHashMap.Task<T>

A task that can be run against a Segment.

private static enum
ConcurrentReferenceHashMap.TaskOption

Various options supported by a Task.

private static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final float
private static final ConcurrentReferenceHashMap.ReferenceType
private volatile Set<Map.Entry<K, V>>
entrySet

Late binding entry set.

private final float
loadFactor

When the average number of references per table exceeds this value resize will be attempted.

private static final int
private static final int
private final ConcurrentReferenceHashMap.ReferenceType
referenceType

The reference type: SOFT or WEAK.

private final ConcurrentReferenceHashMap<K, V>.Segment[]
segments

Array of segments indexed using the high order bits from the hash.

private final int
shift

The shift value used to calculate the size of the segments array and an index from the hash.

Constructor Summary

AccessConstructor and Description
public
ConcurrentReferenceHashMap()

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
)

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
,
float
the load factor. When the average number of references per table exceeds this value resize will be attempted
loadFactor
)

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
,
int
the expected number of threads that will concurrently write to the map
concurrencyLevel
)

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
,
ConcurrentReferenceHashMap.ReferenceType
the reference type used for entries (soft or weak)
referenceType
)

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
,
float
the load factor. When the average number of references per table exceeds this value, resize will be attempted.
loadFactor
,
int
the expected number of threads that will concurrently write to the map
concurrencyLevel
)

Create a new ConcurrentReferenceHashMap instance.

public
ConcurrentReferenceHashMap(int
the initial capacity of the map
initialCapacity
,
float
the load factor. When the average number of references per table exceeds this value, resize will be attempted.
loadFactor
,
int
the expected number of threads that will concurrently write to the map
concurrencyLevel
,
ConcurrentReferenceHashMap.ReferenceType
the reference type used for entries (soft or weak)
referenceType
)

Create a new ConcurrentReferenceHashMap instance.

Method Summary

Modifier and TypeMethod and Description
protected static int

Returns:

the calculated shift (use 1 << shift to obtain a value)
calculateShift
(int
the minimum value
minimumValue
,
int
the maximum value
maximumValue
)

Calculate a shift value that can be used to create a power-of-two value between the specified maximum and minimum values.

public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear.

Removes all of the mappings from this map (optional operation).

public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.

protected ConcurrentReferenceHashMap<K, V>.ReferenceManager

Returns:

a new reference manager
createReferenceManager
()

Factory method that returns the ReferenceManager.

private <T> T
public Set<Map.Entry<K, V>>
entrySet()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.

public V
get(Object
the key whose associated value is to be returned
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

protected int

Returns:

the resulting hash code
getHash
(Object
the object to hash (may be null)
o
)

Get the hash for a given object, apply an additional hash function to reduce collisions.

protected final float
public V
getOrDefault(Object
the key whose associated value is to be returned
key
,
V
the default mapping of the key
defaultValue
)

Overrides default java.util.concurrent.ConcurrentMap.getOrDefault, java.util.Map.getOrDefault.

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

protected final ConcurrentReferenceHashMap.Reference<K, V>

Returns:

the reference, or null if not found
getReference
(Object
the key (can be null)
key
,
ConcurrentReferenceHashMap.Restructure
types of restructure allowed during this call
restructure
)

Return a Reference to the Entry for the specified key, or null if not found.

protected final ConcurrentReferenceHashMap<K, V>.Segment
getSegment(int index)

private ConcurrentReferenceHashMap<K, V>.Segment
protected final int
public boolean
isEmpty()

Overrides java.util.AbstractMap.isEmpty.

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

public void
purgeUnreferencedEntries()

Remove any entries that have been garbage collected and are no longer referenced.

public V
put(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides java.util.AbstractMap.put.

Implements java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation).

private V
put(final K key, final V value, final boolean overwriteExisting)

public V
putIfAbsent(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides default java.util.Map.putIfAbsent.

Implements java.util.concurrent.ConcurrentMap.putIfAbsent.

If the specified key is not already associated with a value, associates it with the given value.

public V
remove(Object
key whose mapping is to be removed from the map
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation).

public boolean
remove(Object
key with which the specified value is associated
key
,
final Object
value expected to be associated with the specified key
value
)

Overrides default java.util.Map.remove.

Implements java.util.concurrent.ConcurrentMap.remove.

Removes the entry for a key only if currently mapped to a given value.

public boolean
replace(K
key with which the specified value is associated
key
,
final V
value expected to be associated with the specified key
oldValue
,
final V
value to be associated with the specified key
newValue
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replaces the entry for a key only if currently mapped to a given value.

public V
replace(K
key with which the specified value is associated
key
,
final V
value to be associated with the specified key
value
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replaces the entry for a key only if currently mapped to some value.

public int
size()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.

Inherited from java.util.AbstractMap:
clonecontainsValueequalshashCodekeySetputAlltoStringvalues