Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution. : Hash Code « Development Class « Java






Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution.

      
/*
 * RWStripedHashSet.java
 *
 * Created on November 15, 2006, 3:35 PM
 *
 * From "The Art of Multiprocessor Programming",
 * by Maurice Herlihy and Nir Shavit.
 *
 * This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
 * http://i.creativecommons.org/l/by-sa/3.0/us/88x31.png
 */
//package xbird.util.concurrent.set;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Concurrent Cuckoo hashing using lock striping.
 * Uses R/W locks for resizing. Exercise solution.
  * @param <T> type
 * @author Maurice Herlihy
 */
public class RWStripedHashSet<T> extends BaseHashSet<T> {
    final Lock readLock;
    final Lock writeLock;
    volatile Lock[] locks;

    /**
     * Constructor
     * @param capacity Initial number of  buckets.
     */
    public RWStripedHashSet(int capacity) {
        super(capacity);
        locks = new Lock[capacity];
        for(int j = 0; j < locks.length; j++) {
            locks[j] = new ReentrantLock();
        }
        ReadWriteLock rwLock = new ReentrantReadWriteLock();
        readLock = rwLock.readLock();
        writeLock = rwLock.writeLock();
    }

    /**
     * double the set size
     */
    @Override
    public void resize() {
        int oldCapacity = table.length;
        writeLock.lock();
        try {
            if(oldCapacity != table.length) {
                return; // someone beat us to it
            }
            int newCapacity = 2 * oldCapacity;
            List<T>[] oldTable = table;
            table = (List<T>[]) new List[newCapacity];
            for(int i = 0; i < newCapacity; i++)
                table[i] = new ArrayList<T>();
            initializeFrom(oldTable);
        } finally {
            writeLock.unlock();
        }
    }

    private void initializeFrom(List<T>[] oldTable) {
        for(List<T> bucket : oldTable) {
            for(T x : bucket) {
                int myBucket = Math.abs(x.hashCode() % table.length);
                table[myBucket].add(x);
            }
        }
    }

    /**
     * Synchronize before adding, removing, or testing for item
     * @param x item involved
     */
    @Override
    public final void acquire(T x) {
        readLock.lock();
        int myBucket = Math.abs(x.hashCode() % locks.length);
        locks[myBucket].lock();
    }

    /**
     * synchronize after adding, removing, or testing for item
     * @param x item involved
     */
    @Override
    public void release(T x) {
        readLock.unlock();
        int myBucket = Math.abs(x.hashCode() % locks.length);
        locks[myBucket].unlock();
    }

    @Override
    public boolean policy() {
        return size / table.length > 4;
    }
}


/**
 * Simple fine-grained hash map.
 * @param <T> type
 * @author Maurice Herlihy
 */
 abstract class BaseHashSet<T> {
    protected List<T>[] table;
    protected int size;

    public BaseHashSet(int capacity) {
        size = 0;
        table = (List<T>[]) new List[capacity];
        for(int i = 0; i < capacity; i++) {
            table[i] = new ArrayList<T>();
        }
    }

    /**
     * Is item in set?
     * @param x item to test
     * @return <code>true</code> iff item present
     */
    public boolean contains(T x) {
        acquire(x);
        try {
            int myBucket = Math.abs(x.hashCode() % table.length);
            return table[myBucket].contains(x);
        } finally {
            release(x);
        }
    }

    /**
     * Add item to set
     * @param x item to add
     * @return <code>true</code> iff set changed
     */
    public boolean add(T x) {
        boolean result = false;
        acquire(x);
        try {
            int myBucket = Math.abs(x.hashCode() % table.length);
            result = table[myBucket].add(x);
            size = result ? size + 1 : size;
        } finally {
            release(x); // always unlock
        }
        if(policy())
            resize();
        return result;
    }

    /**
     * Remove item from set
     * @param x item to remove
     * @return <code>true</code> iff set changed
     */
    public boolean remove(T x) {
        acquire(x);
        try {
            int myBucket = Math.abs(x.hashCode() % table.length);
            boolean result = table[myBucket].remove(x);
            size = result ? size - 1 : size;
            return result;
        } finally {
            release(x); // always unlock
        }
    }

    /**
     * Synchronize before adding, removing, or testing for item
     * @param x item involved
     */
    public abstract void acquire(T x);

    /**
     * synchronize after adding, removing, or testing for item
     * @param x item involved
     */
    public abstract void release(T x);

    /**
     * double the set size
     */
    public abstract void resize();

    /**
     * decide whether to resize
     * @return whether to resize
     */
    public abstract boolean policy();

}

   
    
    
    
    
    
  








Related examples in the same category

1.Computing hash codes
2.A hash-code generator and a collection of static hash-code generation methods.
3.MD5 hash generator
4.Hash 32 String
5.Hash 64 String
6.MD5 hashing: Encodes a string
7.MD5 String
8.Hash Code BuilderHash Code Builder
9.HashCode generationHashCode generation
10.Get hash code for primitive data types
11.Return as hash code for the given object
12.Null Safe Hash Code
13.A very efficient java hash algorithm, based on the BuzHash algoritm
14.Easy implementation of hashCode
15.An implementation of the HMACT64 keyed hashing algorithm
16.Gets the hash code of an object returning zero when the object is null
17.Unify Hash
18.Secure Hash
19.FNV Hash
20.Jenkins Hash
21.Concurrent Cuckoo hashing using lock striping.
22.encode Hex
23.Fowler/Noll/Vo hash algorhythm
24.Produces 32-bit hash for hash table lookup. (Jenkins Hash Function)
25.Key Value Hash
26.Paul Hsieh's Hash Function.
27.An extension of WeakReference that implements a sane equals and hashcode method.
28.Dual Key Hash Map
29.A hash map with int key and int values.
30.null Safe Equals and Hash
31.Generates a hash code for a given source code.
32.AtkinsonHash utility class implements the hash algorithm used by HyperCard's ask password command.
33.Hash Code AssistHash Code Assist
34.This is a very fast, non-cryptographic hash suitable for general hash-based lookup.
35.An advanced hash table supporting configurable garbage collection semantics of keys and values
36.Hash string