peek
at a synchronous queue because an element is only
present when you try to remove it; you cannot insert an element
(using any method) unless another thread is trying to remove it;
you cannot iterate as there is nothing to iterate. The
head of the queue is the element that the first queued
inserting thread is trying to add to the queue; if there is no such
queued thread then no element is available for removal and
poll()
will return null
. For purposes of other
Collection
methods (for example contains
), a
SynchronousQueue
acts as an empty collection. This queue
does not permit null
elements.
Synchronous queues are similar to rendezvous channels used in CSP and Ada. They are well suited for handoff designs, in which an object running in one thread must sync up with an object running in another thread in order to hand it some information, event, or task.
This class supports an optional fairness policy for ordering
waiting producer and consumer threads. By default, this ordering
is not guaranteed. However, a queue constructed with fairness set
to true
grants threads access in FIFO order.
This class and its iterator implement all of the optional
methods of the Collection
and Iterator
interfaces.
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
pack-priv static class | |
pack-priv static class | |
pack-priv static class | |
pack-priv static class |
Modifier and Type | Field and Description |
---|---|
private final transient boolean | |
private ReentrantLock | |
private static final long | |
private final transient SynchronousQueue. | transferer
The transferer. |
private SynchronousQueue. | |
private SynchronousQueue. |
Access | Constructor and Description |
---|---|
public | |
public | SynchronousQueue(boolean
if true, waiting threads contend in FIFO order for
access; otherwise the order is unspecified. fair)Creates a |
Modifier and Type | Method and Description |
---|---|
public void | clear()
Overrides java. Implements java. Does nothing. |
public boolean | Returns: false the element o)Overrides java. Implements java. Always returns |
public boolean | Returns: false unless given collection is emptythe collection c)Overrides java. Implements java. Returns |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c)Implements java. 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. Removes at most the given number of available elements from this queue and adds them to the given collection. |
public boolean | Returns: true Overrides java. Implements java. Always returns |
public Iterator | Returns: an empty iteratorImplements abstract java. Implements java. Returns an empty iterator in which |
public boolean | Returns: true if successful, or false if the
specified waiting time elapses before a consumer appearsthe element to add e, long how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Inserts the specified element into this queue, waiting if necessary up to the specified wait time for another thread to receive it. |
public boolean | Returns: true if the element was added to this queue, else
false the element to add e)Implements java. Inserts the specified element into this queue, if another thread is waiting to receive it. |
public E | |
public E | Returns: the head of this queue, ornull if the
specified waiting time elapses before an element is presenthow long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it. |
public E | Returns: the head of this queue, ornull if no
element is availableImplements java. Retrieves and removes the head of this queue, if another thread is currently making an element available. |
public void | put(E
the element to add e)Implements java. Adds the specified element to this queue, waiting if necessary for another thread to receive it. |
private Object | |
public int | Returns: zeroImplements java. Always returns zero. |
public boolean | Returns: false the element to remove o)Overrides java. Implements java. Always returns |
public boolean | Returns: false the collection c)Overrides java. Implements java. Always returns |
public boolean | Returns: false the collection c)Overrides java. Implements java. Always returns |
public int | Returns: zeroImplements abstract java. Implements java. Always returns zero. |
public Spliterator | Returns: an empty spliteratorOverrides default java. Returns an empty spliterator in which calls to
|
public E | Returns: the head of this queueImplements java. Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it. |
public Object[] | Returns: a zero-length arrayOverrides java. Implements java. Returns a zero-length array. |
public <T> T[] | Returns: the specified arraythe array a)Overrides java. Implements java. Sets the zeroth element of the specified array to |
public String | |
private void | |
private Object |
fair | back to summary |
---|---|
private final transient boolean fair |
qlock | back to summary |
---|---|
private ReentrantLock qlock |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
transferer | back to summary |
---|---|
private final transient SynchronousQueue. The transferer. (See below about serialization.) |
waitingConsumers | back to summary |
---|---|
private SynchronousQueue. |
waitingProducers | back to summary |
---|---|
private SynchronousQueue. |
SynchronousQueue | back to summary |
---|---|
public SynchronousQueue() Creates a |
SynchronousQueue | back to summary |
---|---|
public SynchronousQueue(boolean fair) Creates a
|
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Does nothing.
A |
contains | back to summary |
---|---|
public boolean contains(Object o) Overrides java. Implements java. Always returns
|
containsAll | back to summary |
---|---|
public boolean containsAll(Collection<?> c) Overrides java. Implements java. Returns
|
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c) Implements java. Doc from java. Removes all available elements from this queue and adds them
to the given collection. This operation may be more
efficient than repeatedly polling this queue. A failure
encountered while attempting to add elements to
collection
|
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c, int maxElements) Implements java. Doc from java. Removes at most the given number of available elements from
this queue and adds them to the given collection. A failure
encountered while attempting to add elements to
collection
|
isEmpty | back to summary |
---|---|
public boolean isEmpty() Overrides java. Implements java. Always returns
|
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an empty iterator in which
|
offer | back to summary |
---|---|
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException Implements java. Inserts the specified element into this queue, waiting if necessary up to the specified wait time for another thread to receive it.
|
offer | back to summary |
---|---|
public boolean offer(E e) Implements java. Inserts the specified element into this queue, if another thread is waiting to receive it.
|
peek | back to summary |
---|---|
public E peek() Implements java. Always returns
|
poll | back to summary |
---|---|
public E poll(long timeout, TimeUnit unit) throws InterruptedException Implements java. Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it.
|
poll | back to summary |
---|---|
public E poll() Implements java. Retrieves and removes the head of this queue, if another thread is currently making an element available.
|
put | back to summary |
---|---|
public void put(E e) throws InterruptedException Implements java. Adds the specified element to this queue, waiting if necessary for another thread to receive it.
|
readResolve | back to summary |
---|---|
private Object readResolve() Replaces a deserialized SynchronousQueue with a fresh one with the associated fairness |
remainingCapacity | back to summary |
---|---|
public int remainingCapacity() Implements java. Always returns zero.
A
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Always returns
|
removeAll | back to summary |
---|---|
public boolean removeAll(Collection<?> c) Overrides java. Implements java. Always returns
|
retainAll | back to summary |
---|---|
public boolean retainAll(Collection<?> c) Overrides java. Implements java. Always returns
|
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Always returns zero.
A
|
spliterator | back to summary |
---|---|
public Spliterator Overrides default java. Returns an empty spliterator in which calls to
|
take | back to summary |
---|---|
public E take() throws InterruptedException Implements java. Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it.
|
toArray | back to summary |
---|---|
public Object[] toArray() Overrides java. Implements java. Returns a zero-length array.
|
toArray | back to summary |
---|---|
public <T> T[] toArray(T[] a) Overrides java. Implements java. Sets the zeroth element of the specified array to
|
toString | back to summary |
---|---|
public String toString() Overrides java. Always returns
|
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Saves this queue to a stream (that is, serializes it).
|
xfer | back to summary |
---|---|
private Object xfer(Object e, long nanos) Invokes fair or lifo transfer |
Modifier and Type | Field and Description |
---|---|
private static final long |
Access | Constructor and Description |
---|---|
pack-priv |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
FifoWaitQueue | back to summary |
---|---|
pack-priv FifoWaitQueue() |
Modifier and Type | Field and Description |
---|---|
private static final long |
Access | Constructor and Description |
---|---|
pack-priv |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
LifoWaitQueue | back to summary |
---|---|
pack-priv LifoWaitQueue() |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
private void | |
pack-priv final Object |
Transferer | back to summary |
---|---|
pack-priv Transferer() |
unspliceLifo | back to summary |
---|---|
private void unspliceLifo(LinkedTransferQueue. Unlinks node s. Same idea as Fifo version. |
xferLifo | back to summary |
---|---|
pack-priv final Object xferLifo(Object e, long ns) Puts or takes an item with lifo ordering. Loops trying: * If top (var p) exists and is already matched, pop and continue * If top has complementary type, try to fulfill by CASing item, On success pop (which will succeed unless already helped), otherwise restart. * If no possible match, unless immediate mode, push a node and wait, later unsplicing if cancelled. |
Access | Constructor and Description |
---|---|
pack-priv |
WaitQueue | back to summary |
---|---|
pack-priv WaitQueue() |