| java.lang.Object java.util.AbstractMap wicket.util.concurrent.ConcurrentReaderHashMap
ConcurrentReaderHashMap | public class ConcurrentReaderHashMap extends AbstractMap implements Map,Cloneable,Serializable(Code) | | A version of Hashtable that supports mostly-concurrent reading, but exclusive
writing. Because reads are not limited to periods without writes, a
concurrent reader policy is weaker than a classic reader/writer policy, but
is generally faster and allows more concurrency. This class is a good choice
especially for tables that are mainly created by one thread during the
start-up phase of a program, and from then on, are mainly read (with perhaps
occasional additions or removals) in many threads. If you also need
concurrency among writes, consider instead using ConcurrentHashMap.
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.
[
Introduction to this package. ]
|
Inner Class :protected static class Entry implements Map.Entry | |
Inner Class :protected class KeyIterator extends HashIterator | |
Inner Class :protected class ValueIterator extends HashIterator | |
Field Summary | |
final public static int | DEFAULT_INITIAL_CAPACITY The default initial number of table slots for this table (32). | final public static float | DEFAULT_LOAD_FACTOR The default load factor for this table (1.0). | final protected BarrierLock | barrierLock Lock used only for its memory effects. | protected transient int | count The total number of mappings in the hash table. | protected transient Set | entrySet | protected transient Set | keySet | protected transient Object | lastWrite field written to only to guarantee lock ordering. | protected float | loadFactor The load factor for the hash table. | protected transient Entry[] | table The hash table data. | protected int | threshold The table is rehashed when its size exceeds this threshold. | protected transient Collection | values |
Constructor Summary | |
public | ConcurrentReaderHashMap(int initialCapacity, float loadFactor) Constructs a new, empty map with the specified initial capacity and the
specified load factor. | public | ConcurrentReaderHashMap(int initialCapacity) Constructs a new, empty map with the specified initial capacity and
default load factor. | public | ConcurrentReaderHashMap() Constructs a new, empty map with a default initial capacity and load
factor. | public | ConcurrentReaderHashMap(Map t) Constructs a new map with the same mappings as the given map. |
Method Summary | |
public synchronized int | capacity() | public synchronized void | clear() Removes all mappings from this map. | public synchronized Object | clone() Returns a shallow copy of this ConcurrentReaderHashMap
instance: the keys and values themselves are not cloned. | public boolean | contains(Object value) Tests if some key maps into the specified value in this table. | public boolean | containsKey(Object key) Tests if the specified object is a key in this table.
Parameters: key - possible key. | public boolean | containsValue(Object value) Returns true if this map maps one or more keys to the
specified value. | public Enumeration | elements() Returns an enumeration of the values in this table. | public Set | entrySet() Returns a collection view of the mappings contained in this map. | protected boolean | eq(Object x, Object y) Check for equality of non-null references x and y. | protected synchronized boolean | findAndRemoveEntry(Map.Entry entry) | public Object | get(Object key) Returns the value to which the specified key is mapped in this table.
Parameters: key - a key in the table. | final protected Entry[] | getTableForReading() | public synchronized boolean | isEmpty() Returns true if this map contains no key-value mappings. | public Set | keySet() Returns a set view of the keys contained in this map. | public Enumeration | keys() Returns an enumeration of the keys in this table. | public float | loadFactor() | public Object | put(Object key, Object value) Maps the specified key to the specified value
in this table. | public synchronized void | putAll(Map t) Copies all of the mappings from the specified map to this one. | final protected void | recordModification(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. | public Object | remove(Object key) Removes the key (and its corresponding value) from this table. | public synchronized int | size() Returns the number of key-value mappings in this map. | protected Object | sput(Object key, Object value, int hash) Continuation of put(), called only when synch lock is held and
interference has been detected. | protected Object | sremove(Object key, int hash) Continuation of remove(), called only when synch lock is held and
interference has been detected. | public Collection | values() Returns a collection view of the values contained in this map. |
DEFAULT_INITIAL_CAPACITY | final public static int DEFAULT_INITIAL_CAPACITY(Code) | | The default initial number of table slots for this table (32). Used when
not otherwise specified in constructor.
|
DEFAULT_LOAD_FACTOR | final public static float DEFAULT_LOAD_FACTOR(Code) | | The default load factor for this table (1.0). Used when not otherwise
specified in constructor.
|
barrierLock | final protected BarrierLock barrierLock(Code) | | Lock used only for its memory effects.
|
count | protected transient int count(Code) | | The total number of mappings in the hash table.
|
entrySet | protected transient Set entrySet(Code) | | |
lastWrite | protected transient Object lastWrite(Code) | | field written to only to guarantee lock ordering.
|
loadFactor | protected float loadFactor(Code) | | The load factor for the hash table.
|
table | protected transient Entry[] table(Code) | | The hash table data.
|
threshold | protected int threshold(Code) | | The table is rehashed when its size exceeds this threshold. (The value of
this field is always (int)(capacity * loadFactor).)
|
ConcurrentReaderHashMap | public ConcurrentReaderHashMap(int initialCapacity, float loadFactor)(Code) | | Constructs a new, empty map with the specified initial capacity and the
specified load factor.
Parameters: initialCapacity - the initial capacity The actual initial capacity is rounded tothe nearest power of two. Parameters: loadFactor - the load factor of the ConcurrentReaderHashMap throws: IllegalArgumentException - if the initial maximum number of elements is less than zero,or if the load factor is nonpositive. |
ConcurrentReaderHashMap | public ConcurrentReaderHashMap(int initialCapacity)(Code) | | Constructs a new, empty map with the specified initial capacity and
default load factor.
Parameters: initialCapacity - the initial capacity of the ConcurrentReaderHashMap. throws: IllegalArgumentException - if the initial maximum number of elements is less than zero. |
ConcurrentReaderHashMap | public ConcurrentReaderHashMap()(Code) | | Constructs a new, empty map with a default initial capacity and load
factor.
|
ConcurrentReaderHashMap | public ConcurrentReaderHashMap(Map t)(Code) | | Constructs a new map with the same mappings as the given map. The map is
created with a capacity of twice the number of mappings in the given map
or 16 (whichever is greater), and a default load factor.
Parameters: t - |
capacity | public synchronized int capacity()(Code) | | Return the number of slots in this table
number of slots in this table |
clear | public synchronized void clear()(Code) | | Removes all mappings from this map.
|
clone | public synchronized Object clone() throws CloneNotSupportedException(Code) | | Returns a shallow copy of this ConcurrentReaderHashMap
instance: the keys and values themselves are not cloned.
a shallow copy of this map. |
contains | public boolean contains(Object value)(Code) | | Tests if some key maps into the specified value in this table. This
operation is more expensive than the containsKey method.
Note that this method is identical in functionality to containsValue,
(which is part of the Map interface in the collections framework).
Parameters: value - a value to search for. true if and only if some key maps to thevalue argument in this table as determined by theequals method; false otherwise. exception: NullPointerException - if the value is null . See Also: ConcurrentReaderHashMap.containsKey(Object) See Also: ConcurrentReaderHashMap.containsValue(Object) See Also: Map |
containsKey | public boolean containsKey(Object key)(Code) | | Tests if the specified object is a key in this table.
Parameters: key - possible key. true if and only if the specified object is a keyin this table, as determined by the equals method;false otherwise. exception: NullPointerException - if the key is null . See Also: ConcurrentReaderHashMap.contains(Object) |
containsValue | public boolean containsValue(Object value)(Code) | | Returns true if this map maps one or more keys to the
specified value. Note: This method requires a full internal traversal of
the hash table, and so is much slower than method containsKey.
Parameters: value - value whose presence in this map is to be tested. true if this map maps one or more keys to thespecified value. exception: NullPointerException - if the value is null . |
entrySet | public Set entrySet()(Code) | | Returns a collection view of the mappings contained in this map. Each
element in the returned collection is a Map.Entry. The
collection is backed by the map, so changes to the map are reflected in
the collection, and vice-versa. The collection supports element removal,
which removes the corresponding mapping from the map, via the
Iterator.remove, Collection.remove,
removeAll, retainAll, and clear
operations. It does not support the add or addAll
operations.
a collection view of the mappings contained in this map. |
eq | protected boolean eq(Object x, Object y)(Code) | | Check for equality of non-null references x and y.
Parameters: x - Parameters: y - equality |
findAndRemoveEntry | protected synchronized boolean findAndRemoveEntry(Map.Entry entry)(Code) | | Helper method for entrySet.remove
Parameters: entry - true when the element was found and removed. |
getTableForReading | final protected Entry[] getTableForReading()(Code) | | Get ref to table; the reference and the cells it accesses will be at
least as fresh as from last use of barrierLock
table cells |
isEmpty | public synchronized boolean isEmpty()(Code) | | Returns true if this map contains no key-value mappings.
true if this map contains no key-value mappings. |
keySet | public Set keySet()(Code) | | Returns a set view of the keys contained in this map. The set is backed
by the map, so changes to the map are reflected in the set, and
vice-versa. The set supports element removal, which removes the
corresponding mapping from this map, via the Iterator.remove,
Set.remove, removeAll, retainAll, and
clear operations. It does not support the add or
addAll operations.
a set view of the keys contained in this map. |
loadFactor | public float loadFactor()(Code) | | Return the load factor
the load factor |
put | public Object put(Object key, Object value)(Code) | | Maps the specified 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.
Parameters: key - the table key. Parameters: value - the value. the previous value of the specified key in this table, ornull if it did not have one. exception: NullPointerException - if the key or value is null . See Also: Object.equals(Object) See Also: ConcurrentReaderHashMap.get(Object) |
putAll | public synchronized void putAll(Map t)(Code) | | Copies all of the mappings from the specified map to this one.
These mappings replace any mappings that this map had for any of the keys
currently in the specified Map.
Parameters: t - Mappings to be stored in this map. |
recordModification | final protected void recordModification(Object x)(Code) | | Force a memory synchronization that will cause all readers to see table.
Call only when already holding main synch lock.
Parameters: x - |
rehash | protected void rehash()(Code) | | Rehashes the contents of this map into a new table with a larger
capacity. This method is called automatically when the number of keys in
this map exceeds its capacity and load factor.
|
remove | public Object remove(Object key)(Code) | | Removes the key (and its corresponding value) from this table. This
method does nothing if the key is not in the table.
Parameters: key - the key that needs to be removed. the value to which the key had been mapped in this table, ornull if the key did not have a mapping. exception: NullPointerException - if the key is null . |
size | public synchronized int size()(Code) | | Returns the number of key-value mappings in this map.
the number of key-value mappings in this map. |
sput | protected Object sput(Object key, Object value, int hash)(Code) | | Continuation of put(), called only when synch lock is held and
interference has been detected.
Parameters: key - Parameters: value - Parameters: hash - continuation object |
sremove | protected Object sremove(Object key, int hash)(Code) | | Continuation of remove(), called only when synch lock is held and
interference has been detected.
Parameters: key - Parameters: hash - continuation object |
values | public Collection values()(Code) | | Returns a collection view of the values contained in this map. The
collection is backed by the map, so changes to the map are reflected in
the collection, and vice-versa. The collection supports element removal,
which removes the corresponding mapping from this map, via the
Iterator.remove, Collection.remove,
removeAll, retainAll, and clear
operations. It does not support the add or addAll
operations.
a collection view of the values contained in this map. |
|
|