public class ConcurrentReaderHashMap
extends java.util.AbstractMap
implements java.lang.Cloneable, java.io.Serializable
Successful retrievals using get(key) and containsKey(key) usually run without locking. Unsuccessful ones (i.e., when the key is not present) do involve brief synchronization (locking). Also, the size and isEmpty methods are always synchronized.
Because retrieval operations can ordinarily overlap with writing operations (i.e., put, remove, and their derivatives), retrievals can only be guaranteed to return the results of the most recently completed operations holding upon their onset. Retrieval operations may or may not return results reflecting in-progress writing operations. However, the retrieval operations do always return consistent results -- either those holding before any single modification or after it, but never a nonsense result. For aggregate operations such as putAll and clear, concurrent reads may reflect insertion or removal of only some entries. In those rare contexts in which you use a hash table to synchronize operations across threads (for example, to prevent reads until after clears), you should either encase operations in synchronized blocks, or instead use java.util.Hashtable.
This class also supports optional guaranteed
exclusive reads, simply by surrounding a call within a synchronized
block, as in
ConcurrentReaderHashMap t; ... Object v;
synchronized(t) { v = t.get(k); }
But this is not usually necessary in practice. For
example, it is generally inefficient to write:
ConcurrentReaderHashMap t; ... // Inefficient version Object key; ... Object value; ... synchronized(t) { if (!t.containsKey(key)) t.put(key, value); // other code if not previously present } else { // other code if it was previously present } }Instead, if the values are intended to be the same in each case, just take advantage of the fact that put returns null if the key was not previously present:
ConcurrentReaderHashMap t; ... // Use this instead Object key; ... Object value; ... Object oldValue = t.put(key, value); if (oldValue == null) { // other code if not previously present } else { // other code if it was previously present }
Iterators and Enumerations (i.e., those returned by keySet().iterator(), entrySet().iterator(), values().iterator(), keys(), and elements()) return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They will return at most one instance of each element (via next()/nextElement()), but might or might not reflect puts and removes that have been processed since they were created. They do not throw ConcurrentModificationException. However, these iterators are designed to be used by only one thread at a time. Sharing an iterator across multiple threads may lead to unpredictable results if the table is being concurrently modified. Again, you can ensure interference-free iteration by enclosing the iteration in a synchronized block.
This class may be used as a direct replacement for any use of java.util.Hashtable that does not depend on readers being blocked during updates. Like Hashtable but unlike java.util.HashMap, this class does NOT allow null to be used as a key or value. This class is also typically faster than ConcurrentHashMap when there is usually only one thread updating the table, but possibly many retrieving values from it.
Implementation note: A slightly faster implementation of this class will be possible once planned Java Memory Model revisions are in place.
Modifier and Type | Class and Description |
---|---|
protected static class |
ConcurrentReaderHashMap.BarrierLock
A Serializable class for barrier lock
|
protected static class |
ConcurrentReaderHashMap.Entry
ConcurrentReaderHashMap collision list entry.
|
protected class |
ConcurrentReaderHashMap.HashIterator |
protected class |
ConcurrentReaderHashMap.KeyIterator |
protected class |
ConcurrentReaderHashMap.ValueIterator |
Modifier and Type | Field and Description |
---|---|
protected ConcurrentReaderHashMap.BarrierLock |
barrierLock
Lock used only for its memory effects.
|
protected int |
count
The total number of mappings in the hash table.
|
static int |
DEFAULT_INITIAL_CAPACITY
The default initial number of table slots for this table (32).
|
static float |
DEFAULT_LOAD_FACTOR
The default load factor for this table (1.0).
|
protected java.util.Set |
entrySet |
protected java.util.Set |
keySet |
protected java.lang.Object |
lastWrite
field written to only to guarantee lock ordering.
|
protected float |
loadFactor
The load factor for the hash table.
|
protected ConcurrentReaderHashMap.Entry[] |
table
The hash table data.
|
protected int |
threshold
The table is rehashed when its size exceeds this threshold.
|
protected java.util.Collection |
values |
Constructor and Description |
---|
ConcurrentReaderHashMap()
Constructs a new, empty map with a default initial capacity
and load factor.
|
ConcurrentReaderHashMap(int initialCapacity)
Constructs a new, empty map with the specified initial
capacity and default load factor.
|
ConcurrentReaderHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial
capacity and the specified load factor.
|
ConcurrentReaderHashMap(java.util.Map t)
Constructs a new map with the same mappings as the given map.
|
Modifier and Type | Method and Description |
---|---|
int |
capacity() |
void |
clear()
Removes all mappings from this map.
|
java.lang.Object |
clone()
Returns a shallow copy of this
ConcurrentReaderHashMap instance: the keys and
values themselves are not cloned.
|
boolean |
contains(java.lang.Object value)
Tests if some key maps into the specified value in this table.
|
boolean |
containsKey(java.lang.Object key)
Tests if the specified object is a key in this table.
|
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the
specified value.
|
java.util.Enumeration |
elements()
Returns an enumeration of the values in this table.
|
java.util.Set |
entrySet()
Returns a collection view of the mappings contained in this map.
|
protected boolean |
eq(java.lang.Object x,
java.lang.Object y)
Check for equality of non-null references x and y.
|
protected boolean |
findAndRemoveEntry(java.util.Map.Entry entry)
Helper method for entrySet.remove
|
java.lang.Object |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this table.
|
protected ConcurrentReaderHashMap.Entry[] |
getTableForReading()
Get ref to table; the reference and the cells it
accesses will be at least as fresh as from last
use of barrierLock
|
boolean |
isEmpty()
Returns true if this map contains no key-value mappings.
|
java.util.Enumeration |
keys()
Returns an enumeration of the keys in this table.
|
java.util.Set |
keySet()
Returns a set view of the keys contained in this map.
|
float |
loadFactor() |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Maps the specified
key to the specified
value in this table. |
void |
putAll(java.util.Map t)
Copies all of the mappings from the specified map to this one.
|
protected void |
recordModification(java.lang.Object x)
Force a memory synchronization that will cause
all readers to see table.
|
protected void |
rehash()
Rehashes the contents of this map into a new table
with a larger capacity.
|
java.lang.Object |
remove(java.lang.Object key)
Removes the key (and its corresponding value) from this
table.
|
int |
size()
Returns the number of key-value mappings in this map.
|
protected java.lang.Object |
sput(java.lang.Object key,
java.lang.Object value,
int hash)
Continuation of put(), called only when sync lock is
held and interference has been detected.
|
protected java.lang.Object |
sremove(java.lang.Object key,
int hash)
Continuation of remove(), called only when sync lock is
held and interference has been detected.
|
java.util.Collection |
values()
Returns a collection view of the values contained in this map.
|
protected final ConcurrentReaderHashMap.BarrierLock barrierLock
protected transient java.lang.Object lastWrite
public static final int DEFAULT_INITIAL_CAPACITY
public static final float DEFAULT_LOAD_FACTOR
protected transient ConcurrentReaderHashMap.Entry[] table
protected transient int count
protected int threshold
protected float loadFactor
protected transient java.util.Set keySet
protected transient java.util.Set entrySet
protected transient java.util.Collection values
public ConcurrentReaderHashMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity
The actual initial capacity is rounded to the nearest power of two.loadFactor
- the load factor of the ConcurrentReaderHashMapjava.lang.IllegalArgumentException
- if the initial maximum number
of elements is less
than zero, or if the load factor is non-positive.public ConcurrentReaderHashMap(int initialCapacity)
initialCapacity
- the initial capacity of the
ConcurrentReaderHashMap.java.lang.IllegalArgumentException
- if the initial maximum number
of elements is less than zero.public ConcurrentReaderHashMap()
public ConcurrentReaderHashMap(java.util.Map t)
protected final void recordModification(java.lang.Object x)
protected final ConcurrentReaderHashMap.Entry[] getTableForReading()
protected boolean eq(java.lang.Object x, java.lang.Object y)
public int size()
size
in interface java.util.Map
size
in class java.util.AbstractMap
public boolean isEmpty()
isEmpty
in interface java.util.Map
isEmpty
in class java.util.AbstractMap
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
get
in class java.util.AbstractMap
key
- a key in the table.null
if the key is not mapped to any value in
this table.java.lang.NullPointerException
- if the key is null
.put(Object, Object)
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
containsKey
in class java.util.AbstractMap
key
- possible key.true
if and only if the specified object
is a key in this table, as determined by the
equals method; false
otherwise.java.lang.NullPointerException
- if the key is null
.contains(Object)
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
key
to the specified
value
in this table. Neither the key nor the
value can be null
.
The value can be retrieved by calling the get
method
with a key that is equal to the original key.
put
in interface java.util.Map
put
in class java.util.AbstractMap
key
- the table key.value
- the value.null
if it did not have one.java.lang.NullPointerException
- if the key or value is null
.Object.equals(Object)
,
get(Object)
protected java.lang.Object sput(java.lang.Object key, java.lang.Object value, int hash)
protected void rehash()
public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
remove
in class java.util.AbstractMap
key
- the key that needs to be removed.null
if the key did not have a mapping.java.lang.NullPointerException
- if the key is
null
.protected java.lang.Object sremove(java.lang.Object key, int hash)
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
containsValue
in class java.util.AbstractMap
value
- value whose presence in this map is to be tested.java.lang.NullPointerException
- if the value is null
.public boolean contains(java.lang.Object value)
containsKey
method.Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the collections framework).
value
- a value to search for.true
if and only if some key maps to the
value
argument in this table as
determined by the equals method;
false
otherwise.java.lang.NullPointerException
- if the value is null
.containsKey(Object)
,
containsValue(Object)
,
Map
public void putAll(java.util.Map t)
putAll
in interface java.util.Map
putAll
in class java.util.AbstractMap
t
- Mappings to be stored in this map.public void clear()
clear
in interface java.util.Map
clear
in class java.util.AbstractMap
public java.lang.Object clone()
clone
in class java.util.AbstractMap
public java.util.Set keySet()
keySet
in interface java.util.Map
keySet
in class java.util.AbstractMap
public java.util.Collection values()
values
in interface java.util.Map
values
in class java.util.AbstractMap
public java.util.Set entrySet()
entrySet
in interface java.util.Map
entrySet
in class java.util.AbstractMap
protected boolean findAndRemoveEntry(java.util.Map.Entry entry)
public java.util.Enumeration keys()
Enumeration
,
elements()
,
keySet()
,
Map
public java.util.Enumeration elements()
public int capacity()
public float loadFactor()