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(); }