org.castor.util.concurrent
Class ConcurrentHashMap
AbstractMap
org.castor.util.concurrent.ConcurrentHashMap
- Cloneable, Map, Serializable
public class ConcurrentHashMap
extends AbstractMap
implements Map, Cloneable, Serializable
A version of Hashtable supporting concurrency for both retrievals and
updates:
- completednot
- concurrency
levelNOT
Because of how concurrency control is split up, the size() and isEmpty()
methods require accumulations across 32 control segments, and so might be
slightly slower than you expect.
This class may be used as a direct replacement for java.util.Hashtable in any
application that does not rely on the ability to lock the entire table to
prevent updates. As of this writing, it performs much faster than Hashtable
in typical multi-threaded applications with multiple readers and writers.
Like Hashtable but unlike java.util.HashMap, this class does NOT allow
null to be used as a key or value.
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. ]
ConcurrentHashMap() - Constructs a new, empty map with a default initial capacity and default
load factor.
|
ConcurrentHashMap(Map t) - Constructs a new map with the same mappings as the given map.
|
ConcurrentHashMap(int initialCapacity) - Constructs a new, empty map with the specified initial capacity and
default load factor.
|
ConcurrentHashMap(int initialCapacity, float loadFactor) - Constructs a new, empty map with the specified initial capacity and the
specified load factor.
|
protected static int | bitcount(int w) - Return the number of set bits in w.
|
void | clear() - Removes all mappings from this map.
|
Object | clone() - Returns a shallow copy of this ConcurrentHashMap instance: the
keys and values themselves are not cloned.
|
boolean | contains(Object value) - Tests if some key maps into the specified value in this table.
|
boolean | containsKey(Object key) - Tests if the specified object is a key in this table.
|
boolean | containsValue(Object value) - Returns true if this map maps one or more keys to the
specified value.
|
Enumeration | elements() - Returns an enumeration of the values in this table.
|
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.
|
Object | get(Object key) - Returns the value to which the specified key is mapped in this table.
|
protected static int | hash(Object x) - Return hash code for Object x.
|
boolean | isEmpty() - Returns true if this map contains no key-value mappings.
|
Set | keySet() - Returns a set view of the keys contained in this map.
|
Enumeration | keys() - Returns an enumeration of the keys in this table.
|
protected ConcurrentHashMap.Entry[] | newTable(int capacity) - Create table array and set the per-segment threshold *
|
Object | put(Object key, Object value) - Maps the specified
key to the specified value
in this table.
|
void | putAll(Map t) - Copies all of the mappings from the specified map to this one.
|
protected void | rehash() - Rehashes the contents of this map into a new table with a larger
capacity.
|
Object | remove(Object key) - Removes the key (and its corresponding value) from this table.
|
protected Object | remove(Object key, Object value) - Removes the (key, value) pair from this table.
|
protected void | resize(int index, ConcurrentHashMap.Entry[] assumedTab) - Gather all locks in order to call rehash, by recursing within synch
blocks for each segment index.
|
int | size() - Returns the number of key-value mappings in this map.
|
Collection | values() - Returns a collection view of the values contained in this map.
|
CONCURRENCY_LEVEL
protected static final int CONCURRENCY_LEVEL
The number of concurrency control segments. The value can be at most 32
since ints are used as bitsets over segments. Emprically, it doesn't seem
to pay to decrease it either, so the value should be at least 32. In
other words, do not redefine this :-)
DEFAULT_INITIAL_CAPACITY
public static int DEFAULT_INITIAL_CAPACITY
The default initial number of table slots for this table (32). Used when
not otherwise specified in constructor.
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTOR
The default load factor for this table (0.75) Used when not otherwise
specified in constructor.
SEGMENT_MASK
protected static final int SEGMENT_MASK
Mask value for indexing into segments
entrySet
protected Set entrySet
keySet
protected Set keySet
loadFactor
protected final float loadFactor
The load factor for the hash table.
threshold
protected int threshold
Per-segment resize threshold.
values
protected Collection values
votesForResize
protected int votesForResize
Number of segments voting for resize. The table is doubled when 1/4 of
the segments reach threshold. Volatile but updated without synch since
this is just a heuristic.
ConcurrentHashMap
public ConcurrentHashMap()
Constructs a new, empty map with a default initial capacity and default
load factor.
ConcurrentHashMap
public ConcurrentHashMap(Map t)
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 32 (whichever is greater), and a default load factor.
ConcurrentHashMap
public ConcurrentHashMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and
default load factor.
initialCapacity
- the initial capacity of the ConcurrentHashMap.
ConcurrentHashMap
public ConcurrentHashMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and the
specified load factor.
initialCapacity
- the initial capacity. The actual initial capacity is rounded
to the nearest power of two.loadFactor
- the load factor threshold, used to control resizing. This
value is used in an approximate way: When at least a quarter
of the segments of the table reach per-segment threshold, or
one of the segments itself exceeds overall threshold, the
table is doubled. This will on average cause resizing when the
table-wide load factor is slightly less than the threshold. If
you'd like to avoid resizing, you can set this to a
ridiculously large value.
bitcount
protected static int bitcount(int w)
Return the number of set bits in w. For a derivation of this algorithm,
see "Algorithms and data structures with applications to graphics and
geometry", by Jurg Nievergelt and Klaus Hinrichs, Prentice Hall, 1993.
See also notes by Torsten Sillke at
http://www.mathematik.uni-bielefeld.de/~sillke/PROBLEMS/bitcount
clear
public void clear()
Removes all mappings from this map.
clone
public Object clone()
Returns a shallow copy of this ConcurrentHashMap instance: the
keys and values themselves are not cloned.
- a shallow copy of this map.
contains
public boolean contains(Object value)
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).
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.
containsKey
public boolean containsKey(Object key)
Tests if the specified object is a key in this table.
true
if and only if the specified object is a key
in this table, as determined by the equals method;
false
otherwise.
containsValue
public boolean containsValue(Object value)
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.
value
- value whose presence in this map is to be tested.
- true if this map maps one or more keys to the
specified value.
elements
public Enumeration elements()
Returns an enumeration of the values in this table. Use the Enumeration
methods on the returned object to fetch the elements sequentially.
- an enumeration of the values in this table.
entrySet
public Set entrySet()
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)
Check for equality of non-null references x and y.
get
public Object get(Object key)
Returns the value to which the specified key is mapped in this table.
key
- a key in the table.
- the value to which the key is mapped in this table;
null
if the key is not mapped to any value in this
table.
hash
protected static int hash(Object x)
Return hash code for Object x. Since we are using power-of-two tables, it
is worth the effort to improve hashcode via the same multiplicative
scheme as used in IdentityHashMap.
isEmpty
public boolean isEmpty()
Returns true if this map contains no key-value mappings.
- true if this map contains no key-value mappings.
keySet
public Set keySet()
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.
keys
public Enumeration keys()
Returns an enumeration of the keys in this table.
- an enumeration of the keys in this table.
newTable
protected ConcurrentHashMap.Entry[] newTable(int capacity)
Create table array and set the per-segment threshold *
put
public Object put(Object key,
Object value)
Maps the specified
key
to the specified
value
in this table. Neither the key nor the value can be
null
.
(Note that this policy is the same as for java.util.Hashtable, but unlike
java.util.HashMap, which does accept nulls as valid keys and values.)
The value can be retrieved by calling the
get
method with
a key that is equal to the original key.
key
- the table key.value
- the value.
- the previous value of the specified key in this table, or
null
if it did not have one.
putAll
public void putAll(Map t)
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.
t
- Mappings to be stored in this map.
rehash
protected void rehash()
Rehashes the contents of this map into a new table with a larger
capacity.
remove
public Object remove(Object key)
Removes the key (and its corresponding value) from this table. This
method does nothing if the key is not in the table.
key
- the key that needs to be removed.
- the value to which the key had been mapped in this table, or
null
if the key did not have a mapping.
remove
protected Object remove(Object key,
Object value)
Removes the (key, value) pair from this table. This method does nothing
if the key is not in the table, or if the key is associated with a
different value. This method is needed by EntrySet.
key
- the key that needs to be removed.value
- the associated value. If the value is null, it means "any
value".
- the value to which the key had been mapped in this table, or
null
if the key did not have a mapping.
resize
protected void resize(int index,
ConcurrentHashMap.Entry[] assumedTab)
Gather all locks in order to call rehash, by recursing within synch
blocks for each segment index.
index
- the current segment. initially call value must be 0assumedTab
- the state of table on first call to resize. If this changes on
any call, the attempt is aborted because the table has already
been resized by another thread.
size
public int size()
Returns the number of key-value mappings in this map.
- the number of key-value mappings in this map.
values
public Collection values()
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.
Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com