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) class
Iterators.static final class
Padded version of AtomicReference used for head, tail and cleanMe, to alleviate contention across threads CASing one vs the other.private static final class
Node 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 int
The number of times to spin before blocking in timed waits.private static final int
The number of times to spin before blocking in untimed waits.private static final int
The number of CPUs, for spin controlprivate static final int
private static final long
The number of nanoseconds for which it is faster to spin rather than to use timed park.tail of the queueprivate static final int
private 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 boolean
Tries to cas nh as new head; if successful, unlink old head's next node to avoid garbage retention.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.(package private) E
(package private) void
Gets rid of cancelled node s with original predecessor pred.int
drainTo
(Collection<? super E> c) int
drainTo
(Collection<? super E> c, int maxElements) private Object
Version of xfer for poll() and tryTransfer, which simplifies control paths both here and in xferprivate LinkedTransferQueue.QNode
Returns validated tail for use in cleaning methodsint
boolean
boolean
isEmpty()
iterator()
boolean
boolean
peek()
poll()
void
private LinkedTransferQueue.QNode
reclean()
Tries to unsplice the cancelled node held in cleanMe that was previously uncleanable because it was at tail.int
int
size()
Returns the number of elements in this queue.take()
void
(package private) LinkedTransferQueue.QNode
Return head after performing any outstanding helping stepsboolean
tryTransfer
(E e) boolean
tryTransfer
(E e, long timeout, TimeUnit unit) private Object
Puts or takes an item.Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.BlockingQueue
add, contains, remove
Methods 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:
put
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
offer
- Specified by:
offer
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
offer
-
transfer
- Throws:
InterruptedException
-
tryTransfer
- Throws:
InterruptedException
-
tryTransfer
-
take
- Specified by:
take
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
poll
- Specified by:
poll
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
poll
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<E>
-
traversalHead
LinkedTransferQueue.QNode traversalHead()Return head after performing any outstanding helping steps -
iterator
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
-
peek
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
- Overrides:
isEmpty
in 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:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this queue
-
getWaitingConsumerCount
public int getWaitingConsumerCount() -
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacity
in interfaceBlockingQueue<E>
-