Interface ConcurrentMap<K,​V>

    • 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 locked
        cWait - 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.
        Specified by:
        size in interface Map<K,​V>
        Returns:
        the number of key-value mappings in this map
      • 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.
        Specified by:
        isEmpty in interface Map<K,​V>
        Returns:
        true if this map contains no key-value mappings
      • containsKey

        boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface Map<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 map
        NullPointerException - 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 interface Map<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 interface Map<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 map
        NullPointerException - 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 interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - 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 key
        ClassCastException - if the class of the specified key or value prevents it from being stored in this map
        NullPointerException - 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 interface Map<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 interface Map<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 map
        ConcurrentModificationException - if the lock could not be successfully obtained for some key
        NullPointerException - 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 interface Map<K,​V>
        Throws:
        ConcurrentModificationException - if a lock could not be successfully obtained for some key