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>

public class ConcurrentReferenceHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
A ConcurrentHashMap variant that uses soft or weak references for its entries.

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.

Note that the soft or weak references are applied to the internal entries of the map, not to the keys and values themselves: each entry holds strong references to its key and value, whereas the map only holds a soft or weak reference to the entry. Consequently, an entry may be discarded even if its key and value are still strongly reachable from elsewhere. In contrast to WeakHashMap, the lifetime of an entry is therefore not tied to the reachability of its key. In particular, with weak references, entries are likely to be removed on the next garbage collection. This makes the map suitable for caches whose entries can be recomputed on demand, but not for associating data with a key for the key's lifetime.

If not explicitly specified, this implementation will use soft entry references.

Since:
3.2
Author:
Phillip Webb, Juergen Hoeller, Brian Clozel
  • Constructor Details

    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap()
      Create a new ConcurrentReferenceHashMap instance.
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity)
      Create a new ConcurrentReferenceHashMap instance.
      Parameters:
      initialCapacity - the initial capacity of the map
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor)
      Create a new ConcurrentReferenceHashMap instance.
      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
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity, int concurrencyLevel)
      Create a new ConcurrentReferenceHashMap instance.
      Parameters:
      initialCapacity - the initial capacity of the map
      concurrencyLevel - the expected number of threads that will concurrently write to the map
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity, ConcurrentReferenceHashMap.ReferenceType referenceType)
      Create a new ConcurrentReferenceHashMap instance.
      Parameters:
      initialCapacity - the initial capacity of the map
      referenceType - the reference type used for entries (soft or weak)
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
      Create a new ConcurrentReferenceHashMap instance.
      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
    • ConcurrentReferenceHashMap

      public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ConcurrentReferenceHashMap.ReferenceType referenceType)
      Create a new ConcurrentReferenceHashMap instance.
      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