public class SynchronousQueue
extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
| java.lang.Object | |||
| java.util.AbstractCollection<E> | |||
| java.util.AbstractQueue<E> | |||
| java.util.concurrent.SynchronousQueue<E> | |||
一个blocking queue ,其中每个插入操作必须等待另一个线程的相应删除操作,反之亦然。 同步队列没有任何内部容量,甚至没有一个容量。 您不能在同步队列中使用peek ,因为只有在尝试删除元素时才会显示该元素; 除非另一个线程试图删除它,否则不能插入元素(使用任何方法); 你不能迭代,因为没有什么可以迭代。 队列的头部是第一个排队插入线程尝试添加到队列中的元素; 如果没有这样的排队线程,则没有元素可用于移除,并且poll()将返回null 。 对于其他Collection方法(例如contains )而言, SynchronousQueue充当空集合。 该队列不允许null元素。
同步队列与CSP和Ada中使用的汇合信道类似。 它们非常适合于越区切换设计,其中在一个线程中运行的对象必须与在另一个线程中运行的对象同步,以便交付一些信息,事件或任务。
该类支持订购等待生产者和消费者线程的可选公平策略。 默认情况下,这个顺序不能保证。 但是,公平性设置为true的队列将按照FIFO顺序授予线程访问权限。
该类及其迭代器实现 Collection和 Iterator接口的所有 可选方法。
Public constructors |
|
|---|---|
SynchronousQueue() 使用非公平访问策略创建 |
|
SynchronousQueue(boolean fair) 使用指定的公平策略创建一个 |
|
公共方法(Public methods) |
|
|---|---|
void |
clear() 什么也没做。 |
boolean |
contains(Object o) 始终返回 |
boolean |
containsAll(Collection<?> c) 除非给定的集合是空的,否则返回 |
int |
drainTo(Collection<? super E> c, int maxElements) 最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 |
int |
drainTo(Collection<? super E> c) 从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 |
boolean |
isEmpty() 始终返回 |
Iterator<E> |
iterator() 返回一个空的迭代器,其中 |
boolean |
offer(E e) 如果另一个线程正在等待接收它,则将指定的元素插入此队列中。 |
boolean |
offer(E e, long timeout, TimeUnit unit) 将指定的元素插入此队列中,如果需要,可以等待另一个线程接收指定的元素。 |
E |
peek() 始终返回 |
E |
poll(long timeout, TimeUnit unit) 检索并删除此队列的头部,如果需要等待指定的等待时间,则另一个线程将其插入。 |
E |
poll() 如果另一个线程当前正在创建一个元素,则检索并删除此队列的头部。 |
void |
put(E e) 将指定的元素添加到此队列中,等待其他线程接收它。 |
int |
remainingCapacity() 始终返回零。 |
boolean |
remove(Object o) 始终返回 |
boolean |
removeAll(Collection<?> c) 始终返回 |
boolean |
retainAll(Collection<?> c) 始终返回 |
int |
size() 始终返回零。 |
Spliterator<E> |
spliterator() 返回一个空分隔符,其中调用 |
E |
take() 检索并删除此队列的头部,如果有必要,等待另一个线程插入它。 |
<T> T[] |
toArray(T[] a) 将指定数组的第零个元素设置为 |
Object[] |
toArray() 返回一个零长度的数组。 |
String |
toString() 始终返回 |
继承方法(Inherited methods) |
|
|---|---|
java.util.AbstractQueue
|
|
java.util.AbstractCollection
|
|
java.lang.Object
|
|
java.util.Queue
|
|
java.util.Collection
|
|
java.util.concurrent.BlockingQueue
|
|
java.lang.Iterable
|
|
SynchronousQueue (boolean fair)
使用指定的公平策略创建 SynchronousQueue 。
| 参数(Parameters) | |
|---|---|
fair |
boolean: if true, waiting threads contend in FIFO order for access; otherwise the order is unspecified. |
boolean contains (Object o)
始终返回false 。 A SynchronousQueue没有内部容量。
| 参数(Parameters) | |
|---|---|
o |
Object: the element |
| 返回(Returns) | |
|---|---|
boolean |
false |
boolean containsAll (Collection<?> c)
除非给定的集合是空的,否则返回false 。 A SynchronousQueue没有内部容量。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection |
| 返回(Returns) | |
|---|---|
boolean |
false unless given collection is empty |
int drainTo (Collection<? super E> c, int maxElements)
最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联异常时集合中的任何一个或两个集合。 尝试将队列IllegalArgumentException自身导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection to transfer elements into |
maxElements |
int: the maximum number of elements to transfer |
| 返回(Returns) | |
|---|---|
int |
the number of elements transferred |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
|
int drainTo (Collection<? super E> c)
从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 该操作可能比重复轮询该队列更有效。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联异常时集合中的任何一个集合或两个集合都不在元素中。 尝试将队列IllegalArgumentException自身导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection to transfer elements into |
| 返回(Returns) | |
|---|---|
int |
the number of elements transferred |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
|
boolean isEmpty ()
始终返回true 。 A SynchronousQueue没有内部容量。
| 返回(Returns) | |
|---|---|
boolean |
true |
Iterator<E> iterator ()
返回一个空的迭代器,其中 hasNext始终返回 false 。
| 返回(Returns) | |
|---|---|
Iterator<E> |
an empty iterator |
boolean offer (E e)
如果另一个线程正在等待接收它,则将指定的元素插入此队列中。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true if the element was added to this queue, else false |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
boolean offer (E e,
long timeout,
TimeUnit unit)
将指定的元素插入此队列中,如果需要,可以等待另一个线程接收指定的元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
timeout |
long: how long to wait before giving up, in units of unit |
unit |
TimeUnit: a TimeUnit determining how to interpret the timeout parameter |
| 返回(Returns) | |
|---|---|
boolean |
true if successful, or false if the specified waiting time elapses before a consumer appears |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
|
NullPointerException |
|
E poll (long timeout,
TimeUnit unit)
检索并删除此队列的头部,如果需要等待指定的等待时间,则另一个线程将其插入。
| 参数(Parameters) | |
|---|---|
timeout |
long: how long to wait before giving up, in units of unit |
unit |
TimeUnit: a TimeUnit determining how to interpret the timeout parameter |
| 返回(Returns) | |
|---|---|
E |
the head of this queue, or null if the specified waiting time elapses before an element is present |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
|
E poll ()
如果另一个线程当前正在创建一个元素,则检索并删除此队列的头部。
| 返回(Returns) | |
|---|---|
E |
the head of this queue, or null if no element is available |
void put (E e)
将指定的元素添加到此队列中,等待其他线程接收它。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
|
NullPointerException |
|
int remainingCapacity ()
始终返回零。 A SynchronousQueue没有内部容量。
| 返回(Returns) | |
|---|---|
int |
zero |
boolean remove (Object o)
始终返回false 。 A SynchronousQueue没有内部容量。
| 参数(Parameters) | |
|---|---|
o |
Object: the element to remove |
| 返回(Returns) | |
|---|---|
boolean |
false |
boolean removeAll (Collection<?> c)
总是返回false 。 A SynchronousQueue没有内部容量。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection |
| 返回(Returns) | |
|---|---|
boolean |
false |
boolean retainAll (Collection<?> c)
始终返回false 。 A SynchronousQueue没有内部容量。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection |
| 返回(Returns) | |
|---|---|
boolean |
false |
Spliterator<E> spliterator ()
返回一个空分隔符,其中调用 trySplit()始终返回 null 。
| 返回(Returns) | |
|---|---|
Spliterator<E> |
an empty spliterator |
E take ()
检索并删除此队列的头部,如果有必要,等待另一个线程插入它。
| 返回(Returns) | |
|---|---|
E |
the head of this queue |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
|
T[] toArray (T[] a)
将指定数组的第零个元素设置为 null (如果该数组的长度不为零)并将其返回。
| 参数(Parameters) | |
|---|---|
a |
T: the array |
| 返回(Returns) | |
|---|---|
T[] |
the specified array |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified array is null |
Object[] toArray ()
返回一个零长度的数组。
| 返回(Returns) | |
|---|---|
Object[] |
a zero-length array |