public class java.util.concurrent
SynchronousQueue<E>


Show All Login
Java SE 6
  
Extends: AbstractCollection > AbstractQueue
Implements: BlockingQueue, Serializable
Details
A blocking queue 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.
since1.5
Ethe type of elements held in this collection


Constructors
public SynchronousQueue()
Creates a SynchronousQueue with nonfair access policy.
public SynchronousQueue(boolean fair) Details
Creates a SynchronousQueue with the specified fairness policy.
fairif true, waiting threads contend in FIFO order for access; otherwise the order is unspecified.

Methods
public void clear()
Does nothing. A SynchronousQueue has no internal capacity.
public boolean contains(Object o) Details
Always returns false. A SynchronousQueue has no internal capacity.
othe element
returnfalse
public boolean containsAll(Collection c) Details
Returns false unless the given collection is empty. A SynchronousQueue has no internal capacity.
cthe collection
returnfalse unless given collection is empty
public int drainTo(Collection c) Details
ThrowsUnsupportedOperationException: if addition of elements is not supported by the specified collection
ThrowsClassCastException: if the class of an element of this queue prevents it from being added to the specified collection
ThrowsNullPointerException: if the specified collection is null
ThrowsIllegalArgumentException: if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
public int drainTo(Collection c, int maxElements) Details
ThrowsUnsupportedOperationException: if addition of elements is not supported by the specified collection
ThrowsClassCastException: if the class of an element of this queue prevents it from being added to the specified collection
ThrowsNullPointerException: if the specified collection is null
ThrowsIllegalArgumentException: if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
public Iterator iterator() Details
Returns an empty iterator in which hasNext always returns false.
returnan empty iterator
public boolean offer(Object o, long timeout, TimeUnit unit) throws InterruptedException Details
Inserts the specified element into this queue, waiting if necessary up to the specified wait time for another thread to receive it.
returntrue if successful, or false if the specified waiting time elapses before a consumer appears.
ThrowsInterruptedException: if interrupted while waiting
ThrowsNullPointerException: als het gespecifieerde element null is
public boolean offer(Object e) Details
Inserts the specified element into this queue, if another thread is waiting to receive it.
ethe element to add
returntrue if the element was added to this queue, else false
ThrowsNullPointerException: als het gespecifieerde element null is
public Object peek() Details
Always returns null. A SynchronousQueue does not return elements unless actively waited on.
returnnull
public Object poll(long timeout, TimeUnit unit) throws InterruptedException Details
Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it.
returnthe head of this queue, or null if the specified waiting time elapses before an element is present.
ThrowsInterruptedException: if interrupted while waiting
public Object poll() Details
Retrieves and removes the head of this queue, if another thread is currently making an element available.
returnthe head of this queue, or null if no element is available.
public void put(Object o) throws InterruptedException Details
Adds the specified element to this queue, waiting if necessary for another thread to receive it.
ThrowsInterruptedException: if interrupted while waiting
ThrowsNullPointerException: als het gespecifieerde element null is
public int remainingCapacity() Details
Always returns zero. A SynchronousQueue has no internal capacity.
returnzero.
public boolean remove(Object o) Details
Always returns false. A SynchronousQueue has no internal capacity.
othe element to remove
returnfalse
public boolean removeAll(Collection c) Details
Always returns false. A SynchronousQueue has no internal capacity.
cthe collection
returnfalse
public boolean retainAll(Collection c) Details
Always returns false. A SynchronousQueue has no internal capacity.
cthe collection
returnfalse
public int size() Details
Always returns zero. A SynchronousQueue has no internal capacity.
returnzero.
public Object take() throws InterruptedException Details
Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it.
returnthe head of this queue
ThrowsInterruptedException: if interrupted while waiting
public Object[] toArray() Details
Returns a zero-length array.
returna zero-length array
public Object[]<T> toArray(Object[] a) Details
Sets the zeroeth element of the specified array to null (if the array has non-zero length) and returns it.
athe array
returnthe specified array
ThrowsNullPointerException: if the specified array is null

Properties
public boolean isEmpty() Details
Always returns true. A SynchronousQueue has no internal capacity.
returntrue