org.norther.tammi.acorn.util
Class ConcurrentWriteMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.norther.tammi.acorn.util.ConcurrentAbstractMap<K,V>
          extended by org.norther.tammi.acorn.util.ConcurrentWriteMap<K,V>
Type Parameters:
K - the type of keys maintained by this map.
V - the type of mapped values.
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>

public class ConcurrentWriteMap<K,V>
extends ConcurrentAbstractMap<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 .

Version:
$Id: ConcurrentWriteMap.java,v 1.17 2009/09/28 15:08:48 cvsimp Exp $
Author:
Doug Lea, Ilkka Priha
See Also:
Serialized Form

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

MAXIMUM_CAPACITY

protected static final int MAXIMUM_CAPACITY
The maximum capacity.

See Also:
Constant Field Values

MINIMUM_CAPACITY

protected static final int MINIMUM_CAPACITY
The minimum capacity.

See Also:
Constant Field Values

DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY
The default capacity.

See Also:
Constant Field Values
Constructor Detail

ConcurrentWriteMap

public ConcurrentWriteMap()
Constructs a new, empty map with a default capacity and load factor.


ConcurrentWriteMap

public ConcurrentWriteMap(int capacity)
Constructs a new, empty map with the specified initial capacity and default load factor.

Note that the initial capacity will be rounded to the nearest power of two.

Parameters:
capacity - the initial capacity.
Throws:
IllegalArgumentException - for negative values.

ConcurrentWriteMap

public ConcurrentWriteMap(int capacity,
                          float factor)
Constructs a new, empty map with the specified initial capacity and the specified load 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.

Parameters:
capacity - the initial capacity.
factor - the load factor threshold, used to control resizing.
Throws:
IllegalArgumentException - for negative values.

ConcurrentWriteMap

public ConcurrentWriteMap(Map<? extends K,? extends V> map)
Constructs a new map with the same mappings as the given map. The map is created with the default load factor and the corresponding capacity.

Parameters:
map - the map to copy.
Method Detail

size

public int size()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in class AbstractMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>

putAll

public void putAll(Map<? extends K,? extends V> map)
Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in class AbstractMap<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

capacity

protected int capacity(int capacity)
Description copied from class: ConcurrentAbstractMap
Returns the appropriate capacity for the expected maximum capacity.

Specified by:
capacity in class ConcurrentAbstractMap<K,V>
Parameters:
capacity - the desired capacity.
Returns:
the appropriate capacity.

getEntryTable

protected final ConcurrentAbstractMap.ConcurrentEntry<K,V>[] getEntryTable()
Description copied from class: ConcurrentAbstractMap
Gets the entry table.

Specified by:
getEntryTable in class ConcurrentAbstractMap<K,V>
Returns:
the entry table.

setEntryTable

protected final void setEntryTable(ConcurrentAbstractMap.ConcurrentEntry<K,V>[] tab)
Description copied from class: ConcurrentAbstractMap
Sets the entry table.

Specified by:
setEntryTable in class ConcurrentAbstractMap<K,V>
Parameters:
tab - the entry table.

getEntry

protected ConcurrentAbstractMap.ConcurrentEntry<K,V> getEntry(Object key)
Description copied from class: ConcurrentAbstractMap
Gets an entry mapped to the specified key.

Specified by:
getEntry in class ConcurrentAbstractMap<K,V>
Parameters:
key - the key.
Returns:
the entry or null if not found.

putEntry

protected V putEntry(K key,
                     V value,
                     int state,
                     boolean r,
                     ConcurrentAbstractMap.Modification[] m)
Description copied from class: ConcurrentAbstractMap
Puts an entry mapped to the specified key.

Specified by:
putEntry in class ConcurrentAbstractMap<K,V>
Parameters:
key - the key.
value - the value.
state - the state.
r - true to replace, false otherwise.
m - modification status (output).
Returns:
the previous value or null if not found.

replaceEntry

protected V replaceEntry(K key,
                         V repValue,
                         V newValue,
                         boolean r,
                         ConcurrentAbstractMap.Modification[] m)
Description copied from class: ConcurrentAbstractMap
Replaces an entry mapped to the specified key.

Specified by:
replaceEntry in class ConcurrentAbstractMap<K,V>
Parameters:
key - the key.
repValue - the value to replace.
newValue - the new value.
r - true to replace, false otherwise.
m - modification status (output).
Returns:
the previous value or null if not found.

removeEntry

protected ConcurrentAbstractMap.ConcurrentEntry<K,V> removeEntry(Object key,
                                                                 Object value,
                                                                 boolean r,
                                                                 ConcurrentAbstractMap.Modification[] m)
Description copied from class: ConcurrentAbstractMap
Removes an entry mapped to the specified key.

Specified by:
removeEntry in class ConcurrentAbstractMap<K,V>
Parameters:
key - the key.
value - the value.
r - true to replace, false otherwise.
m - modification status (output).
Returns:
the entry or null if not found.

rehash

protected void rehash()
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.


resize

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.

Parameters:
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.


Copyright © 2004 The Norther Organization. All rights reserved.