| |
|
| java.util.concurrent.SynchronousQueue
SynchronousQueue | public class SynchronousQueue extends AbstractQueue implements BlockingQueue<E>,java.io.Serializable(Code) | | A
in which each insert
operation must wait for a corresponding remove operation by another
thread, 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 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.
since: 1.5 author: Doug Lea and Bill Scherer and Michael Scott< Parameters: E - > the type of elements held in this collection |
Inner Class :abstract static class Transferer | |
Inner Class :final static class TransferStack extends Transferer | |
Inner Class :final static class TransferQueue extends Transferer | |
Inner Class :static class LifoWaitQueue extends WaitQueue | |
Inner Class :static class FifoWaitQueue extends WaitQueue | |
Field Summary | |
final static int | NCPUS | final static int | maxTimedSpins The 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. | final static int | maxUntimedSpins The number of times to spin before blocking in untimed waits. | final static long | spinForTimeoutThreshold The number of nanoseconds for which it is faster to spin
rather than to use timed park. |
Constructor Summary | |
public | SynchronousQueue() Creates a SynchronousQueue with nonfair access policy. | public | SynchronousQueue(boolean fair) Creates a SynchronousQueue with the 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 the 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 e) Inserts the specified element into this queue, if another thread is
waiting to receive it. | 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. | 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. |
NCPUS | final static int NCPUS(Code) | | The number of CPUs, for spin control
|
maxTimedSpins | final static int maxTimedSpins(Code) | | The 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 | final static int maxUntimedSpins(Code) | | The 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 | final static long spinForTimeoutThreshold(Code) | | The number of nanoseconds for which it is faster to spin
rather than to use timed park. A rough estimate suffices.
|
SynchronousQueue | public SynchronousQueue()(Code) | | Creates a SynchronousQueue with nonfair access policy.
|
SynchronousQueue | public SynchronousQueue(boolean fair)(Code) | | Creates a SynchronousQueue with the specified fairness policy.
Parameters: fair - if true, waiting threads contend in FIFO order foraccess; 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 the 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.
true if successful, or false if thespecified waiting time elapses before a consumer appears. throws: InterruptedException - throws: NullPointerException - |
offer | public boolean offer(E e)(Code) | | Inserts the specified element into this queue, if another thread is
waiting to receive it.
Parameters: e - the element to add true if the element was added to this queue, elsefalse 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.
the head of this queue, or null if thespecified waiting time elapses before an element is present. throws: InterruptedException - |
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. |
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 throws: NullPointerException - if the specified array is null |
|
|
|