Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class LinkedTransferQueue<E>

extends AbstractQueue<E>
implements TransferQueue<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.concurrent.TransferQueue, java.util.concurrent.BlockingQueue, java.util.Queue, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
java.util.concurrent.SynchronousQueue.Transferer
Type Parameters
<E>
the type of elements held in this queue
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.AbstractQueue, .Arrays, .Collection, .Iterator, .NoSuchElementException, .Objects, .Queue, .Spliterator, .Spliterators, java.util.concurrent.locks.LockSupport, java.util.concurrent.ForkJoinWorkerThread, java.util.function.Consumer, .Predicate

An unbounded TransferQueue based on linked nodes. This queue orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time for some producer.

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, and so may report inaccurate results if this collection is modified during traversal.

Bulk operations that add, remove, or examine multiple elements, such as addAll, removeIf or forEach, are not guaranteed to be performed atomically. For example, a forEach traversal concurrent with an addAll operation might observe only some of the added 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.

This class is a member of the Java Collections Framework.

Author
Doug Lea
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
LinkedTransferQueue.DualNode

Node for linked dual data structures.

pack-priv class
LinkedTransferQueue.Itr

Weakly-consistent iterator.

pack-priv class
LinkedTransferQueue.LTQSpliterator

A customized variant of Spliterators.IteratorSpliterator

Field Summary

Modifier and TypeField and Description
pack-priv transient volatile LinkedTransferQueue.DualNode
head

Unless empty (in which case possibly null), a node from which all live nodes are reachable.

pack-priv static final VarHandle
private static final int
MAX_HOPS

Tolerate this many consecutive dead nodes before CAS-collapsing.

private static final long
pack-priv static final int
SWEEP_THRESHOLD

The maximum number of estimated removal failures (sweepVotes) to tolerate before sweeping through the queue unlinking dead nodes that were initially pinned.

pack-priv transient volatile int
sweepVotes

The number of apparent failures to unsplice cancelled nodes

pack-priv static final VarHandle
pack-priv transient volatile LinkedTransferQueue.DualNode
tail

Unless null, a node from which the last node on list (that is, the unique node with node.next == null), if one exists, can be reached.

pack-priv static final VarHandle

Constructor Summary

AccessConstructor and Description
public
LinkedTransferQueue()

Creates an initially empty LinkedTransferQueue.

