org.norther.tammi.acorn.util
Class ConcurrentWriteMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by org.norther.tammi.acorn.util.ConcurrentAbstractMap
          extended by org.norther.tammi.acorn.util.ConcurrentWriteMap
All Implemented Interfaces:
Serializable, ConcurrentMap, Map

public class ConcurrentWriteMap
extends ConcurrentAbstractMap

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 .

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.Entry
 
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 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 containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected  ConcurrentAbstractMap.Entry getEntry(Object key)
          Gets the internal entry mapped to the specified key.
protected  Object getInternal(Object key)
          Gets the internal value mapped to the specified key.
protected  ConcurrentAbstractMap.Entry[] getReadTable()
          Gets the internal read table.
protected  ConcurrentAbstractMap.Entry[] getWriteTable()
          Gets the internal write table.
 boolean isEmpty()
           
 void putAll(Map map)
           
protected  Object putInternal(Object key, Object value, boolean r)
          Puts the internal value mapped to the specified key.
protected  void rehash()
          Rehashes the contents of this map into a new table with a larger capacity.
protected  Object removeInternal(Object key, Object value)
          Removes the internal mapping with the optional value.
protected  Object replaceInternal(Object key, Object oldValue, Object newValue)
          Replaces the internal value mapped to the specified key with the optional value.
protected  void resize(int index, ConcurrentAbstractMap.Entry[] assumedTab)
          Gathers all locks in order to call rehash, by recursing within synch blocks for each segment index.
protected  void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
          Sets the internal write table.
 int size()
           
 
Methods inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap
add, capacity, clone, contains, elements, entrySet, get, hash, 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 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
Overrides:
size in class AbstractMap

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
Overrides:
isEmpty in class AbstractMap

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map
Overrides:
containsKey in class AbstractMap

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
Overrides:
containsValue in class AbstractMap

putAll

public void putAll(Map map)
Specified by:
putAll in interface Map
Overrides:
putAll in class AbstractMap

clear

public void clear()
Specified by:
clear in interface Map
Overrides:
clear in class AbstractMap

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
Parameters:
capacity - the desired capacity.
Returns:
the appropriate capacity.

getReadTable

protected final ConcurrentAbstractMap.Entry[] getReadTable()
Description copied from class: ConcurrentAbstractMap
Gets the internal read table.

Specified by:
getReadTable in class ConcurrentAbstractMap
Returns:
the read table.

getWriteTable

protected final ConcurrentAbstractMap.Entry[] getWriteTable()
Description copied from class: ConcurrentAbstractMap
Gets the internal write table.

Specified by:
getWriteTable in class ConcurrentAbstractMap
Returns:
the write table.

setWriteTable

protected final void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
Description copied from class: ConcurrentAbstractMap
Sets the internal write table.

Specified by:
setWriteTable in class ConcurrentAbstractMap
Parameters:
tab - the write table.

getEntry

protected ConcurrentAbstractMap.Entry getEntry(Object key)
Description copied from class: ConcurrentAbstractMap
Gets the internal entry mapped to the specified key.

Specified by:
getEntry in class ConcurrentAbstractMap
Parameters:
key - the key.
Returns:
the entry or null if not found.

getInternal

protected Object getInternal(Object key)
Description copied from class: ConcurrentAbstractMap
Gets the internal value mapped to the specified key.

Specified by:
getInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
Returns:
the value or null if not found.

putInternal

protected Object putInternal(Object key,
                             Object value,
                             boolean r)
Description copied from class: ConcurrentAbstractMap
Puts the internal value mapped to the specified key.

Specified by:
putInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
value - the value.
r - if true, replace an existing mapping.
Returns:
the previous value or null if not found.

replaceInternal

protected Object replaceInternal(Object key,
                                 Object oldValue,
                                 Object newValue)
Description copied from class: ConcurrentAbstractMap
Replaces the internal value mapped to the specified key with the optional value.

Specified by:
replaceInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
oldValue - the old value.
newValue - the new value.
Returns:
the previous value or null if not found.

removeInternal

protected Object removeInternal(Object key,
                                Object value)
Description copied from class: ConcurrentAbstractMap
Removes the internal mapping with the optional value.

Specified by:
removeInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
value - the value.
Returns:
the removed value 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.Entry[] 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.