public void enqueue(Object anObject) {
   if (head == null) {
     head = tail = new QNode(anObject, null);
   } else {
     tail.next = new QNode(anObject, null);
     tail = tail.next;
   }
 }
      public Object dequeue() {
        if (head == null) {
          throw new NoSuchElementException("No more elements");
        }

        Object retval = head.object;
        QNode oldHead = head;
        head = head.next;
        if (head == null) {
          tail = null;
        } else {
          oldHead.next = null;
        }
        return retval;
      }
Example #3
0
 /**
  * Tries to cas nh as new head; if successful, unlink old head's next node to avoid garbage
  * retention.
  */
 void advanceHead(QNode h, QNode nh) {
   if (h == head && UNSAFE.compareAndSwapObject(this, headOffset, h, nh))
     h.next = h; // forget old next
 }