public
LinkedTransferQueue(Collection<? extends E>
the collection of elements to initially contain
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 TypeMethod and Description
public boolean

Returns:

true (as specified by Collection#add)
add
(E
the element to add
e
)

Overrides java.util.AbstractQueue.add.

Implements java.util.concurrent.BlockingQueue.add, java.util.Queue.add, java.util.Collection.add.

Inserts the specified element at the tail of this queue.

private boolean
bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

public void
clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this collection (optional operation).

pack-priv final LinkedTransferQueue.DualNode
pack-priv final LinkedTransferQueue.DualNode
public boolean

Returns:

true if this queue contains the specified element
contains
(Object
object to be checked for containment in this queue
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.concurrent.BlockingQueue.contains, java.util.Collection.contains.

Returns true if this queue contains the specified element.

pack-priv final int
countOfMode(boolean data)

Traverses and counts unmatched nodes of the given mode.

public int
drainTo(Collection<? super E>
the collection to transfer elements into
c
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Removes all available elements from this queue and adds them to the given collection.

public int
drainTo(Collection<? super E>
the collection to transfer elements into
c
,
int
the maximum number of elements to transfer
maxElements
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Removes at most the given number of available elements from this queue and adds them to the given collection.

pack-priv final LinkedTransferQueue.DualNode
firstDataNode()

Returns the first unmatched data node, or null if none.

public void
forEach(Consumer<? super E>
The action to be performed for each element
action
)

Overrides default java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

pack-priv void
forEachFrom(Consumer<? super E> action, LinkedTransferQueue.DualNode p)

Runs action on each element found during a traversal starting at p.

public int
getWaitingConsumerCount()

Implements java.util.concurrent.TransferQueue.getWaitingConsumerCount.

Returns an estimate of the number of consumers waiting to receive elements via take or timed poll.

public boolean
hasWaitingConsumer()

Implements java.util.concurrent.TransferQueue.hasWaitingConsumer.

Returns true if there is at least one consumer waiting to receive an element via take or timed poll.

public boolean

Returns:

true if this queue contains no elements
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this queue contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this queue in proper sequence
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

Returns an iterator over the elements in this queue in proper sequence.

public boolean

Returns:

true (as specified by BlockingQueue.offer)
offer
(E
the element to add
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingQueue.offer.

Inserts the specified element at the tail of this queue.

public boolean

Returns:

true (as specified by Queue#offer)
offer
(E
the element to add
e
)

Implements java.util.concurrent.BlockingQueue.offer, java.util.Queue.offer.

Inserts the specified element at the tail of this queue.

public E
peek()

Implements java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

public E
poll(long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingQueue.poll.

Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

public E
poll()

Implements java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

public void
put(E
the element to add
e
)

Implements java.util.concurrent.BlockingQueue.put.

Inserts the specified element at the tail of this queue.

private void
readObject(ObjectInputStream
the stream
s
)

Reconstitutes this queue from a stream (that is, deserializes it).

public int

Returns:

Integer.MAX_VALUE (as specified by BlockingQueue#remainingCapacity())
remainingCapacity
()

Implements java.util.concurrent.BlockingQueue.remainingCapacity.

Always returns Integer.MAX_VALUE because a LinkedTransferQueue is not capacity constrained.

public boolean

Returns:

true if this queue changed as a result of the call
remove
(Object
element to be removed from this queue, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.concurrent.BlockingQueue.remove, java.util.Collection.remove.

Removes a single instance of the specified element from this queue, if it is present.

public boolean
removeAll(Collection<?>
collection containing elements to be removed from this collection
c
)

Overrides java.util.AbstractCollection.removeAll.

Implements java.util.Collection.removeAll.

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

public boolean
removeIf(Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation).

public boolean
retainAll(Collection<?>
collection containing elements to be retained in this collection
c
)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.Collection.retainAll.

Retains only the elements in this collection that are contained in the specified collection (optional operation).

public int

Returns:

the number of elements in this queue
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements in this queue.

pack-priv final LinkedTransferQueue.DualNode

Returns:

pred if pred still alive and CAS succeeded; else p
skipDeadNodes
(LinkedTransferQueue.DualNode
the last known live node, or null if none
pred
,
LinkedTransferQueue.DualNode
the first dead node
c
,
LinkedTransferQueue.DualNode
the last dead node
p
,
LinkedTransferQueue.DualNode
p.next: the next live node, or null if at end
q
)

Collapses dead (matched) nodes between pred and q.

public Spliterator<E>

Returns:

a Spliterator over the elements in this queue
spliterator
()

Overrides default java.util.Collection.spliterator.

Returns a Spliterator over the elements in this queue.

pack-priv final boolean
sweepNow()

Adds a sweepVote and returns true if triggered threshold.

public E
take()

Implements java.util.concurrent.BlockingQueue.take.

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

public Object[]

Returns:

an array containing all of the elements in this queue
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this queue, in proper sequence.

public <T> T[]

Returns:

an array containing all of the elements in this queue
toArray
(T[]
the array into which the elements of the queue are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
a
)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array.

private Object[]
public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.

public void
transfer(E
the element to transfer
e
)

Implements java.util.concurrent.TransferQueue.transfer.

Transfers the element to a consumer, waiting if necessary to do so.

pack-priv final boolean
tryCasSuccessor(LinkedTransferQueue.DualNode pred, LinkedTransferQueue.DualNode c, LinkedTransferQueue.DualNode p)

Tries to CAS pred.next (or head, if pred is null) from c to p.

pack-priv final boolean
tryMatchData(LinkedTransferQueue.DualNode p, Object x)

Tries to match the given object only if p is a data node.

public boolean
tryTransfer(E
the element to transfer
e
)

Implements java.util.concurrent.TransferQueue.tryTransfer.

Transfers the element to a waiting consumer immediately, if possible.

public boolean
tryTransfer(E
the element to transfer
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.TransferQueue.tryTransfer.

Transfers the element to a consumer if it is possible to do so before the timeout elapses.

private void
unsplice(LinkedTransferQueue.DualNode
if nonnull, a node that was at one time known to be the predecessor of s (else s may have been head)
pred
,
LinkedTransferQueue.DualNode
the node to be unspliced
s
)

Unlinks (now or later) the given (non-live) node with given predecessor.

private void
writeObject(ObjectOutputStream
the stream
s
)

Saves this queue to a stream (that is, serializes it).

pack-priv final Object

Returns:

an item if matched, else e
xfer
(Object
the item or null for take
e
,
long
timeout or negative if async, 0 if immediate, Long.MAX_VALUE if untimed
ns
)

Implements all queuing methods.

Inherited from java.util.AbstractQueue:
addAllelementremove