K
- the type of keys maintained by this mapV
- the type of mapped values@ThreadSafe
public final class ConcurrentLinkedHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable
ConcurrentHashMap
in that it maintains a
page replacement algorithm that is used to evict an entry when the map has
exceeded its capacity. Unlike the Java Collections Framework, this
map does not have a publicly visible constructor and instances are created
through a ConcurrentLinkedHashMap.Builder
.
An entry is evicted from the map when the weighted capacity exceeds
its maximum weighted capacity threshold. A EntryWeigher
determines how many units of capacity that an entry consumes. The default
weigher assigns each value a weight of 1 to bound the map by the
total number of key-value pairs. A map that holds collections may choose to
weigh values by the number of elements in the collection and bound the map
by the total number of elements that it contains. A change to a value that
modifies its weight requires that an update operation is performed on the
map.
An EvictionListener
may be supplied for notification when an entry
is evicted from the map. This listener is invoked on a caller's thread and
will not block other threads from operating on the map. An implementation
should be aware that the caller's thread will not expect long execution
times or failures as a side effect of the listener being notified. Execution
safety and a fast turn around time can be achieved by performing the
operation asynchronously, such as by submitting a task to an
ExecutorService
.
The concurrency level determines the number of threads that can concurrently modify the table. Using a significantly higher or lower value than needed can waste space or lead to thread contention, but an estimate within an order of magnitude of the ideal value does not usually have a noticeable impact. Because placement in hash tables is essentially random, the actual concurrency will vary.
This class and its views and iterators implement all of the
optional methods of the Map
and Iterator
interfaces.
Like Hashtable
but unlike HashMap
, this class
does not allow null to be used as a key or value. Unlike
LinkedHashMap
, this class does not provide
predictable iteration order. A snapshot of the keys and entries may be
obtained in ascending and descending order of retention.
Modifier and Type | Class and Description |
---|---|
(package private) class |
ConcurrentLinkedHashMap.AddTask
Adds the node to the page replacement policy.
|
(package private) static class |
ConcurrentLinkedHashMap.BoundedEntryWeigher<K,V>
A weigher that enforces that the weight falls within a valid range.
|
static class |
ConcurrentLinkedHashMap.Builder<K,V>
A builder that creates
ConcurrentLinkedHashMap instances. |
(package private) static class |
ConcurrentLinkedHashMap.DiscardingListener
A listener that ignores all notifications.
|
(package private) static class |
ConcurrentLinkedHashMap.DiscardingQueue
A queue that discards all additions and is always empty.
|
(package private) static class |
ConcurrentLinkedHashMap.DrainStatus
The draining status of the buffers.
|
(package private) class |
ConcurrentLinkedHashMap.EntryIterator
An adapter to safely externalize the entry iterator.
|
(package private) class |
ConcurrentLinkedHashMap.EntrySet
An adapter to safely externalize the entries.
|
(package private) class |
ConcurrentLinkedHashMap.KeyIterator
An adapter to safely externalize the key iterator.
|
(package private) class |
ConcurrentLinkedHashMap.KeySet
An adapter to safely externalize the keys.
|
(package private) static class |
ConcurrentLinkedHashMap.Node<K,V>
A node contains the key, the weighted value, and the linkage pointers on
the page-replacement algorithm's data structures.
|
(package private) class |
ConcurrentLinkedHashMap.RemovalTask
Removes a node from the page replacement policy.
|
(package private) static class |
ConcurrentLinkedHashMap.SerializationProxy<K,V>
A proxy that is serialized instead of the map.
|
(package private) class |
ConcurrentLinkedHashMap.UpdateTask
Updates the weighted size and evicts an entry on overflow.
|
(package private) class |
ConcurrentLinkedHashMap.ValueIterator
An adapter to safely externalize the value iterator.
|
(package private) class |
ConcurrentLinkedHashMap.Values
An adapter to safely externalize the values.
|
(package private) static class |
ConcurrentLinkedHashMap.WeightedValue<V>
A value, its weight, and the entry's status.
|
(package private) class |
ConcurrentLinkedHashMap.WriteThroughEntry
An entry that allows updates to write through to the map.
|
Modifier and Type | Field and Description |
---|---|
(package private) AtomicLong |
capacity |
(package private) int |
concurrencyLevel |
(package private) ConcurrentMap<K,ConcurrentLinkedHashMap.Node<K,V>> |
data |
(package private) static Queue<?> |
DISCARDING_QUEUE
A queue that discards all entries.
|
(package private) AtomicReference<ConcurrentLinkedHashMap.DrainStatus> |
drainStatus |
(package private) Set<Map.Entry<K,V>> |
entrySet |
(package private) LinkedDeque<ConcurrentLinkedHashMap.Node<K,V>> |
evictionDeque |
(package private) Lock |
evictionLock |
(package private) Set<K> |
keySet |
(package private) EvictionListener<K,V> |
listener |
(package private) static long |
MAXIMUM_CAPACITY
The maximum weighted capacity of the map.
|
(package private) static int |
NCPU
The number of CPUs
|
(package private) static int |
NUMBER_OF_READ_BUFFERS
The number of read buffers to use.
|
(package private) Queue<ConcurrentLinkedHashMap.Node<K,V>> |
pendingNotifications |
(package private) static int |
READ_BUFFER_DRAIN_THRESHOLD
The maximum number of read operations to perform per amortized drain.
|
(package private) static int |
READ_BUFFER_INDEX_MASK
Mask value for indexing into the read buffer.
|
(package private) static int |
READ_BUFFER_SIZE
The maximum number of pending reads per buffer.
|
(package private) static int |
READ_BUFFER_THRESHOLD
The number of pending read operations before attempting to drain.
|
(package private) static int |
READ_BUFFERS_MASK
Mask value for indexing into the read buffers.
|
(package private) AtomicLong[] |
readBufferDrainAtWriteCount |
(package private) long[] |
readBufferReadCount |
(package private) AtomicReference<ConcurrentLinkedHashMap.Node<K,V>>[][] |
readBuffers |
(package private) AtomicLong[] |
readBufferWriteCount |
(package private) static long |
serialVersionUID |
(package private) Collection<V> |
values |
(package private) EntryWeigher<? super K,? super V> |
weigher |
(package private) AtomicLong |
weightedSize |
(package private) static int |
WRITE_BUFFER_DRAIN_THRESHOLD
The maximum number of write operations to perform per amortized drain.
|
(package private) Queue<Runnable> |
writeBuffer |
Modifier and Type | Method and Description |
---|---|
(package private) void |
afterRead(ConcurrentLinkedHashMap.Node<K,V> node)
Performs the post-processing work required after a read.
|
(package private) void |
afterWrite(Runnable task)
Performs the post-processing work required after a write.
|
(package private) void |
applyRead(ConcurrentLinkedHashMap.Node<K,V> node)
Updates the node's location in the page replacement policy.
|
Set<K> |
ascendingKeySet()
Returns a unmodifiable snapshot
Set view of the keys contained in
this map. |
Set<K> |
ascendingKeySetWithLimit(int limit)
Returns an unmodifiable snapshot
Set view of the keys contained in
this map. |
Map<K,V> |
ascendingMap()
Returns an unmodifiable snapshot
Map view of the mappings contained
in this map. |
Map<K,V> |
ascendingMapWithLimit(int limit)
Returns an unmodifiable snapshot
Map view of the mappings contained
in this map. |
long |
capacity()
Retrieves the maximum weighted capacity of the map.
|
(package private) static int |
ceilingNextPowerOfTwo(int x) |
(package private) static void |
checkArgument(boolean expression)
Ensures that the argument expression is true.
|
(package private) static void |
checkNotNull(Object o)
Ensures that the object is not null.
|
(package private) static void |
checkState(boolean expression)
Ensures that the state expression is true.
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<K> |
descendingKeySet()
Returns an unmodifiable snapshot
Set view of the keys contained in
this map. |
Set<K> |
descendingKeySetWithLimit(int limit)
Returns an unmodifiable snapshot
Set view of the keys contained in
this map. |
Map<K,V> |
descendingMap()
Returns an unmodifiable snapshot
Map view of the mappings contained
in this map. |
Map<K,V> |
descendingMapWithLimit(int limit)
Returns an unmodifiable snapshot
Map view of the mappings contained
in this map. |
(package private) void |
drainBuffers()
Drains the read and write buffers up to an amortized threshold.
|
(package private) void |
drainOnReadIfNeeded(int bufferIndex,
long writeCount)
Attempts to drain the buffers if it is determined to be needed when
post-processing a read.
|
(package private) void |
drainReadBuffer(int bufferIndex)
Drains the read buffer up to an amortized threshold.
|
(package private) void |
drainReadBuffers()
Drains the read buffers, each up to an amortized threshold.
|
(package private) void |
drainWriteBuffer()
Drains the read buffer up to an amortized threshold.
|
Set<Map.Entry<K,V>> |
entrySet() |
(package private) void |
evict()
Evicts entries from the map while it exceeds the capacity and appends
evicted entries to the notification queue for processing.
|
V |
get(Object key) |
V |
getQuietly(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key. |
(package private) boolean |
hasOverflowed()
Determines whether the map has exceeded its capacity.
|
boolean |
isEmpty() |
Set<K> |
keySet() |
(package private) void |
makeDead(ConcurrentLinkedHashMap.Node<K,V> node)
Atomically transitions the node to the dead state and decrements
the weightedSize.
|
(package private) void |
makeRetired(ConcurrentLinkedHashMap.Node<K,V> node)
Atomically transitions the node from the alive state to the
retired state, if a valid transition.
|
(package private) void |
notifyListener()
Notifies the listener of entries that were evicted.
|
(package private) Set<K> |
orderedKeySet(boolean ascending,
int limit) |
(package private) Map<K,V> |
orderedMap(boolean ascending,
int limit) |
V |
put(K key,
V value) |
(package private) V |
put(K key,
V value,
boolean onlyIfAbsent)
Adds a node to the list and the data store.
|
V |
putIfAbsent(K key,
V value) |
(package private) static int |
readBufferIndex()
Returns the index to the read buffer to record into.
|
(package private) long |
recordRead(int bufferIndex,
ConcurrentLinkedHashMap.Node<K,V> node)
Records a read in the buffer and return its write count.
|
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
void |
setCapacity(long capacity)
Sets the maximum weighted capacity of the map and eagerly evicts entries
until it shrinks to the appropriate size.
|
int |
size() |
(package private) void |
tryToDrainBuffers()
Attempts to acquire the eviction lock and apply the pending operations, up
to the amortized threshold, to the page replacement policy.
|
(package private) boolean |
tryToRetire(ConcurrentLinkedHashMap.Node<K,V> node,
ConcurrentLinkedHashMap.WeightedValue<V> expect)
Attempts to transition the node from the alive state to the
retired state.
|
Collection<V> |
values() |
long |
weightedSize()
Returns the weighted size of this map.
|
(package private) Object |
writeReplace() |
static final int NCPU
static final long MAXIMUM_CAPACITY
static final int NUMBER_OF_READ_BUFFERS
static final int READ_BUFFERS_MASK
static final int READ_BUFFER_THRESHOLD
static final int READ_BUFFER_DRAIN_THRESHOLD
static final int READ_BUFFER_SIZE
static final int READ_BUFFER_INDEX_MASK
static final int WRITE_BUFFER_DRAIN_THRESHOLD
static final Queue<?> DISCARDING_QUEUE
final ConcurrentMap<K,ConcurrentLinkedHashMap.Node<K,V>> data
final int concurrencyLevel
final long[] readBufferReadCount
final LinkedDeque<ConcurrentLinkedHashMap.Node<K,V>> evictionDeque
final AtomicLong weightedSize
final AtomicLong capacity
final Lock evictionLock
final Queue<Runnable> writeBuffer
final AtomicLong[] readBufferWriteCount
final AtomicLong[] readBufferDrainAtWriteCount
final AtomicReference<ConcurrentLinkedHashMap.Node<K,V>>[][] readBuffers
final AtomicReference<ConcurrentLinkedHashMap.DrainStatus> drainStatus
final EntryWeigher<? super K,? super V> weigher
final Queue<ConcurrentLinkedHashMap.Node<K,V>> pendingNotifications
final EvictionListener<K,V> listener
transient Set<K> keySet
transient Collection<V> values
static final long serialVersionUID
static int ceilingNextPowerOfTwo(int x)
static void checkNotNull(Object o)
static void checkArgument(boolean expression)
static void checkState(boolean expression)
public long capacity()
public void setCapacity(long capacity)
capacity
- the maximum weighted capacity of the mapIllegalArgumentException
- if the capacity is negativeboolean hasOverflowed()
void evict()
void afterRead(ConcurrentLinkedHashMap.Node<K,V> node)
node
- the entry in the page replacement policystatic int readBufferIndex()
long recordRead(int bufferIndex, ConcurrentLinkedHashMap.Node<K,V> node)
bufferIndex
- the index to the chosen read buffernode
- the entry in the page replacement policyvoid drainOnReadIfNeeded(int bufferIndex, long writeCount)
bufferIndex
- the index to the chosen read bufferwriteCount
- the number of writes on the chosen read buffervoid afterWrite(Runnable task)
task
- the pending operation to be appliedvoid tryToDrainBuffers()
void drainBuffers()
void drainReadBuffers()
void drainReadBuffer(int bufferIndex)
void applyRead(ConcurrentLinkedHashMap.Node<K,V> node)
void drainWriteBuffer()
boolean tryToRetire(ConcurrentLinkedHashMap.Node<K,V> node, ConcurrentLinkedHashMap.WeightedValue<V> expect)
node
- the entry in the page replacement policyexpect
- the expected weighted valuevoid makeRetired(ConcurrentLinkedHashMap.Node<K,V> node)
node
- the entry in the page replacement policyvoid makeDead(ConcurrentLinkedHashMap.Node<K,V> node)
node
- the entry in the page replacement policyvoid notifyListener()
public boolean isEmpty()
public int size()
public long weightedSize()
public void clear()
public boolean containsKey(Object key)
public boolean containsValue(Object value)
public V get(Object key)
public V getQuietly(Object key)
null
if this map contains no mapping for the key. This method differs from
get(Object)
in that it does not record the operation with the
page replacement policy.key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyNullPointerException
- if the specified key is nullV put(K key, V value, boolean onlyIfAbsent)
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyonlyIfAbsent
- a write is performed only if the key is not already
associated with a valuepublic V remove(Object key)
public boolean remove(Object key, Object value)
public Set<K> keySet()
public Set<K> ascendingKeySet()
Set
view of the keys contained in
this map. The set's iterator returns the keys whose order of iteration is
the ascending order in which its entries are considered eligible for
retention, from the least-likely to be retained to the most-likely.
Beware that, unlike in keySet()
, obtaining the set is NOT
a constant-time operation. Because of the asynchronous nature of the page
replacement policy, determining the retention ordering requires a traversal
of the keys.
public Set<K> ascendingKeySetWithLimit(int limit)
Set
view of the keys contained in
this map. The set's iterator returns the keys whose order of iteration is
the ascending order in which its entries are considered eligible for
retention, from the least-likely to be retained to the most-likely.
Beware that, unlike in keySet()
, obtaining the set is NOT
a constant-time operation. Because of the asynchronous nature of the page
replacement policy, determining the retention ordering requires a traversal
of the keys.
limit
- the maximum size of the returned setIllegalArgumentException
- if the limit is negativepublic Set<K> descendingKeySet()
Set
view of the keys contained in
this map. The set's iterator returns the keys whose order of iteration is
the descending order in which its entries are considered eligible for
retention, from the most-likely to be retained to the least-likely.
Beware that, unlike in keySet()
, obtaining the set is NOT
a constant-time operation. Because of the asynchronous nature of the page
replacement policy, determining the retention ordering requires a traversal
of the keys.
public Set<K> descendingKeySetWithLimit(int limit)
Set
view of the keys contained in
this map. The set's iterator returns the keys whose order of iteration is
the descending order in which its entries are considered eligible for
retention, from the most-likely to be retained to the least-likely.
Beware that, unlike in keySet()
, obtaining the set is NOT
a constant-time operation. Because of the asynchronous nature of the page
replacement policy, determining the retention ordering requires a traversal
of the keys.
limit
- the maximum size of the returned setIllegalArgumentException
- if the limit is negativeSet<K> orderedKeySet(boolean ascending, int limit)
public Collection<V> values()
public Map<K,V> ascendingMap()
Map
view of the mappings contained
in this map. The map's collections return the mappings whose order of
iteration is the ascending order in which its entries are considered
eligible for retention, from the least-likely to be retained to the
most-likely.
Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
public Map<K,V> ascendingMapWithLimit(int limit)
Map
view of the mappings contained
in this map. The map's collections return the mappings whose order of
iteration is the ascending order in which its entries are considered
eligible for retention, from the least-likely to be retained to the
most-likely.
Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
limit
- the maximum size of the returned mapIllegalArgumentException
- if the limit is negativepublic Map<K,V> descendingMap()
Map
view of the mappings contained
in this map. The map's collections return the mappings whose order of
iteration is the descending order in which its entries are considered
eligible for retention, from the most-likely to be retained to the
least-likely.
Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
public Map<K,V> descendingMapWithLimit(int limit)
Map
view of the mappings contained
in this map. The map's collections return the mappings whose order of
iteration is the descending order in which its entries are considered
eligible for retention, from the most-likely to be retained to the
least-likely.
Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
limit
- the maximum size of the returned mapIllegalArgumentException
- if the limit is negativeObject writeReplace()
Copyright © 2010-2020 Toolsverse. All Rights Reserved.