@Override public int size() { // Read consumer first, this is important because if the producer is node is 'older' than the consumer // the consumer may overtake it (consume past it) invalidating the 'snapshot' notion of size. LinkedQueueNode<E> chaserNode = consumerNode; LinkedQueueNode<E> producerNode = this.producerNode; int size = 0; // must chase the nodes all the way to the producer node, but there's no need to count beyond expected head. while (chaserNode != producerNode && // don't go passed producer node chaserNode != null && // stop at last node size < Integer.MAX_VALUE) // stop at max int { LinkedQueueNode<E> next; next = chaserNode.lvNext(); // check if this node has been consumed, if so return what we have if (next == chaserNode) { return size; } chaserNode = next; size++; } return size; }
@Nullable @Override public E peek() { LinkedQueueNode<E> currConsumerNode = consumerNode; // don't load twice, it's alright LinkedQueueNode<E> nextNode = currConsumerNode.lvNext(); if (nextNode != null) { return nextNode.lpValue(); } else if (currConsumerNode != producerNode) { while ((nextNode = currConsumerNode.lvNext()) == null) { } // got the next node... return nextNode.lpValue(); } return null; }
@Override public int size() { // Read consumer first, this is important because if the producer is node is 'older' than the consumer // the consumer may overtake it (consume past it) invalidating the 'snapshot' notion of size. LinkedQueueNode<E> chaserNode = consumerNode; LinkedQueueNode<E> producerNode = this.producerNode; int size = 0; // must chase the nodes all the way to the producer node, but there's no need to count beyond expected head. while (chaserNode != producerNode && // don't go passed producer node chaserNode != null && // stop at last node size < Integer.MAX_VALUE) // stop at max int { LinkedQueueNode<E> next; next = chaserNode.lvNext(); // check if this node has been consumed, if so return what we have if (next == chaserNode) { return size; } chaserNode = next; size++; } return size; }
@Nullable @Override public E peek() { LinkedQueueNode<E> currConsumerNode = consumerNode; // don't load twice, it's alright LinkedQueueNode<E> nextNode = currConsumerNode.lvNext(); if (nextNode != null) { return nextNode.lpValue(); } else if (currConsumerNode != producerNode) { while ((nextNode = currConsumerNode.lvNext()) == null) { } // got the next node... return nextNode.lpValue(); } return null; }