Class ConcurrentKeyLock<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
ConcurrentMap<K,V>, Map<K, V>
ConcurrentMap implementation backed by a ConcurrentHashMap
that provides per-key reentrant locking without intrinsic monitors.
The map operations are delegated to an internal ConcurrentHashMap;
the resource-control-map role only relies on the lock API.
- Since:
- 26.04
- Author:
- Aleks Seovic 2026.04.10
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceObserve contention on key locks.protected static classState for a single key lock.classLive view of a contended key lock.protected static classAReentrantLockthat exposes its owner thread.protected static classThe current thread's global read-lock hold count.Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ReentrantReadWriteLockGlobal read/write lock supportingConcurrentMap.LOCK_ALL.protected final ConcurrentHashMap<K, ConcurrentKeyLock.LockEntry> Per-key lock entries.protected final ConcurrentHashMap<K, V> Map data delegated to an internalConcurrentHashMap.protected final ConcurrentKeyLock.ContentionObserver<K> Optional contention observer.protected final ThreadLocal<ConcurrentKeyLock.ReadLockHold> Per-thread read-lock accounting for held key locks.Fields inherited from interface ConcurrentMap
LOCK_ALL -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an unlockedConcurrentKeyLock.Construct an unlockedConcurrentKeyLock. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacquireReadLock(long cWait, long ldtLimit) Acquire the global read lock.voidclear()Removes all mappings from this map.booleancontainsKey(Object key) Returns true if this map contains a mapping for the specified key.booleancontainsValue(Object value) Returns true if this map maps one or more keys to the specified value.entrySet()Returns the value to which this map maps the specified key.booleanisEmpty()Returns true if this map contains no key-value mappings.booleanAttempt to lock the specified item and return immediately.booleanAttempt to lock the specified item within the specified period of time.protected booleanlockAll(long cWait) Attempt to acquire the global write lock.Associates the specified value with the specified key in this map (optional operation).voidCopies all of the mappings from the specified map to this map (optional operation).protected voidreleaseLockEntry(K key, ConcurrentKeyLock.LockEntry entry) Release the retained reference for the specified key.protected voidRelease one per-key-lock hold against the global read lock.protected longremainingNanos(long cWait, long ldtLimit) Return the remaining time in nanos.Removes the mapping for this key from this map if present (optional operation).protected ConcurrentKeyLock.LockEntryretainLockEntry(K key) Retain the lock entry for the specified key.intsize()Returns the number of key-value mappings in this map.booleanUnlock the specified item.protected booleanRelease the global write lock.Methods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll, values
-
Field Details
-
f_mapValues
Map data delegated to an internalConcurrentHashMap. -
f_mapLocks
Per-key lock entries. -
f_lockAll
Global read/write lock supportingConcurrentMap.LOCK_ALL. -
f_tloReadLockHold
Per-thread read-lock accounting for held key locks. -
f_observer
Optional contention observer.
-
-
Constructor Details
-
ConcurrentKeyLock
public ConcurrentKeyLock()Construct an unlockedConcurrentKeyLock. -
ConcurrentKeyLock
Construct an unlockedConcurrentKeyLock.- Parameters:
observer- the optional contention observer
-
-
Method Details
-
lock
Description copied from interface:ConcurrentMapAttempt to lock the specified item and return immediately.This method behaves exactly as if it simply performs the call lock(oKey, 0).
- Specified by:
lockin interfaceConcurrentMap<K,V> - Parameters:
oKey- key being locked- Returns:
- true if the item was successfully locked; false otherwise
-
lock
Description copied from interface:ConcurrentMapAttempt to lock the specified item within the specified period of time.The item doesn't have to exist to be locked. While the item is locked there is known to be a lock holder which has an exclusive right to modify (calling put and remove methods) that item.
Lock holder is an abstract concept that depends on the ConcurrentMap implementation. For example, holder could be a cluster member or a thread (or both).
Locking strategy may vary for concrete implementations as well. Lock could have an expiration time (this lock is sometimes called a "lease") or be held indefinitely (until the lock holder terminates).
Some implementations may allow the entire map to be locked. If the map is locked in such a way, then only a lock holder is allowed to perform any of the "put" or "remove" operations. Pass the special constant
ConcurrentMap.LOCK_ALLas the oKey parameter to indicate the map lock.- Specified by:
lockin interfaceConcurrentMap<K,V> - Parameters:
oKey- key being lockedcWait- the number of milliseconds to continue trying to obtain a lock; pass zero to return immediately; pass -1 to block the calling thread until the lock could be obtained- Returns:
- true if the item was successfully locked within the specified time; false otherwise
-
unlock
Description copied from interface:ConcurrentMapUnlock the specified item. The item doesn't have to exist to be unlocked. If the item is currently locked, only the holder of the lock could successfully unlock it.- Specified by:
unlockin interfaceConcurrentMap<K,V> - Parameters:
oKey- key being unlocked- Returns:
- true if the item was successfully unlocked; false otherwise
-
entrySet
-
put
Description copied from interface:ConcurrentMapAssociates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced.Some implementations will attempt to obtain a lock for the key (if necessary) before proceeding with the put operation. For such implementations, the specified item has to be either already locked or able to be locked for this operation to succeed.
- Specified by:
putin interfaceConcurrentMap<K,V> - Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
-
get
Description copied from interface:ConcurrentMapReturns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.- Specified by:
getin interfaceConcurrentMap<K,V> - Specified by:
getin interfaceMap<K,V> - Overrides:
getin classAbstractMap<K,V> - Parameters:
key- key whose associated value is to be returned- Returns:
- the value to which this map maps the specified key, or null if the map contains no mapping for this key
- See Also:
-
remove
Description copied from interface:ConcurrentMapRemoves the mapping for this key from this map if present (optional operation).Some implementations will attempt to obtain a lock for the key (if necessary) before proceeding with the remove operation. For such implementations, the specified item has to be either already locked or able to be locked for this operation to succeed.
- Specified by:
removein interfaceConcurrentMap<K,V> - Specified by:
removein interfaceMap<K,V> - Overrides:
removein classAbstractMap<K,V> - Parameters:
key- key whose mapping is to be removed from the map- Returns:
- previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values
-
containsKey
Description copied from interface:ConcurrentMapReturns true if this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceConcurrentMap<K,V> - Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Parameters:
key- key whose presence in this map is to be tested- Returns:
- true if this map contains a mapping for the specified key
-
containsValue
Description copied from interface:ConcurrentMapReturns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the ConcurrentMap interface.- Specified by:
containsValuein interfaceConcurrentMap<K,V> - Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V> - Parameters:
value- value whose presence in this map is to be tested- Returns:
- true if this map maps one or more keys to the specified value
-
size
public int size()Description copied from interface:ConcurrentMapReturns the number of key-value mappings in this map. Note that this number does not include the items that were locked but didn't have corresponding map entries. -
isEmpty
public boolean isEmpty()Description copied from interface:ConcurrentMapReturns true if this map contains no key-value mappings. Note that the map could have some items locked and be empty at the same time. -
putAll
Description copied from interface:ConcurrentMapCopies all of the mappings from the specified map to this map (optional operation). These mappings will replace any mappings that this map had for any of the keys currently in the specified map. -
clear
public void clear()Description copied from interface:ConcurrentMapRemoves all mappings from this map.Some implementations will attempt to lock the entire map (if necessary) before proceeding with the clear operation. For such implementations, the entire map has to be either already locked or able to be locked for this operation to succeed.
-
lockAll
protected boolean lockAll(long cWait) Attempt to acquire the global write lock.- Parameters:
cWait- the wait timeout in millis- Returns:
trueif the write lock was acquired- Implementation Note:
- Callers must not attempt to upgrade from a held per-key lock
to
ConcurrentMap.LOCK_ALLon the same thread; the underlyingReentrantReadWriteLockdoes not support read-to-write upgrades.
-
unlockAll
protected boolean unlockAll()Release the global write lock.- Returns:
trueif the current thread held the global write lock
-
acquireReadLock
protected boolean acquireReadLock(long cWait, long ldtLimit) Acquire the global read lock.- Parameters:
cWait- the wait timeout in millisldtLimit- the absolute deadline in nanos if timed- Returns:
trueif the read lock was acquired
-
releaseReadLock
protected void releaseReadLock()Release one per-key-lock hold against the global read lock. -
remainingNanos
protected long remainingNanos(long cWait, long ldtLimit) Return the remaining time in nanos.- Parameters:
cWait- the original wait in millisldtLimit- the absolute deadline in nanos- Returns:
- the remaining time in nanos
-
retainLockEntry
Retain the lock entry for the specified key.- Parameters:
key- the key- Returns:
- the retained lock entry
-
releaseLockEntry
Release the retained reference for the specified key.- Parameters:
key- the keyentry- the retained lock entry
-