Class LinkedTransferQueue<E>
- Type Parameters:
E- the type of elements held in this collection
- All Implemented Interfaces:
Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
- Direct Known Subclasses:
FlowControlLinkedTransferQueue
Beware that, unlike in most collections, the size method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires a traversal of the elements.
This class and its iterator implement all of the optional methods of the
Collection and Iterator interfaces.
Memory consistency effects: As with other concurrent collections, actions in a thread prior to
placing an object into a LinkedTransferQueue
happen-before actions subsequent to
the access or removal of that element from the LinkedTransferQueue in another thread.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classIterators.static final classPadded version of AtomicReference used for head, tail and cleanMe, to alleviate contention across threads CASing one vs the other.private static final classNode class for LinkedTransferQueue. -
Field Summary
FieldsModifier and TypeFieldDescriptionReference to a cancelled node that might not yet have been unlinked from queue because it was the last inserted node when it cancelled.head of the queueprivate static final intThe number of times to spin before blocking in timed waits.private static final intThe number of times to spin before blocking in untimed waits.private static final intThe number of CPUs, for spin controlprivate static final intprivate static final longThe number of nanoseconds for which it is faster to spin rather than to use timed park.tail of the queueprivate static final intprivate static final int -
Constructor Summary
ConstructorsConstructorDescriptionCreates an initially empty LinkedTransferQueue.LinkedTransferQueue(Collection<? extends E> c) Creates a LinkedTransferQueue initially containing the elements of the given collection, added in traversal order of the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanTries to cas nh as new head; if successful, unlink old head's next node to avoid garbage retention.private ObjectawaitFulfill(LinkedTransferQueue.QNode pred, LinkedTransferQueue.QNode s, Object e, int mode, long nanos) Spins/blocks until node s is fulfilled or caller gives up, depending on wait mode.(package private) E(package private) voidGets rid of cancelled node s with original predecessor pred.intdrainTo(Collection<? super E> c) intdrainTo(Collection<? super E> c, int maxElements) private ObjectVersion of xfer for poll() and tryTransfer, which simplifies control paths both here and in xferprivate LinkedTransferQueue.QNodeReturns validated tail for use in cleaning methodsintbooleanbooleanisEmpty()iterator()booleanbooleanpeek()poll()voidprivate LinkedTransferQueue.QNodereclean()Tries to unsplice the cancelled node held in cleanMe that was previously uncleanable because it was at tail.intintsize()Returns the number of elements in this queue.take()void(package private) LinkedTransferQueue.QNodeReturn head after performing any outstanding helping stepsbooleantryTransfer(E e) booleantryTransfer(E e, long timeout, TimeUnit unit) private ObjectPuts or takes an item.Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.BlockingQueue
add, contains, removeMethods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Field Details
-
NOWAIT
private static final int NOWAIT- See Also:
-
TIMEOUT
private static final int TIMEOUT- See Also:
-
WAIT
private static final int WAIT- See Also:
-
NCPUS
private static final int NCPUSThe number of CPUs, for spin control -
maxTimedSpins
private static final int maxTimedSpinsThe number of times to spin before blocking in timed waits. The value is empirically derived -- it works well across a variety of processors and OSes. Empirically, the best value seems not to vary with number of CPUs (beyond 2) so is just a constant. -
maxUntimedSpins
private static final int maxUntimedSpinsThe number of times to spin before blocking in untimed waits. This is greater than timed value because untimed waits spin faster since they don't need to check times on each spin. -
spinForTimeoutThreshold
private static final long spinForTimeoutThresholdThe number of nanoseconds for which it is faster to spin rather than to use timed park. A rough estimate suffices.- See Also:
-
head
head of the queue -
tail
tail of the queue -
cleanMe
Reference to a cancelled node that might not yet have been unlinked from queue because it was the last inserted node when it cancelled.
-
-
Constructor Details
-
LinkedTransferQueue
public LinkedTransferQueue()Creates an initially empty LinkedTransferQueue. -
LinkedTransferQueue
Creates a LinkedTransferQueue initially containing the elements of the given collection, added in traversal order of the collection's iterator.- Parameters:
c- the collection of elements to initially contain- Throws:
NullPointerException- if the specified collection or any of its elements are null
-
-
Method Details
-
advanceHead
Tries to cas nh as new head; if successful, unlink old head's next node to avoid garbage retention. -
xfer
Puts or takes an item. Used for most queue operations (except poll() and tryTransfer()). See the similar code in SynchronousQueue for detailed explanation.- Parameters:
e- the item or if null, signifies that this is a takemode- the wait mode: NOWAIT, TIMEOUT, WAITnanos- timeout in nanosecs, used only if mode is TIMEOUT- Returns:
- an item, or null on failure
-
fulfill
Version of xfer for poll() and tryTransfer, which simplifies control paths both here and in xfer -
awaitFulfill
private Object awaitFulfill(LinkedTransferQueue.QNode pred, LinkedTransferQueue.QNode s, Object e, int mode, long nanos) Spins/blocks until node s is fulfilled or caller gives up, depending on wait mode.- Parameters:
pred- the predecessor of waiting nodes- the waiting nodee- the comparison value for checking matchmode- modenanos- timeout value- Returns:
- matched item, or s if cancelled
-
getValidatedTail
Returns validated tail for use in cleaning methods -
clean
Gets rid of cancelled node s with original predecessor pred.- Parameters:
pred- predecessor of cancelled nodes- the cancelled node
-
reclean
Tries to unsplice the cancelled node held in cleanMe that was previously uncleanable because it was at tail.- Returns:
- current cleanMe node (or null)
-
cast
-
put
- Specified by:
putin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
offer
- Specified by:
offerin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
offer
-
transfer
- Throws:
InterruptedException
-
tryTransfer
- Throws:
InterruptedException
-
tryTransfer
-
take
- Specified by:
takein interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
- Specified by:
pollin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
traversalHead
LinkedTransferQueue.QNode traversalHead()Return head after performing any outstanding helping steps -
iterator
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>
-
peek
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
hasWaitingConsumer
public boolean hasWaitingConsumer() -
size
public int size()Returns the number of elements in this queue. If this queue contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O(n) traversal.
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this queue
-
getWaitingConsumerCount
public int getWaitingConsumerCount() -
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacityin interfaceBlockingQueue<E>
-