SynchronousQueue (Java 2 Platform SE 5.0) (original) (raw)


java.util.concurrent

Class SynchronousQueue

java.lang.Object extended by java.util.AbstractCollection extended by java.util.AbstractQueue extended by java.util.concurrent.SynchronousQueue

Type Parameters:

E - the type of elements held in this collection

All Implemented Interfaces:

Serializable, Iterable, Collection, BlockingQueue, Queue


public class SynchronousQueue

extends AbstractQueue

implements BlockingQueue, Serializable

A blocking queue in which eachput 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 isnull. 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 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

See Also:

Serialized Form


Constructor Summary
SynchronousQueue() Creates a SynchronousQueue with nonfair access policy.
SynchronousQueue(boolean fair) Creates a SynchronousQueue with specified fairness policy.
Method Summary
void clear() Does nothing.
boolean contains(Object o) Always returns false.
boolean containsAll(Collection<?> c) Returns false unless given collection is empty.
int drainTo(Collection<? super E> c) Removes all available elements from this queue and adds them into the given collection.
int [drainTo](../../../java/util/concurrent/SynchronousQueue.html#drainTo%28java.util.Collection, int%29)(Collection<? super E> c, int maxElements) Removes at most the given number of available elements from this queue and adds them into the given collection.
boolean isEmpty() Always returns true.
Iterator<E> iterator() Returns an empty iterator in which hasNext always returnsfalse.
boolean offer(E o) Inserts the specified element into this queue, if another thread is waiting to receive it.
boolean [offer](../../../java/util/concurrent/SynchronousQueue.html#offer%28E, long, java.util.concurrent.TimeUnit%29)(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.
E peek() Always returns null.
E poll() Retrieves and removes the head of this queue, if another thread is currently making an element available.
E [poll](../../../java/util/concurrent/SynchronousQueue.html#poll%28long, java.util.concurrent.TimeUnit%29)(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.
void put(E o) Adds the specified element to this queue, waiting if necessary for another thread to receive it.
int remainingCapacity() Always returns zero.
boolean remove(Object o) Always returns false.
boolean removeAll(Collection<?> c) Always returns false.
boolean retainAll(Collection<?> c) Always returns false.
int size() Always returns zero.
E take() Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it.
Object[] toArray() Returns a zero-length array.
T[] toArray(T[] a) Sets the zeroeth element of the specified array to null (if the array has non-zero length) and returns it.
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
Methods inherited from class java.util.AbstractCollection
toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../../java/lang/Object.html#wait%28long, int%29)
Methods inherited from interface java.util.concurrent.BlockingQueue
add
Methods inherited from interface java.util.Queue
element, remove
Methods inherited from interface java.util.Collection
addAll, equals, hashCode
Constructor Detail

SynchronousQueue

public SynchronousQueue()

Creates a SynchronousQueue with nonfair access policy.


SynchronousQueue

public SynchronousQueue(boolean fair)

Creates a SynchronousQueue with specified fairness policy.

Parameters:

fair - if true, threads contend in FIFO order for access; otherwise the order is unspecified.

Method Detail

put

public void put(E o) throws InterruptedException

Adds the specified element to this queue, waiting if necessary for another thread to receive it.

Specified by:

[put](../../../java/util/concurrent/BlockingQueue.html#put%28E%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

o - the element to add

Throws:

[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") - if interrupted while waiting.

[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang") - if the specified element is null.


offer

public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException

Inserts the specified element into this queue, waiting if necessary up to the specified wait time for another thread to receive it.

Specified by:

[offer](../../../java/util/concurrent/BlockingQueue.html#offer%28E, long, java.util.concurrent.TimeUnit%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

o - the element to add

timeout - how long to wait before giving up, in units ofunit

unit - a TimeUnit determining how to interpret thetimeout parameter

Returns:

true if successful, or false if the specified waiting time elapses before a consumer appears.

Throws:

[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") - if interrupted while waiting.

[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang") - if the specified element is null.


take

public E take() throws InterruptedException

Retrieves and removes the head of this queue, waiting if necessary for another thread to insert it.

Specified by:

[take](../../../java/util/concurrent/BlockingQueue.html#take%28%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

the head of this queue

Throws:

[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") - if interrupted while waiting.


poll

public E poll(long timeout, TimeUnit unit) throws InterruptedException

Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it.

Specified by:

[poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long, java.util.concurrent.TimeUnit%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

timeout - how long to wait before giving up, in units ofunit

unit - a TimeUnit determining how to interpret thetimeout parameter

Returns:

the head of this queue, or null if the specified waiting time elapses before an element is present.

Throws:

[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") - if interrupted while waiting.


offer

public boolean offer(E o)

Inserts the specified element into this queue, if another thread is waiting to receive it.

Specified by:

[offer](../../../java/util/concurrent/BlockingQueue.html#offer%28E%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Specified by:

[offer](../../../java/util/Queue.html#offer%28E%29) in interface [Queue](../../../java/util/Queue.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

o - the element to add.

Returns:

true if it was possible to add the element to this queue, else false

Throws:

[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang") - if the specified element is null


poll

public E poll()

Retrieves and removes the head of this queue, if another thread is currently making an element available.

Specified by:

[poll](../../../java/util/Queue.html#poll%28%29) in interface [Queue](../../../java/util/Queue.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

the head of this queue, or null if no element is available.


isEmpty

public boolean isEmpty()

Always returns true. A SynchronousQueue has no internal capacity.

Specified by:

[isEmpty](../../../java/util/Collection.html#isEmpty%28%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[isEmpty](../../../java/util/AbstractCollection.html#isEmpty%28%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

true


size

public int size()

Always returns zero. A SynchronousQueue has no internal capacity.

Specified by:

[size](../../../java/util/Collection.html#size%28%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Specified by:

[size](../../../java/util/AbstractCollection.html#size%28%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

zero.


remainingCapacity

public int remainingCapacity()

Always returns zero. A SynchronousQueue has no internal capacity.

Specified by:

[remainingCapacity](../../../java/util/concurrent/BlockingQueue.html#remainingCapacity%28%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

zero.


clear

public void clear()

Does nothing. A SynchronousQueue has no internal capacity.

Specified by:

[clear](../../../java/util/Collection.html#clear%28%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[clear](../../../java/util/AbstractQueue.html#clear%28%29) in class [AbstractQueue](../../../java/util/AbstractQueue.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>


contains

public boolean contains(Object o)

Always returns false. A SynchronousQueue has no internal capacity.

Specified by:

[contains](../../../java/util/Collection.html#contains%28java.lang.Object%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[contains](../../../java/util/AbstractCollection.html#contains%28java.lang.Object%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

o - the element

Returns:

false


remove

public boolean remove(Object o)

Always returns false. A SynchronousQueue has no internal capacity.

Specified by:

[remove](../../../java/util/Collection.html#remove%28java.lang.Object%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[remove](../../../java/util/AbstractCollection.html#remove%28java.lang.Object%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

o - the element to remove

Returns:

false


containsAll

public boolean containsAll(Collection<?> c)

Returns false unless given collection is empty. A SynchronousQueue has no internal capacity.

Specified by:

[containsAll](../../../java/util/Collection.html#containsAll%28java.util.Collection%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[containsAll](../../../java/util/AbstractCollection.html#containsAll%28java.util.Collection%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

c - the collection

Returns:

false unless given collection is empty

See Also:

AbstractCollection.contains(Object)


removeAll

public boolean removeAll(Collection<?> c)

Always returns false. A SynchronousQueue has no internal capacity.

Specified by:

[removeAll](../../../java/util/Collection.html#removeAll%28java.util.Collection%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[removeAll](../../../java/util/AbstractCollection.html#removeAll%28java.util.Collection%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

c - the collection

Returns:

false

See Also:

AbstractCollection.remove(Object), AbstractCollection.contains(Object)


retainAll

public boolean retainAll(Collection<?> c)

Always returns false. A SynchronousQueue has no internal capacity.

Specified by:

[retainAll](../../../java/util/Collection.html#retainAll%28java.util.Collection%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[retainAll](../../../java/util/AbstractCollection.html#retainAll%28java.util.Collection%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

c - the collection

Returns:

false

See Also:

AbstractCollection.remove(Object), AbstractCollection.contains(Object)


peek

public E peek()

Always returns null. A SynchronousQueue does not return elements unless actively waited on.

Specified by:

[peek](../../../java/util/Queue.html#peek%28%29) in interface [Queue](../../../java/util/Queue.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

null


iterator

public Iterator<E> iterator()

Returns an empty iterator in which hasNext always returnsfalse.

Specified by:

[iterator](../../../java/lang/Iterable.html#iterator%28%29) in interface [Iterable](../../../java/lang/Iterable.html "interface in java.lang")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Specified by:

[iterator](../../../java/util/Collection.html#iterator%28%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Specified by:

[iterator](../../../java/util/AbstractCollection.html#iterator%28%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

an empty iterator


toArray

public Object[] toArray()

Returns a zero-length array.

Specified by:

[toArray](../../../java/util/Collection.html#toArray%28%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[toArray](../../../java/util/AbstractCollection.html#toArray%28%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Returns:

a zero-length array


toArray

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.

Specified by:

[toArray](../../../java/util/Collection.html#toArray%28T[]%29) in interface [Collection](../../../java/util/Collection.html "interface in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Overrides:

[toArray](../../../java/util/AbstractCollection.html#toArray%28T[]%29) in class [AbstractCollection](../../../java/util/AbstractCollection.html "class in java.util")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

a - the array

Returns:

the specified array


drainTo

public int drainTo(Collection<? super E> c)

Description copied from interface: [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection%29)

Removes all available elements from this queue and adds them into the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result inIllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Specified by:

[drainTo](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

c - the collection to transfer elements into

Returns:

the number of elements transferred.


drainTo

public int drainTo(Collection<? super E> c, int maxElements)

Description copied from interface: [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection, int%29)

Removes at most the given number of available elements from this queue and adds them into the given collection. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result inIllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Specified by:

[drainTo](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection, int%29) in interface [BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent")<[E](../../../java/util/concurrent/SynchronousQueue.html "type parameter in SynchronousQueue")>

Parameters:

c - the collection to transfer elements into

maxElements - the maximum number of elements to transfer

Returns:

the number of elements transferred.



Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.