K - the type of keys maintained by this mapV - the type of mapped valuespublic class BlockingHashMap<K,V> extends Object implements BlockingMap<K,V>
This is similar to unbounded buffer in which the synchronizer elements are inserted by producers and extracted by consumers. The only twist is that each product has a key and consumers know which product they are interested in. Attempts to put/offer an element into the map will always succeed because this is an unbound map; attempts to take element corresponding to a key that is not available on the map will block.
This map can be shutdown using clear. All consumers blocked on the map while invoking clear will be throw InterruptedException or return with null. Attempting any operation after shutdown will throw IllegalStateException.
This class implements some of optional methods of the Map.
| Constructor and Description |
|---|
BlockingHashMap() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Shuts down this blocking map and removes all mappings from this map.The map
will be empty after this call.
|
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.
|
Set<Map.Entry<K,V>> |
entrySet()
Not supported Semantics of addition/removal to map outside the
producer/consumer methods not defined
|
V |
get(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
boolean |
isKeyAvailable(K key)
Returns true if this map contains a mapping for the specified
key.
|
Set<K> |
keySet()
Not supported Semantics of addition/removal to map outside the
producer/consumer methods not defined
|
V |
offer(K key,
V value)
Associates the specified value with the specified key in this map.
|
V |
offer(K key,
V value,
long timeout,
TimeUnit unit)
Associates the specified value with the specified key in this map.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
void |
putAll(Map<? extends K,? extends V> m)
To be supported
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present.
|
int |
size()
Returns the number of key-value mappings in this map
|
V |
take(K key)
Retrieves and removes the mapping for a key from this map if it is
present, waiting if necessary until the mapping becomes available.
|
V |
take(K key,
long timeout,
TimeUnit unit)
Retrieves and removes the mapping for a key from this map if it is
present, waiting if necessary until the mapping becomes available or the
specified time elapses.
|
Collection<V> |
values()
Not supported Semantics of addition/removal to map outside the
producer/consumer methods not defined
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAllpublic boolean isKeyAvailable(K key)
isKeyAvailable in interface BlockingMap<K,V>key - key whose presence in this map is to be testedClassCastException - if the key is of an inappropriate type for
this mapNullPointerException - if the specified key is nullIllegalStateException - if the map has been shut-downpublic boolean containsKey(Object key)
containsKey in interface Map<K,V>key - key whose presence in this map is to be testedClassCastException - if the key is of an inappropriate type for
this map (optional)NullPointerException - if the specified key is null and this map
does not permit null keys (optional)IllegalStateException - if the map has been shut-downpublic V get(Object key)
null
if this map contains no mapping for the key.
Note that null is used as a special marker to indicate the
absence of the requested key
get in interface Map<K,V>get in interface BlockingMap<K,V>key - the key whose associated value is to be returnednull
if this map contains no mapping for the keyClassCastException - if the key is of an inappropriate type for
this mapNullPointerException - if the specified key is null and this map
does not permit null keys (optional)IllegalStateException - if the map has been shut-downpublic V put(K key, V value)
If the Map is bounded and there is no space to put the new mapping, this method returns with null. put on an unbound map will always succeed
Producers cannot put on a key that is already available on the map. Attempts to put a mapping whose key is already available on the map are ignored. However, the same mapping can be put in to the map after it is taken by consumer(s)
put in interface Map<K,V>put in interface BlockingMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keyUnsupportedOperationException - if the put operation is
not supported by this mapClassCastException - if the class of the specified key or value
prevents it from being stored in this mapNullPointerException - if the specified key or value is null and
this map does not permit null keys or valuesIllegalArgumentException - if some property of the specified key or
value prevents it from being stored in this mappublic V remove(Object key)
Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
The map will not contain a mapping for the specified key once the call returns.
remove in interface Map<K,V>remove in interface BlockingMap<K,V>key - key whose mapping is to be removed from the mapUnsupportedOperationException - if the remove operation is
not supported by this mapClassCastException - if the key is of an inappropriate type for
this map (optional)NullPointerException - if the specified key is null and this map
does not permit null keys (optional)IllegalStateException - if the map has been shut-downpublic V offer(K key, V value) throws InterruptedException
If the Map is bounded and there is no space to put the new mapping, this method blocks till space becomes available. offer on an unbound map will always succeed
Producers cannot offer a mapping on a key that is already available on the map. Attempts to such a mapping are ignored. However, the same mapping can be successfully offered after the existing mapping is taken by consumer(s)
offer in interface BlockingMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keyInterruptedException - if interrupted while waitingClassCastException - if the class of the specified element prevents
it from being added to this queueNullPointerException - if the specified element is nullIllegalArgumentException - if some property of the specified
element prevents it from being added to this queuepublic V take(K key) throws InterruptedException
take in interface BlockingMap<K,V>key - key whose mapping is to be removed from the mapUnsupportedOperationException - if the remove operation is
not supported by this mapClassCastException - if the key is of an inappropriate type for
this map (optional)NullPointerException - if the specified key is null and this map
does not permit null keys (optional)InterruptedException - if interrupted while waitingIllegalStateException - if the map has been shut-downpublic V offer(K key, V value, long timeout, TimeUnit unit) throws InterruptedException
If the Map is bounded and there is no space to put the new mapping, this method blocks till space becomes available or the specified time elapses. offer on an unbound map will always succeed
Producers cannot offer a mapping on a key that is already available on the map. Attempts to such a mapping are ignored. However, the same mapping can be successfully offered after the existing mapping is taken by consumer(s)
offer in interface BlockingMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keytimeout - how long to wait before giving up, in units of
unitunit - a TimeUnit determining how to interpret the
timeout parameterInterruptedException - if interrupted while waitingClassCastException - if the class of the specified element prevents
it from being added to this queueNullPointerException - if the specified element is nullIllegalArgumentException - if some property of the specified
element prevents it from being added to this queueIllegalStateException - if the map has been shut-downpublic V take(K key, long timeout, TimeUnit unit) throws InterruptedException
take in interface BlockingMap<K,V>key - key with which the specified value is to be associatedtimeout - how long to wait before giving up, in units of
unitunit - a TimeUnit determining how to interpret theUnsupportedOperationException - if the remove operation is
not supported by this mapClassCastException - if the key is of an inappropriate type for
this map (optional)NullPointerException - if the specified key is null and this map
does not permit null keys (optional)InterruptedException - if interrupted while waitingIllegalStateException - if the map has been shut-downpublic void clear()
Interrupts any threads waiting on any key in map before clearing. This is done to prevent threads being blocked forever
clear in interface Map<K,V>IllegalStateException - if the map has been shut-downpublic boolean containsValue(Object value)
containsValue in interface Map<K,V>value - value whose presence in this map is to be testedIllegalStateException - if the map has been shut-downUnsupportedOperationException - if map is in passive statepublic boolean isEmpty()
public int size()
size in interface Map<K,V>IllegalStateException - if the map has been shut-downpublic Set<Map.Entry<K,V>> entrySet()
entrySet in interface Map<K,V>IllegalStateException - if the map has been shut-downUnsupportedOperationException - if map is in passive statepublic Set<K> keySet()
keySet in interface Map<K,V>IllegalStateException - if the map has been shut-downUnsupportedOperationException - if map is in passive statepublic Collection<V> values()
values in interface Map<K,V>IllegalStateException - if the map has been shut-downUnsupportedOperationException - if map is in passive statepublic void putAll(Map<? extends K,? extends V> m)
putAll in interface Map<K,V>UnsupportedOperationException - if map is in passive stateIllegalStateException - if the map has been shut-downCopyright © 2018. All rights reserved.