org.springframework.util

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:
    java.util.concurrent.ConcurrentMap<K,V>, java.util.Map<K,V>


    public class ConcurrentReferenceHashMap<K,V>
    extends java.util.AbstractMap<K,V>
    implements java.util.concurrent.ConcurrentMap<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
    • Constructor Detail

      • 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 Detail

      • getLoadFactor

        protected final float getLoadFactor()
      • getSegmentsSize

        protected final int getSegmentsSize()
      • getHash

        protected int getHash(@Nullable
                              java.lang.Object o)
        Get the hash for a given object, apply an additional hash function to reduce collisions. This implementation uses the same Wang/Jenkins algorithm as ConcurrentHashMap. Subclasses can override to provide alternative hashing.
        Parameters:
        o - the object to hash (may be null)
        Returns:
        the resulting hash code
      • get

        @Nullable
        public V get(@Nullable
                               java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,V>
        Overrides:
        get in class java.util.AbstractMap<K,V>
      • getOrDefault

        @Nullable
        public V getOrDefault(@Nullable
                                        java.lang.Object key,
                                        @Nullable
                                        V defaultValue)
        Specified by:
        getOrDefault in interface java.util.concurrent.ConcurrentMap<K,V>
        Specified by:
        getOrDefault in interface java.util.Map<K,V>
      • containsKey

        public boolean containsKey(@Nullable
                                   java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,V>
        Overrides:
        containsKey in class java.util.AbstractMap<K,V>
      • put

        @Nullable
        public V put(@Nullable
                               K key,
                               @Nullable
                               V value)
        Specified by:
        put in interface java.util.Map<K,V>
        Overrides:
        put in class java.util.AbstractMap<K,V>
      • putIfAbsent

        @Nullable
        public V putIfAbsent(@Nullable
                                       K key,
                                       @Nullable
                                       V value)
        Specified by:
        putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,V>
        Specified by:
        putIfAbsent in interface java.util.Map<K,V>
      • remove

        @Nullable
        public V remove(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,V>
        Overrides:
        remove in class java.util.AbstractMap<K,V>
      • remove

        public boolean remove(java.lang.Object key,
                              java.lang.Object value)
        Specified by:
        remove in interface java.util.concurrent.ConcurrentMap<K,V>
        Specified by:
        remove in interface java.util.Map<K,V>
      • replace

        public boolean replace(K key,
                               V oldValue,
                               V newValue)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,V>
        Specified by:
        replace in interface java.util.Map<K,V>
      • replace

        @Nullable
        public V replace(K key,
                                   V value)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,V>
        Specified by:
        replace in interface java.util.Map<K,V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,V>
        Overrides:
        clear in class java.util.AbstractMap<K,V>
      • 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()
        Specified by:
        size in interface java.util.Map<K,V>
        Overrides:
        size in class java.util.AbstractMap<K,V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,V>
        Overrides:
        isEmpty in class java.util.AbstractMap<K,V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,V>
      • 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 value
        maximumValue - the maximum value
        Returns:
        the calculated shift (use 1 << shift to obtain a value)