| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
SynchronousQueue q = new SynchronousQueue();
Based on 113 examples
public class SynchronousQueue extends AbstractQueue implements BlockingQueue, Serializable
A {@linkplain BlockingQueue 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 {@link Collection} and {@link Iterator} interfaces.
This class is a member of the Java Collections Framework.
Constructor Summary | |
---|---|
Creates a SynchronousQueue with nonfair access policy. |
|
SynchronousQueue(boolean fair) Creates a SynchronousQueue with the specified fairness policy. |
Method Summary | |
---|---|
void |
clear() Does nothing. |
boolean |
Always returns false. |
boolean |
Returns false unless the given collection is empty. |
int |
|
int |
drainTo(Collection c, int maxElements) |
boolean |
isEmpty() Always returns true. |
Iterator |
iterator() Returns an empty iterator in which hasNext always returns false. |
boolean |
Inserts the specified element into this queue, if another thread is waiting to receive it. |
boolean |
Inserts the specified element into this queue, waiting if necessary up to the specified wait time for another thread to receive it. |
Object |
peek() Always returns null. |
Object |
poll() Retrieves and removes the head of this queue, if another thread is currently making an element available. |
Object |
Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it. |
void |
Adds the specified element to this queue, waiting if necessary for another thread to receive it. |
int |
Always returns zero. |
boolean |
Always returns false. |
boolean |
Always returns false. |
boolean |
Always returns false. |
int |
size() Always returns zero. |
Object |
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. |
Object[] |
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, clear, element, remove |
Methods inherited from class java.util.AbstractCollection |
---|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SynchronousQueue()
public SynchronousQueue(boolean fair)
fair
- if true, waiting threads contend in FIFO order for
access; otherwise the order is unspecified.Method Detail |
---|
public void clear()
clear
in class AbstractQueue
public boolean contains(Object o)
contains
in class AbstractCollection
o
- the elementpublic boolean containsAll(Collection c)
containsAll
in class AbstractCollection
c
- the collectionpublic int drainTo(Collection c)
c
public int drainTo(Collection c, int maxElements)
c
maxElements
public boolean isEmpty()
isEmpty
in class AbstractCollection
public Iterator iterator()
iterator
in class AbstractCollection
public boolean offer(Object e)
e
- the element to addpublic boolean offer(Object o, long timeout, TimeUnit unit) throws InterruptedException
o
timeout
unit
InterruptedException
- {@inheritDoc}public Object peek()
public Object poll()
public Object poll(long timeout, TimeUnit unit) throws InterruptedException
timeout
unit
InterruptedException
- {@inheritDoc}public void put(Object o) throws InterruptedException
o
InterruptedException
- {@inheritDoc}public int remainingCapacity()
public boolean remove(Object o)
remove
in class AbstractCollection
o
- the element to removepublic boolean removeAll(Collection c)
removeAll
in class AbstractCollection
c
- the collectionpublic boolean retainAll(Collection c)
retainAll
in class AbstractCollection
c
- the collectionpublic int size()
size
in class AbstractCollection
public Object take() throws InterruptedException
InterruptedException
- {@inheritDoc}public Object[] toArray()
toArray
in class AbstractCollection
public Object[] toArray(Object[] a)
toArray
in class AbstractCollection
a
- the array
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |