FileDocCategorySizeDatePackage
LinkList.javaAPI DocExample1866Wed Apr 20 15:12:20 BST 2005None

LinkList

public class LinkList extends Object
A partial implementation of a linked list of values of type E. It demonstrates hand-over-hand locking with Lock

Fields Summary
E
value
LinkList
rest
Lock
lock
Constructors Summary
public LinkList(E value)

  // Constructor for a list
        this.value = value;          // Node value
        rest = null;                 // This is the only node in the list
        lock = new ReentrantLock();  // We can lock this node
    
Methods Summary
public voidappend(E value)
Append a node to the end of the list, traversing the list using hand-over-hand locking. This method is threadsafe: multiple threads may traverse different portions of the list at the same time.

        LinkList<E> node = this;  // Start at this node
        node.lock.lock();         // Lock it.

        // Loop 'till we find the last node in the list
        while(node.rest != null) {
            LinkList<E> next = node.rest;

            // This is the hand-over-hand part.  Lock the next node and then
            // unlock the current node.  We use a try/finally construct so
            // that the current node is unlocked even if the lock on the
            // next node fails with an exception.
            try { next.lock.lock(); }  // lock the next node
            finally { node.lock.unlock(); } // unlock the current node
            node = next;
        }

        // At this point, node is the final node in the list, and we have
        // a lock on it.  Use a try/finally to ensure that we unlock it.
        try {
            node.rest = new LinkList<E>(value); // Append new node
        }
        finally { node.lock.unlock(); }