Interface ConcurrentMap<K,V>
-
- Type Parameters:
K
- the type of the map entry keysV
- the type of the map entry values
- All Superinterfaces:
Map<K,V>
- All Known Subinterfaces:
NamedCache<K,V>
,NamedMap<K,V>
,TransactionMap
- All Known Implementing Classes:
BundlingNamedCache
,ContinuousQueryCache
,ConverterCollections.ConverterConcurrentMap
,ConverterCollections.ConverterNamedCache
,NearCache
,ReadonlyNamedCache
,SegmentedConcurrentMap
,VersionedNearCache
,WrapperConcurrentMap
,WrapperNamedCache
public interface ConcurrentMap<K,V> extends Map<K,V>
Map with additional concurrency features.- Author:
- gg 2001.12.16
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clear()
Removes all mappings from this map.boolean
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.boolean
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.V
get(Object key)
Returns the value to which this map maps the specified key.boolean
isEmpty()
Returns true if this map contains no key-value mappings.boolean
lock(Object oKey)
Attempt to lock the specified item and return immediately.boolean
lock(Object oKey, long cWait)
Attempt to lock the specified item within the specified period of time.V
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).void
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map (optional operation).V
remove(Object key)
Removes the mapping for this key from this map if present (optional operation).int
size()
Returns the number of key-value mappings in this map.boolean
unlock(Object oKey)
Unlock the specified item.-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, entrySet, equals, forEach, getOrDefault, hashCode, keySet, merge, putIfAbsent, remove, replace, replace, replaceAll, values
-
-
-
-
Field Detail
-
LOCK_ALL
static final Object LOCK_ALL
Special key value indicating an intent to lock the entire map.
-
-
Method Detail
-
lock
boolean lock(Object oKey, long cWait)
Attempt 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
LOCK_ALL
as the oKey parameter to indicate the map lock.- 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
-
lock
boolean lock(Object oKey)
Attempt to lock the specified item and return immediately.This method behaves exactly as if it simply performs the call lock(oKey, 0).
- Parameters:
oKey
- key being locked- Returns:
- true if the item was successfully locked; false otherwise
-
unlock
boolean unlock(Object oKey)
Unlock 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.- Parameters:
oKey
- key being unlocked- Returns:
- true if the item was successfully unlocked; false otherwise
-
size
int size()
Returns 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
boolean isEmpty()
Returns 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.
-
containsKey
boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.- Specified by:
containsKey
in interfaceMap<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
- Throws:
ClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the key is null and this map does not not permit null keys
-
containsValue
boolean containsValue(Object value)
Returns 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:
containsValue
in interfaceMap<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
-
get
V get(Object key)
Returns 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:
get
in interfaceMap<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
- Throws:
ClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- key is null and this map does not not permit null keys- See Also:
containsKey(Object)
-
put
V put(K key, V value)
Associates 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:
put
in interfaceMap<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
- Throws:
IllegalArgumentException
- if the value cannot be stored in the map (i.e. not serializable)ConcurrentModificationException
- if the lock could not be successfully obtained for the specified keyClassCastException
- if the class of the specified key or value prevents it from being stored in this mapNullPointerException
- this map does not permit null keys or values, and the specified key or value is null
-
remove
V remove(Object key)
Removes 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:
remove
in interfaceMap<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
- Throws:
ConcurrentModificationException
- if the lock could not be successfully obtained for the specified key
-
putAll
void putAll(Map<? extends K,? extends V> map)
Copies 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.- Specified by:
putAll
in interfaceMap<K,V>
- Parameters:
map
- Mappings to be stored in this map- Throws:
ClassCastException
- if the class of a key or value in the specified map prevents it from being stored in this mapConcurrentModificationException
- if the lock could not be successfully obtained for some keyNullPointerException
- this map does not permit null keys or values, and the specified key or value is null
-
clear
void clear()
Removes 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.
- Specified by:
clear
in interfaceMap<K,V>
- Throws:
ConcurrentModificationException
- if a lock could not be successfully obtained for some key
-
-