|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractMap org.jdiagnose.concurrent.ConcurrentHashMap
A version of Hashtable supporting concurrency for both retrievals and updates:
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. Passing an iterator across multiple threads may lead to unpredictable results if the table is being concurrently modified.
There is NOT any support for locking the entire table to prevent updates. This makes it imposssible, for example, to add an element only if it is not already present, since another thread may be in the process of doing the same thing. If you need such capabilities, consider instead using the ConcurrentReaderHashMap class.
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. ]
Nested Class Summary | |
protected static class |
ConcurrentHashMap.Entry
ConcurrentHashMap collision list entry. |
protected class |
ConcurrentHashMap.HashIterator
|
protected class |
ConcurrentHashMap.KeyIterator
|
protected static class |
ConcurrentHashMap.Segment
Bookkeeping for each concurrency control segment. |
protected class |
ConcurrentHashMap.ValueIterator
|
Field Summary | |
protected static int |
CONCURRENCY_LEVEL
The number of concurrency control segments. |
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 (0.75) Used when not otherwise specified in constructor. |
protected java.util.Set |
entrySet
|
protected java.util.Set |
keySet
|
protected float |
loadFactor
The load factor for the hash table. |
protected static int |
SEGMENT_MASK
Mask value for indexing into segments |
protected ConcurrentHashMap.Segment[] |
segments
The array of concurrency control segments. |
protected ConcurrentHashMap.Entry[] |
table
The hash table data. |
protected int |
threshold
Per-segment resize threshold. |
protected java.util.Collection |
values
|
protected int |
votesForResize
Number of segments voting for resize. |
Constructor Summary | |
ConcurrentHashMap()
Constructs a new, empty map with a default initial capacity and default load factor. |
|
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. |
|
ConcurrentHashMap(java.util.Map t)
Constructs a new map with the same mappings as the given map. |
Method Summary | |
protected static int |
bitcount(int w)
Return the number of set bits in w. |
void |
clear()
Removes all mappings from this map. |
java.lang.Object |
clone()
Returns a shallow copy of this ConcurrentHashMap 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. |
java.lang.Object |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this table. |
protected static int |
hash(java.lang.Object x)
Return hash code for Object x. |
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. |
protected ConcurrentHashMap.Entry[] |
newTable(int capacity)
Create table array and set the per-segment threshold |
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 |
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. |
protected java.lang.Object |
remove(java.lang.Object key,
java.lang.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. |
java.util.Collection |
values()
Returns a collection view of the values contained in this map. |
Methods inherited from class java.util.AbstractMap |
equals, hashCode, toString |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
equals, hashCode |
Field Detail |
protected transient ConcurrentHashMap.Entry[] table
protected static final int CONCURRENCY_LEVEL
protected static final int SEGMENT_MASK
protected final ConcurrentHashMap.Segment[] segments
public static int DEFAULT_INITIAL_CAPACITY
public static final float DEFAULT_LOAD_FACTOR
protected final float loadFactor
protected int threshold
protected transient volatile int votesForResize
protected transient java.util.Set keySet
protected transient java.util.Set entrySet
protected transient java.util.Collection values
Constructor Detail |
public ConcurrentHashMap(int initialCapacity, float loadFactor)
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.
java.lang.IllegalArgumentException
- if the load factor is nonpositive.public ConcurrentHashMap(int initialCapacity)
initialCapacity
- the initial capacity of the
ConcurrentHashMap.
java.lang.IllegalArgumentException
- if the initial maximum number
of elements is less
than zero.public ConcurrentHashMap()
public ConcurrentHashMap(java.util.Map t)
Method Detail |
protected static int bitcount(int w)
protected static int hash(java.lang.Object x)
protected boolean eq(java.lang.Object x, java.lang.Object y)
protected ConcurrentHashMap.Entry[] newTable(int capacity)
public int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
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
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
. (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.
put
in interface java.util.Map
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 void resize(int index, ConcurrentHashMap.Entry[] assumedTab)
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.protected void rehash()
public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
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 remove(java.lang.Object key, java.lang.Object value)
key
- the key that needs to be removed.value
- the associated value. If the value is null,
it means "any value".
null
if the key did not have a mapping.
java.lang.NullPointerException
- if the key is
null
.public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
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
t
- Mappings to be stored in this map.public void clear()
clear
in interface java.util.Map
public java.lang.Object clone()
public java.util.Set keySet()
keySet
in interface java.util.Map
public java.util.Collection values()
values
in interface java.util.Map
public java.util.Set entrySet()
entrySet
in interface java.util.Map
public java.util.Enumeration keys()
Enumeration
,
elements()
,
keySet()
,
Map
public java.util.Enumeration elements()
Enumeration
,
keys()
,
values()
,
Map
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |