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:
java.io.Serializable,java.util.concurrent.ConcurrentMap<K,V>,java.util.Map<K,V>
static class Cache.ConcurrentLinkedHashMap<K,V> extends java.util.AbstractMap<K,V> implements java.util.concurrent.ConcurrentMap<K,V>, java.io.SerializableAConcurrentMapwith a doubly-linked list running through its entries. This class provides the same semantics as aConcurrentHashMapin terms of iterators, acceptable keys, and concurrency characteristics, but perform slightly worse due to the added expense of maintaining the linked list. It differs fromLinkedHashMapin 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.
LinkedHashMapevicting in access order. Ben Manes
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classCache.ConcurrentLinkedHashMap.EntryIteratorAdapterAn adapter to represent the data store's entry iterator in the external type.private classCache.ConcurrentLinkedHashMap.EntrySetAdapterAn adapter to represent the data store's entry set in the external type.static interfaceCache.ConcurrentLinkedHashMap.EvictionListener<K,V>A listener registered for notification when an entry is evicted.static classCache.ConcurrentLinkedHashMap.EvictionPolicyThe replacement policy to apply to determine which entry to discard to when the capacity has been reached.(package private) static classCache.ConcurrentLinkedHashMap.Node<K,V>A node on the double-linked list.private static classCache.ConcurrentLinkedHashMap.SimpleEntry<K,V>This duplicatesAbstractMap.SimpleEntryuntil the class is made accessible.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.concurrent.atomic.AtomicIntegercapacity(package private) java.util.concurrent.ConcurrentMap<K,Cache.ConcurrentLinkedHashMap.Node<K,V>>data(package private) Cache.ConcurrentLinkedHashMap.Node<K,V>head(package private) java.util.concurrent.atomic.AtomicIntegerlength(package private) java.util.List<Cache.ConcurrentLinkedHashMap.EvictionListener<K,V>>listeners(package private) Cache.ConcurrentLinkedHashMap.EvictionPolicypolicyprivate static longserialVersionUID(package private) Cache.ConcurrentLinkedHashMap.Node<K,V>tail
-
Constructor Summary
Constructors Constructor Description 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.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcapacity()Retrieves the maximum capacity of the map.voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.Set<java.util.Map.Entry<K,V>>entrySet()private voidevict()Evicts a single entry if the map exceeds the maximum capacity.Vget(java.lang.Object key)private booleanisOverflow()Determines whether the map has exceeded its capacity.private voidnotifyEviction(K key, V value)Notifies the listeners that an entry was evicted from the map.private voidoffer(Cache.ConcurrentLinkedHashMap.Node<K,V> node)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.Vput(K key, V value)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.VputIfAbsent(K key, V value)Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidsetCapacity(int capacity)Sets the maximum capacity of the map and eagerly evicts entries until the it shrinks to the appropriate size.intsize()-
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
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
listeners
final java.util.List<Cache.ConcurrentLinkedHashMap.EvictionListener<K,V>> listeners
-
data
final java.util.concurrent.ConcurrentMap<K,Cache.ConcurrentLinkedHashMap.Node<K,V>> data
-
capacity
final java.util.concurrent.atomic.AtomicInteger capacity
-
policy
final Cache.ConcurrentLinkedHashMap.EvictionPolicy policy
-
length
final java.util.concurrent.atomic.AtomicInteger length
-
head
final Cache.ConcurrentLinkedHashMap.Node<K,V> head
-
tail
final Cache.ConcurrentLinkedHashMap.Node<K,V> tail
-
-
Constructor Detail
-
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 Detail
-
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
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
evict
private void evict()
Evicts a single entry if the map exceeds the maximum capacity.
-
notifyEviction
private void notifyEviction(K key, V value)
Notifies the listeners that an entry was evicted from the map.- Parameters:
key- The entry's key.value- The entry's value.
-
poll
private Cache.ConcurrentLinkedHashMap.Node<K,V> 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
private void offer(Cache.ConcurrentLinkedHashMap.Node<K,V> node)
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
public V get(java.lang.Object key)
-
remove
public V remove(java.lang.Object key)
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
-