|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractMap<K,V> org.norther.tammi.acorn.util.ConcurrentAbstractMap<K,V> org.norther.tammi.acorn.util.ConcurrentWriteMap<K,V>
K
- the type of keys maintained by this map.V
- the type of mapped values.public class ConcurrentWriteMap<K,V>
An implementation of the Map
interface that supports both
thread-safe concurrent reading and concurrent writing. This makes it an
interesting choice for writer oriented tasks that require good performance in
a multithreaded context provided by minor synchronization.
There is NOT any support for locking the entire map to prevent
updates. This makes it impossible, 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
ConcurrentReadMap
class.
Iterators and Enumerations return elements reflecting the state of the map at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException.
This class allows null to be used as a key or value.
Based on Doug Lea's ConcurrentHashMap .
Nested Class Summary |
---|
Nested classes/interfaces inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap |
---|
ConcurrentAbstractMap.ConcurrentEntry<K,V>, ConcurrentAbstractMap.Modification |
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Field Summary | |
---|---|
protected static int |
DEFAULT_CAPACITY
The default capacity. |
protected static int |
MAXIMUM_CAPACITY
The maximum capacity. |
protected static int |
MINIMUM_CAPACITY
The minimum capacity. |
Fields inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap |
---|
DEFAULT_LOAD_FACTOR |
Constructor Summary | |
---|---|
ConcurrentWriteMap()
Constructs a new, empty map with a default capacity and load factor. |
|
ConcurrentWriteMap(int capacity)
Constructs a new, empty map with the specified initial capacity and default load factor. |
|
ConcurrentWriteMap(int capacity,
float factor)
Constructs a new, empty map with the specified initial capacity and the specified load factor. |
|
ConcurrentWriteMap(Map<? extends K,? extends V> map)
Constructs a new map with the same mappings as the given map. |
Method Summary | |
---|---|
protected int |
capacity(int capacity)
Returns the appropriate capacity for the expected maximum capacity. |
void |
clear()
|
boolean |
containsValue(Object value)
|
protected ConcurrentAbstractMap.ConcurrentEntry<K,V> |
getEntry(Object key)
Gets an entry mapped to the specified key. |
protected ConcurrentAbstractMap.ConcurrentEntry<K,V>[] |
getEntryTable()
Gets the entry table. |
boolean |
isEmpty()
|
void |
putAll(Map<? extends K,? extends V> map)
|
protected V |
putEntry(K key,
V value,
int state,
boolean r,
ConcurrentAbstractMap.Modification[] m)
Puts an entry mapped to the specified key. |
protected void |
rehash()
Rehashes the contents of this map into a new table with a larger capacity. |
protected ConcurrentAbstractMap.ConcurrentEntry<K,V> |
removeEntry(Object key,
Object value,
boolean r,
ConcurrentAbstractMap.Modification[] m)
Removes an entry mapped to the specified key. |
protected V |
replaceEntry(K key,
V repValue,
V newValue,
boolean r,
ConcurrentAbstractMap.Modification[] m)
Replaces an entry mapped to the specified key. |
protected void |
resize(int index,
ConcurrentAbstractMap.ConcurrentEntry<K,V>[] assumedTab)
Gathers all locks in order to call rehash, by recursing within synch blocks for each segment index. |
protected void |
setEntryTable(ConcurrentAbstractMap.ConcurrentEntry<K,V>[] tab)
Sets the entry table. |
int |
size()
|
Methods inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap |
---|
add, capacity, clone, contains, containsKey, elements, entrySet, get, keys, keySet, put, putIfAbsent, remove, remove, replace, replace, values |
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 static final int MAXIMUM_CAPACITY
protected static final int MINIMUM_CAPACITY
protected static final int DEFAULT_CAPACITY
Constructor Detail |
---|
public ConcurrentWriteMap()
public ConcurrentWriteMap(int capacity)
Note that the initial capacity will be rounded to the nearest power of two.
capacity
- the initial capacity.
IllegalArgumentException
- for negative values.public ConcurrentWriteMap(int capacity, float factor)
Note that the initial capacity will be rounded to the nearest power of two.
The load factor 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.
capacity
- the initial capacity.factor
- the load factor threshold, used to control resizing.
IllegalArgumentException
- for negative values.public ConcurrentWriteMap(Map<? extends K,? extends V> map)
map
- the map to copy.Method Detail |
---|
public int size()
size
in interface Map<K,V>
size
in class AbstractMap<K,V>
public boolean isEmpty()
isEmpty
in interface Map<K,V>
isEmpty
in class AbstractMap<K,V>
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
public void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K,V>
putAll
in class AbstractMap<K,V>
public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
protected int capacity(int capacity)
ConcurrentAbstractMap
capacity
in class ConcurrentAbstractMap<K,V>
capacity
- the desired capacity.
protected final ConcurrentAbstractMap.ConcurrentEntry<K,V>[] getEntryTable()
ConcurrentAbstractMap
getEntryTable
in class ConcurrentAbstractMap<K,V>
protected final void setEntryTable(ConcurrentAbstractMap.ConcurrentEntry<K,V>[] tab)
ConcurrentAbstractMap
setEntryTable
in class ConcurrentAbstractMap<K,V>
tab
- the entry table.protected ConcurrentAbstractMap.ConcurrentEntry<K,V> getEntry(Object key)
ConcurrentAbstractMap
getEntry
in class ConcurrentAbstractMap<K,V>
key
- the key.
protected V putEntry(K key, V value, int state, boolean r, ConcurrentAbstractMap.Modification[] m)
ConcurrentAbstractMap
putEntry
in class ConcurrentAbstractMap<K,V>
key
- the key.value
- the value.state
- the state.r
- true to replace, false otherwise.m
- modification status (output).
protected V replaceEntry(K key, V repValue, V newValue, boolean r, ConcurrentAbstractMap.Modification[] m)
ConcurrentAbstractMap
replaceEntry
in class ConcurrentAbstractMap<K,V>
key
- the key.repValue
- the value to replace.newValue
- the new value.r
- true to replace, false otherwise.m
- modification status (output).
protected ConcurrentAbstractMap.ConcurrentEntry<K,V> removeEntry(Object key, Object value, boolean r, ConcurrentAbstractMap.Modification[] m)
ConcurrentAbstractMap
removeEntry
in class ConcurrentAbstractMap<K,V>
key
- the key.value
- the value.r
- true to replace, false otherwise.m
- modification status (output).
protected void rehash()
protected void resize(int index, ConcurrentAbstractMap.ConcurrentEntry<K,V>[] assumedTab)
index
- the current segment, initially call value must be 0.assumedTab
- 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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |