com.google.common.collect
Class CustomConcurrentHashMap

java.lang.Object
  extended by com.google.common.collect.CustomConcurrentHashMap

public final class CustomConcurrentHashMap
extends java.lang.Object

A framework for concurrent hash map implementations. The CustomConcurrentHashMap class itself is not extensible and does not contain any methods. Use CustomConcurrentHashMap.Builder to create a custom concurrent hash map instance. Client libraries implement CustomConcurrentHashMap.Strategy, and this class provides the surrounding concurrent data structure which implements ConcurrentMap. Additionally supports implementing maps where Map.get(java.lang.Object) atomically computes values on demand (see CustomConcurrentHashMap.Builder.buildComputingMap(CustomConcurrentHashMap.ComputingStrategy, Function)).

The resulting hash table supports full concurrency of retrievals and adjustable expected concurrency for updates. Even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access.

Retrieval operations (including Map.get(java.lang.Object)) generally do not block, so may overlap with update operations (including Map.put(K, V) and Map.remove(java.lang.Object)). Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as Map.putAll(java.util.Map) and Map.clear(), concurrent retrievals may reflect insertion or removal of only some entries. Similarly, iterators return elements reflecting the state of the hash table at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException. However, iterators can only be used by one thread at a time.

The resulting ConcurrentMap and its views and iterators implement all of the optional methods of the Map and Iterator interfaces. Partially reclaimed entries are never exposed through the views or iterators.

For example, the following strategy emulates the behavior of ConcurrentHashMap:

 class ConcurrentHashMapStrategy<K, V>
     implements CustomConcurrentHashMap.Strategy<K, V,
     InternalEntry<K, V>>, Serializable {
   public InternalEntry<K, V> newEntry(K key, int hash,
       InternalEntry<K, V> next) {
     return new InternalEntry<K, V>(key, hash, null, next);
   }
   public InternalEntry<K, V> copyEntry(K key,
       InternalEntry<K, V> original, InternalEntry<K, V> next) {
     return new InternalEntry<K, V>(key, original.hash, original.value, next);
   }
   public void setValue(InternalEntry<K, V> entry, V value) {
     entry.value = value;
   }
   public V getValue(InternalEntry<K, V> entry) { return entry.value; }
   public boolean equalKeys(K a, Object b) { return a.equals(b); }
   public boolean equalValues(V a, Object b) { return a.equals(b); }
   public int hashKey(Object key) { return key.hashCode(); }
   public K getKey(InternalEntry<K, V> entry) { return entry.key; }
   public InternalEntry<K, V> getNext(InternalEntry<K, V> entry) {
     return entry.next;
   }
   public int getHash(InternalEntry<K, V> entry) { return entry.hash; }
   public void setInternals(CustomConcurrentHashMap.Internals<K, V,
       InternalEntry<K, V>> internals) {} // ignored
 }

 class InternalEntry<K, V> {
   final K key;
   final int hash;
   volatile V value;
   final InternalEntry<K, V> next;
   InternalEntry(K key, int hash, V value, InternalEntry<K, V> next) {
     this.key = key;
     this.hash = hash;
     this.value = value;
     this.next = next;
   }
 }
 
To create a ConcurrentMap using the strategy above:
ConcurrentMap<K, V> map = new CustomConcurrentHashMap.Builder()
       .build(new ConcurrentHashMapStrategy<K, V>());
 

Author:
Bob Lee, Doug Lea

Nested Class Summary
static class CustomConcurrentHashMap.Builder
          Builds a custom concurrent hash map.
static interface CustomConcurrentHashMap.ComputingStrategy<K,V,E>
          Extends CustomConcurrentHashMap.Strategy to add support for computing values on-demand.
static interface CustomConcurrentHashMap.Internals<K,V,E>
          Provides access to a map's internal entries.
static interface CustomConcurrentHashMap.Strategy<K,V,E>
          Implements behavior specific to the client's concurrent hash map implementation.
 
Method Summary
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 



Copyright © 2007-2009 Google. All Rights Reserved.