Fix isEmpty() and count()

isEmpty() need not rely on peek(which is intended for consumer use) and instead compare head to tail.
count() should stop counting at max int to avoid wrap.
This commit is contained in:
Nitsan Wakart 2014-08-08 15:17:36 +02:00
parent e8cefbf7ba
commit 74d0f0cc6c

View file

@ -57,12 +57,12 @@ public abstract class AbstractNodeQueue<T> extends AtomicReference<AbstractNodeQ
}
public final boolean isEmpty() {
return peek() == null;
return Unsafe.instance.getObjectVolatile(this, tailOffset)) == get();
}
public final int count() {
int count = 0;
for(Node<T> n = peekNode();n != null; n = n.next())
for(Node<T> n = peekNode();n != null && count < Integer.MAX_VALUE; n = n.next())
++count;
return count;
}