コード例 #1
0
  public void append(E value) {
    // Start the pointer at this node
    LinkList<E> node = this;
    node.lock.lock();

    while (node.rest != null) {
      LinkList<E> next = node.rest;

      // Here's the hand-over-hand locking
      try {
        // Lock the next node
        next.lock.lock();
      } finally {
        // unlock the current node
        node.lock.unlock();
      }

      // Traverse
      node = next;
    }

    // We're at the final node, so append and then unlock
    try {
      node.rest = new LinkList<E>(value);

      // Let any waiting threads know that this node's link has changed
      node.linkChanged.signalAll();
    } finally {
      node.lock.unlock();
    }
  }