Package gw.util.concurrent
Class Cache.ConcurrentLinkedHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
gw.util.concurrent.Cache.ConcurrentLinkedHashMap<K,V>
- All Implemented Interfaces:
Serializable
,ConcurrentMap<K,
,V> Map<K,
V>
static class Cache.ConcurrentLinkedHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable
A
ConcurrentMap
with a doubly-linked list running through its entries.
This class provides the same semantics as a ConcurrentHashMap
in terms of
iterators, acceptable keys, and concurrency characteristics, but perform slightly
worse due to the added expense of maintaining the linked list. It differs from
LinkedHashMap
in that it does not provide predictable iteration
order.
This map is intended to be used for caches and provides the following eviction policies:
- First-in, First-out: Also known as insertion order. This policy has excellent concurrency characteristics and an adequate hit rate.
- Second-chance: An enhanced FIFO policy that marks entries that have been retrieved and saves them from being evicted until the next pass. This enhances the FIFO policy by making it aware of "hot" entries, which increases its hit rate to be equal to an LRU's under normal workloads. In the worst case, where all entries have been saved, this policy degrades to a FIFO.
- Least Recently Used: An eviction policy based on the observation that entries that have been used recently will likely be used again soon. This policy provides a good approximation of an optimal algorithm, but suffers by being expensive to maintain. The cost of reordering entries on the list during every access operation reduces the concurrency and performance characteristics of this policy.
LinkedHashMap
evicting in access order.
Ben Manes-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
An adapter to represent the data store's entry iterator in the external type.private final class
An adapter to represent the data store's entry set in the external type.static interface
A listener registered for notification when an entry is evicted.static enum
The replacement policy to apply to determine which entry to discard to when the capacity has been reached.(package private) static final class
A node on the double-linked list.private static final class
This duplicatesAbstractMap.SimpleEntry
until the class is made accessible.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleImmutableEntry<K extends Object,
V extends Object> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final AtomicInteger
(package private) final ConcurrentMap<K,
Cache.ConcurrentLinkedHashMap.Node<K, V>> (package private) final Cache.ConcurrentLinkedHashMap.Node<K,
V> (package private) final AtomicInteger
(package private) final List<Cache.ConcurrentLinkedHashMap.EvictionListener<K,
V>> (package private) final Cache.ConcurrentLinkedHashMap.EvictionPolicy
private static final long
(package private) final Cache.ConcurrentLinkedHashMap.Node<K,
V> -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLinkedHashMap
(Cache.ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, int concurrencyLevel, Cache.ConcurrentLinkedHashMap.EvictionListener<K, V>... listeners) Creates a new, empty, unbounded map with the specified maximum capacity and concurrency level.ConcurrentLinkedHashMap
(Cache.ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, Cache.ConcurrentLinkedHashMap.EvictionListener<K, V>... listeners) Creates a new, empty, unbounded map with the specified maximum capacity and the default concurrencyLevel. -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Retrieves the maximum capacity of the map.void
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
private void
evict()
Evicts a single entry if the map exceeds the maximum capacity.private boolean
Determines whether the map has exceeded its capacity.private void
notifyEviction
(K key, V value) Notifies the listeners that an entry was evicted from the map.private void
Inserts the specified node on to the tail of the list.private Cache.ConcurrentLinkedHashMap.Node<K,
V> poll()
Retrieves and removes the first node on the list or null if empty.private Cache.ConcurrentLinkedHashMap.Node<K,
V> Adds a node to the list and data store if it does not already exist.putIfAbsent
(K key, V value) boolean
boolean
void
setCapacity
(int capacity) Sets the maximum capacity of the map and eagerly evicts entries until the it shrinks to the appropriate size.int
size()
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, 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, getOrDefault, merge, replaceAll
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
listeners
-
data
-
capacity
-
policy
-
length
-
head
-
tail
-
-
Constructor Details
-
ConcurrentLinkedHashMap
public ConcurrentLinkedHashMap(Cache.ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, Cache.ConcurrentLinkedHashMap.EvictionListener<K, V>... listeners) Creates a new, empty, unbounded map with the specified maximum capacity and the default concurrencyLevel.- Parameters:
policy
- The eviction policy to apply when the size exceeds the maximum capacity.maximumCapacity
- The maximum capacity to coerces to. The size may exceed it temporarily.listeners
- The listeners registered for notification when an entry is evicted.
-
ConcurrentLinkedHashMap
public ConcurrentLinkedHashMap(Cache.ConcurrentLinkedHashMap.EvictionPolicy policy, int maximumCapacity, int concurrencyLevel, Cache.ConcurrentLinkedHashMap.EvictionListener<K, V>... listeners) Creates a new, empty, unbounded map with the specified maximum capacity and concurrency level.- Parameters:
policy
- The eviction policy to apply when the size exceeds the maximum capacity.maximumCapacity
- The maximum capacity to coerces to. The size may exceed it temporarily.concurrencyLevel
- The estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.listeners
- The listeners registered for notification when an entry is evicted.
-
-
Method Details
-
isOverflow
private boolean isOverflow()Determines whether the map has exceeded its capacity.- Returns:
- Whether the map has overflowed and an entry should be evicted.
-
setCapacity
public void setCapacity(int capacity) Sets the maximum capacity of the map and eagerly evicts entries until the it shrinks to the appropriate size.- Parameters:
capacity
- The maximum capacity of the map.
-
capacity
public int capacity()Retrieves the maximum capacity of the map.- Returns:
- The maximum capacity.
-
size
public int size() -
clear
public void clear() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractMap<K,
V>
-
evict
private void evict()Evicts a single entry if the map exceeds the maximum capacity. -
notifyEviction
Notifies the listeners that an entry was evicted from the map.- Parameters:
key
- The entry's key.value
- The entry's value.
-
poll
Retrieves and removes the first node on the list or null if empty.- Returns:
- The first node on the list or null if empty.
-
offer
Inserts the specified node on to the tail of the list.- Parameters:
node
- An unlinked node to append to the tail of the list.
-
putIfAbsent
private Cache.ConcurrentLinkedHashMap.Node<K,V> putIfAbsent(Cache.ConcurrentLinkedHashMap.Node<K, V> node) Adds a node to the list and data store if it does not already exist.- Parameters:
node
- An unlinked node to add.- Returns:
- The previous value in the data store.
-
get
-
put
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,
V> - Specified by:
putIfAbsent
in interfaceMap<K,
V>
-
remove
-
remove
-
replace
-
replace
-
entrySet
-