E
-元素举行此集合中的类型
public class SynchronousQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
peek
在同步队列因为元素只存在当你试图删除它;你不能插入一个元素(使用任何方法)除非另一个线程试图删除它;你不能因为没有迭代迭代。队列的头部是第一个排队的插入线程试图添加到队列的元素;如果没有这样的排队线然后没有元素可用于去除和
poll()
将返回
null
。对于其他
Collection
方法的目的(例如
contains
),一个
SynchronousQueue
作为空集合。这个队列不允许
null
元素。
同步队列类似交会用于CSP和艾达通道。它们非常适合于切换设计,其中一个线程在一个线程中运行的对象必须与另一个线程中运行的对象同步,以便将一些信息、事件或任务。
这个类支持一个可选的公平策略,用于订购等待的生产者和消费者线程。默认情况下,此顺序无法保证。然而,公平设置true
补助按照先进先出的顺序构建了一个线程访问队列。
这个类和它的迭代器实现所有的可选方法的Collection
和Iterator
接口。
这个班的一员 Java Collections Framework。
Constructor and Description |
---|
SynchronousQueue()
创建一个nonfair准入政策
SynchronousQueue 。
|
SynchronousQueue(boolean fair)
创建一个具有指定公平政策
SynchronousQueue 。
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
什么也不做。
|
boolean |
contains(Object o)
总是返回
false 。
|
boolean |
containsAll(Collection<?> c)
返回
false 除非给定集合是空的。
|
int |
drainTo(Collection<? super E> c)
从这个队列中删除所有可用的元素,并将它们添加到给定的集合中。
|
int |
drainTo(Collection<? super E> c, int maxElements)
从这个队列中移除给定数量的可用元素,并将它们添加到给定的集合中。
|
boolean |
isEmpty()
总是返回
true 。
|
Iterator<E> |
iterator()
返回一个空的迭代器,
hasNext 总是返回
false 。
|
boolean |
offer(E e)
如果另一个线程正在等待接收到该队列,则将指定的元素插入到该队列中。
|
boolean |
offer(E e, long timeout, TimeUnit unit)
将指定的元素插入到队列中,等待必要的时间到指定的等待另一个线程接收到的等待时间。
|
E |
peek()
总是返回
null 。
|
E |
poll()
检索和删除此队列的头,如果另一个线程正在使用可用的元素。
|
E |
poll(long timeout, TimeUnit unit)
检索并删除此队列的头,等待必要时到指定的等待时间,另一个线程插入它。
|
void |
put(E e)
将指定的元素添加到这个队列中,等待另一个线程接收它的必要。
|
int |
remainingCapacity()
总是返回零。
|
boolean |
remove(Object o)
总是返回
false 。
|
boolean |
removeAll(Collection<?> c)
总是返回
false 。
|
boolean |
retainAll(Collection<?> c)
总是返回
false 。
|
int |
size()
总是返回零。
|
Spliterator<E> |
spliterator()
返回的空spliterator调用
Spliterator.trySplit() 总是返回
null 。
|
E |
take()
检索并删除此队列的头,等待另一个线程是否需要插入它。
|
Object[] |
toArray()
返回一零个长度数组。
|
<T> T[] |
toArray(T[] a)
设置指定数组
null 第0个元素(如果数组长度不为零)并返回。
|
add, addAll, element, remove
toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add
addAll, equals, hashCode, parallelStream, removeIf, stream
public SynchronousQueue()
SynchronousQueue
。
public SynchronousQueue(boolean fair)
SynchronousQueue
。
fair
-如果是真的,等待的线程争先进先出的顺序访问;否则订单不详。
public void put(E e) throws InterruptedException
put
接口
BlockingQueue<E>
e
-元素添加
InterruptedException
如果中断等待
NullPointerException
-如果指定元素为null
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
offer
接口
BlockingQueue<E>
e
-元素添加
timeout
-多久才放弃等待,在单位
unit
unit
-
TimeUnit
确定如何解释
timeout
参数
true
如果成功,或
false
如果指定的等待时间流逝之前,消费者出现
InterruptedException
如果中断等待
NullPointerException
-如果指定元素为null
public boolean offer(E e)
offer
接口
BlockingQueue<E>
offer
接口
Queue<E>
e
-元素添加
true
如果元素被添加到这个队列,否则
false
NullPointerException
-如果指定元素为null
public E take() throws InterruptedException
take
接口
BlockingQueue<E>
InterruptedException
如果中断等待
public E poll(long timeout, TimeUnit unit) throws InterruptedException
poll
接口
BlockingQueue<E>
timeout
-多久才放弃等待,在单位
unit
unit
-
TimeUnit
确定如何解释
timeout
参数
null
如果指定的等待时间过去之前的元素是存在的
InterruptedException
如果中断等待
public E poll()
public boolean isEmpty()
true
。一个
SynchronousQueue
没有内部能力。
isEmpty
接口
Collection<E>
isEmpty
方法重写,继承类
AbstractCollection<E>
true
public int size()
SynchronousQueue
没有内部能力。
size
接口
Collection<E>
size
方法重写,继承类
AbstractCollection<E>
public int remainingCapacity()
SynchronousQueue
没有内部能力。
remainingCapacity
接口
BlockingQueue<E>
public void clear()
SynchronousQueue
没有内部能力。
clear
接口
Collection<E>
clear
方法重写,继承类
AbstractQueue<E>
public boolean contains(Object o)
false
SynchronousQueue
没有内部能力。
contains
接口
Collection<E>
contains
接口
BlockingQueue<E>
contains
方法重写,继承类
AbstractCollection<E>
o
-元素
false
public boolean remove(Object o)
false
。一个
SynchronousQueue
没有内部能力。
remove
接口
Collection<E>
remove
接口
BlockingQueue<E>
remove
方法重写,继承类
AbstractCollection<E>
o
-要移除的元素
false
public boolean containsAll(Collection<?> c)
false
除非给定集合是空的。一个
SynchronousQueue
没有内部能力。
containsAll
接口
Collection<E>
containsAll
方法重写,继承类
AbstractCollection<E>
c
-集合
false
除非给定集合是空的
AbstractCollection.contains(Object)
public boolean removeAll(Collection<?> c)
false
SynchronousQueue
没有内部能力。
removeAll
接口
Collection<E>
removeAll
方法重写,继承类
AbstractCollection<E>
c
-集合
false
AbstractCollection.remove(Object)
,
AbstractCollection.contains(Object)
public boolean retainAll(Collection<?> c)
false
。一个
SynchronousQueue
没有内部能力。
retainAll
接口
Collection<E>
retainAll
方法重写,继承类
AbstractCollection<E>
c
-集合
false
AbstractCollection.remove(Object)
,
AbstractCollection.contains(Object)
public E peek()
null
。一个
SynchronousQueue
不返回元素除非积极等待。
public Iterator<E> iterator()
hasNext
总是返回
false
。
iterator
接口
Iterable<E>
iterator
接口
Collection<E>
iterator
方法重写,继承类
AbstractCollection<E>
public Spliterator<E> spliterator()
Spliterator.trySplit()
总是返回
null
。
spliterator
接口
Iterable<E>
spliterator
接口
Collection<E>
public Object[] toArray()
toArray
接口
Collection<E>
toArray
方法重写,继承类
AbstractCollection<E>
public <T> T[] toArray(T[] a)
null
指定数组的第0个元素(如果数组长度不为零)并返回。
toArray
接口
Collection<E>
toArray
方法重写,继承类
AbstractCollection<E>
T
-数组的运行时类型包含集合
a
-阵列
NullPointerException
-如果指定的数组是空的
public int drainTo(Collection<? super E> c)
BlockingQueue
c
添加元素可能会导致元素在没有遭遇过失败,或集合时相关的异常被抛出。试图排队列本身造成
IllegalArgumentException
。此外,当操作正在进行中时,此操作的行为是不确定的,如果指定的集合被修改了。
drainTo
接口
BlockingQueue<E>
c
-收集传递元素
UnsupportedOperationException
如果添加元素不按指定集合的支持
ClassCastException
-如果该队列的元素类型阻止其加入指定集合
NullPointerException
-如果指定集合为空
IllegalArgumentException
-如果指定集合的队列,该队列或一个元素的一些特性可以防止它被添加到指定的集合
public int drainTo(Collection<? super E> c, int maxElements)
BlockingQueue
c
添加元素可能会导致元素在没有遭遇过失败,或集合时相关的异常被抛出。试图排队列本身造成
IllegalArgumentException
。此外,当操作正在进行中时,此操作的行为是不确定的,如果指定的集合被修改了。
drainTo
接口
BlockingQueue<E>
c
-收集传递元素
maxElements
-元素的最大数量的转移
UnsupportedOperationException
如果添加元素不按指定集合的支持
ClassCastException
-如果该队列的元素类型阻止其加入指定集合
NullPointerException
-如果指定集合为空
IllegalArgumentException
-如果指定集合的队列,该队列或一个元素的一些特性可以防止它被添加到指定的集合
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.