| java.util.concurrent.SynchronousQueue
SynchronousQueue | public class SynchronousQueue extends AbstractQueue implements BlockingQueue<E>,java.io.Serializable(Code) | | A
in which each
put must wait for a take, and vice versa. A
synchronous queue does not have any internal capacity, not even a
capacity of one. You cannot peek at a synchronous queue
because an element is only present when you try to take it; you
cannot add 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 thread is trying to add to the queue; if there are no
queued threads then no element is being added and the head is
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. Fairness
generally decreases throughput but reduces variability and avoids
starvation.
This class implements all of the optional methods
of the
Collection and
Iterator interfaces.
This class is a member of the
Java Collections Framework.
since: 1.5 author: Doug Lea< Parameters: E - > the type of elements held in this collection |
Inner Class :static class EmptyIterator implements Iterator<E> | |
Constructor Summary | |
public | SynchronousQueue() Creates a SynchronousQueue with nonfair access policy. | public | SynchronousQueue(boolean fair) Creates a SynchronousQueue with specified fairness policy. |
Method Summary | |
public void | clear() Does nothing. | public boolean | contains(Object o) Always returns false. | public boolean | containsAll(Collection> c) Returns false unless given collection is empty. | public int | drainTo(Collection<? super E> c) | public int | drainTo(Collection<? super E> c, int maxElements) | public boolean | isEmpty() Always returns true. | public Iterator<E> | iterator() Returns an empty iterator in which hasNext always returns
false. | public boolean | offer(E o, long timeout, TimeUnit unit) Inserts the specified element into this queue, waiting if necessary
up to the specified wait time for another thread to receive it. | public boolean | offer(E o) Inserts the specified element into this queue, if another thread is
waiting to receive it.
Parameters: o - the element to add. | public E | peek() Always returns null. | public E | poll(long timeout, TimeUnit unit) 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 | poll() Retrieves and removes the head of this queue, if another thread
is currently making an element available. | public void | put(E o) Adds the specified element to this queue, waiting if necessary for
another thread to receive it. | public int | remainingCapacity() Always returns zero. | public boolean | remove(Object o) Always returns false. | public boolean | removeAll(Collection> c) Always returns false. | public boolean | retainAll(Collection> c) Always returns false. | public int | size() Always returns zero. | public E | take() Retrieves and removes the head of this queue, waiting if necessary
for another thread to insert it.
throws: InterruptedException - if interrupted while waiting. | public Object[] | toArray() Returns a zero-length array. | public T[] | toArray(T[] a) Sets the zeroeth element of the specified array to null
(if the array has non-zero length) and returns it. |
SynchronousQueue | public SynchronousQueue()(Code) | | Creates a SynchronousQueue with nonfair access policy.
|
SynchronousQueue | public SynchronousQueue(boolean fair)(Code) | | Creates a SynchronousQueue with specified fairness policy.
Parameters: fair - if true, threads contend in FIFO order for access;otherwise the order is unspecified. |
clear | public void clear()(Code) | | Does nothing.
A SynchronousQueue has no internal capacity.
|
contains | public boolean contains(Object o)(Code) | | Always returns false.
A SynchronousQueue has no internal capacity.
Parameters: o - the element false |
containsAll | public boolean containsAll(Collection> c)(Code) | | Returns false unless given collection is empty.
A SynchronousQueue has no internal capacity.
Parameters: c - the collection false unless given collection is empty |
isEmpty | public boolean isEmpty()(Code) | | Always returns true.
A SynchronousQueue has no internal capacity.
true |
iterator | public Iterator<E> iterator()(Code) | | Returns an empty iterator in which hasNext always returns
false.
an empty iterator |
offer | public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException(Code) | | Inserts the specified element into this queue, waiting if necessary
up to the specified wait time for another thread to receive it.
Parameters: o - the element to add Parameters: timeout - how long to wait before giving up, in units ofunit Parameters: unit - a TimeUnit determining how to interpret thetimeout parameter true if successful, or false ifthe specified waiting time elapses before a consumer appears. throws: InterruptedException - if interrupted while waiting. throws: NullPointerException - if the specified element is null. |
offer | public boolean offer(E o)(Code) | | Inserts the specified element into this queue, if another thread is
waiting to receive it.
Parameters: o - the element to add. true if it was possible to add the element tothis queue, else false throws: NullPointerException - if the specified element is null |
peek | public E peek()(Code) | | Always returns null.
A SynchronousQueue does not return elements
unless actively waited on.
null |
poll | public E poll(long timeout, TimeUnit unit) throws InterruptedException(Code) | | Retrieves and removes the head of this queue, waiting
if necessary up to the specified wait time, for another thread
to insert it.
Parameters: timeout - how long to wait before giving up, in units ofunit Parameters: unit - a TimeUnit determining how to interpret thetimeout parameter the head of this queue, or null if thespecified waiting time elapses before an element is present. throws: InterruptedException - if interrupted while waiting. |
poll | public E poll()(Code) | | Retrieves and removes the head of this queue, if another thread
is currently making an element available.
the head of this queue, or null if noelement is available. |
remainingCapacity | public int remainingCapacity()(Code) | | Always returns zero.
A SynchronousQueue has no internal capacity.
zero. |
remove | public boolean remove(Object o)(Code) | | Always returns false.
A SynchronousQueue has no internal capacity.
Parameters: o - the element to remove false |
removeAll | public boolean removeAll(Collection> c)(Code) | | Always returns false.
A SynchronousQueue has no internal capacity.
Parameters: c - the collection false |
retainAll | public boolean retainAll(Collection> c)(Code) | | Always returns false.
A SynchronousQueue has no internal capacity.
Parameters: c - the collection false |
size | public int size()(Code) | | Always returns zero.
A SynchronousQueue has no internal capacity.
zero. |
take | public E take() throws InterruptedException(Code) | | Retrieves and removes the head of this queue, waiting if necessary
for another thread to insert it.
throws: InterruptedException - if interrupted while waiting. the head of this queue |
toArray | public Object[] toArray()(Code) | | Returns a zero-length array.
a zero-length array |
toArray | public T[] toArray(T[] a)(Code) | | Sets the zeroeth element of the specified array to null
(if the array has non-zero length) and returns it.
Parameters: a - the array the specified array |
|
|