- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- java.util.concurrent.SynchronousQueue<E>
-
- 参数类型
-
E- 保存在此队列中的元素的类型
- All Implemented Interfaces:
-
Serializable,Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
public class SynchronousQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
A blocking queue其中每个插入操作必须等待另一个线程相应的删除操作,反之亦然。 同步队列没有任何内部容量,甚至没有一个容量。 您不能在同步队列peek因为一个元素只有当您尝试删除它时才存在; 您无法插入元素(使用任何方法),除非另有线程正在尝试删除它; 你不能迭代,因为没有什么可以迭代。 队列的头部是第一个排队的插入线程尝试添加到队列中的元素; 如果没有这样排队的线程,那么没有元素可用于删除,poll()将返回null。 为了其他Collection方法(例如contains)的目的,SynchronousQueue用作空集合。 此队列不允许null元素。同步队列类似于CSP和Ada中使用的会合通道。 它们非常适用于切换设计,其中运行在一个线程中的对象必须与在另一个线程中运行的对象同步,以便交付一些信息,事件或任务。
此类支持可选的公平策略,用于订购等待的生产者和消费者线程。 默认情况下,此订单不能保证。 但是,将公平设置为
true的队列以FIFO顺序授予线程访问权限。该类及其迭代器实现了
Collection和Iterator接口的所有可选方法。这个班是Java Collections Framework的会员。
- 从以下版本开始:
- 1.5
- 另请参见:
- Serialized Form
-
-
构造方法摘要
构造方法 Constructor 描述 SynchronousQueue()创建一个SynchronousQueue与非访问策略。SynchronousQueue(boolean fair)创建具有指定公平政策的SynchronousQueue。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidclear()什么也没做。booleancontains(Object o)始终返回false。booleancontainsAll(Collection<?> c)返回false除非给定的集合为空。intdrainTo(Collection<? super E> c)从该队列中删除所有可用的元素,并将它们添加到给定的集合中。intdrainTo(Collection<? super E> c, int maxElements)最多从该队列中删除给定数量的可用元素,并将它们添加到给定的集合中。booleanisEmpty()始终返回true。Iterator<E>iterator()返回一个空的迭代器,其中hasNext总是返回false。booleanoffer(E e)如果另一个线程正在等待接收,则将指定的元素插入到此队列中。booleanoffer(E e, long timeout, TimeUnit unit)将指定的元素插入到此队列中,如果需要,等待另一个线程接收到的指定等待时间。Epeek()始终返回null。Epoll()如果另一个线程正在使一个元素可用,则检索并删除此队列的头。Epoll(long timeout, TimeUnit unit)检索并删除此队列的头,如果需要等待指定的等待时间,另一个线程插入它。voidput(E e)将指定的元素添加到此队列,等待另一个线程接收它。intremainingCapacity()始终返回零。booleanremove(Object o)始终返回false。booleanremoveAll(Collection<?> c)始终返回false。booleanretainAll(Collection<?> c)始终返回false。intsize()始终返回零。Spliterator<E>spliterator()返回一个空的分配器,其中对Spliterator.trySplit()的调用始终返回null。Etake()检索并删除此队列的头,等待另一个线程插入它。Object[]toArray()返回零长度数组。<T> T[]toArray(T[] a)将指定数组的第零个元素设置为null(如果数组的长度不为零)并返回。StringtoString()始终返回"[]"。-
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
-
Methods inherited from interface java.util.concurrent.BlockingQueue
add
-
Methods inherited from interface java.util.Collection
addAll, equals, hashCode, parallelStream, removeIf, stream
-
-
-
-
方法详细信息
-
put
public void put(E e) throws InterruptedException
将指定的元素添加到此队列,等待另一个线程接收它。- Specified by:
-
put在接口BlockingQueue<E> - 参数
-
e- 要添加的元素 - 异常
-
InterruptedException- 如果在等待时中断 -
NullPointerException- 如果指定的元素为空
-
offer
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
将指定的元素插入到此队列中,如果需要,等待另一个线程接收到的指定等待时间。- Specified by:
-
offer在接口BlockingQueue<E> - 参数
-
e- 要添加的元素 -
timeout- 放弃之前等待多久,以unit为单位 -
unit- aTimeUnit确定如何解释timeout参数 - 结果
-
true如果成功,或false如果在消费者出现之前经过了指定的等待时间 - 异常
-
InterruptedException- 如果在等待时中断 -
NullPointerException- 如果指定的元素为空
-
offer
public boolean offer(E e)
如果另一个线程正在等待接收,则将指定的元素插入到此队列中。- Specified by:
-
offer在接口BlockingQueue<E> - Specified by:
-
offer在接口Queue<E> - 参数
-
e- 要添加的元素 - 结果
-
true如果元素被添加到该队列,否则为false - 异常
-
NullPointerException- 如果指定的元素为空
-
take
public E take() throws InterruptedException
检索并删除此队列的头,等待另一个线程插入它。- Specified by:
-
take在接口BlockingQueue<E> - 结果
- 这个队列的头
- 异常
-
InterruptedException- 如果等待中断
-
poll
public E poll(long timeout, TimeUnit unit) throws InterruptedException
检索并删除此队列的头,如果需要等待指定的等待时间,另一个线程插入它。- Specified by:
-
poll在接口BlockingQueue<E> - 参数
-
timeout- 放弃之前等待多久,以unit为单位 -
unit- aTimeUnit确定如何解释timeout参数 - 结果
-
该队列的头部,如果在元素存在之前经过了指定的等待时间,
null - 异常
-
InterruptedException- 如果中断等待
-
poll
public E poll()
如果另一个线程正在使一个元素可用,则检索并删除此队列的头。
-
isEmpty
public boolean isEmpty()
始终返回true。 ASynchronousQueue没有内部容量。- Specified by:
-
isEmpty在接口Collection<E> - 重写:
-
isEmptyAbstractCollection<E> - 结果
-
true
-
size
public int size()
始终返回零。 ASynchronousQueue没有内部容量。- Specified by:
-
size在接口Collection<E> - Specified by:
-
size在AbstractCollection<E> - 结果
- 零
-
remainingCapacity
public int remainingCapacity()
始终返回零。 ASynchronousQueue没有内部容量。- Specified by:
-
remainingCapacity在接口BlockingQueue<E> - 结果
- 零
-
clear
public void clear()
什么也没做。 ASynchronousQueue没有内部容量。- Specified by:
-
clear在接口Collection<E> - 重写:
-
clear在AbstractQueue<E>
-
contains
public boolean contains(Object o)
始终返回false。 ASynchronousQueue没有内部容量。- Specified by:
-
contains在接口BlockingQueue<E> - Specified by:
-
contains在接口Collection<E> - 重写:
-
contains在AbstractCollection<E> - 参数
-
o- 元素 - 结果
-
false
-
remove
public boolean remove(Object o)
始终返回false。 ASynchronousQueue没有内部容量。- Specified by:
-
remove在接口BlockingQueue<E> - Specified by:
-
remove接口Collection<E> - 重写:
-
remove在AbstractCollection<E> - 参数
-
o- 要删除的元素 - 结果
-
false
-
containsAll
public boolean containsAll(Collection<?> c)
返回false除非给定的集合为空。 ASynchronousQueue没有内部容量。- Specified by:
-
containsAll在接口Collection<E> - 重写:
-
containsAll在AbstractCollection<E> - 参数
-
c- 收藏 - 结果
-
false除非给定的集合是空的 - 另请参见:
-
AbstractCollection.contains(Object)
-
removeAll
public boolean removeAll(Collection<?> c)
始终返回false。 ASynchronousQueue没有内部容量。- Specified by:
-
removeAll在接口Collection<E> - 重写:
-
removeAll在AbstractCollection<E> - 参数
-
c- 收藏 - 结果
-
false - 另请参见:
-
AbstractCollection.remove(Object),AbstractCollection.contains(Object)
-
retainAll
public boolean retainAll(Collection<?> c)
始终返回false。 ASynchronousQueue没有内部容量。- Specified by:
-
retainAll在接口Collection<E> - 重写:
-
retainAll中的AbstractCollection<E> - 参数
-
c- 收藏 - 结果
-
false - 另请参见:
-
AbstractCollection.remove(Object),AbstractCollection.contains(Object)
-
peek
public E peek()
始终返回null。 ASynchronousQueue除非积极地等待,否则不返回元素。
-
iterator
public Iterator<E> iterator()
返回一个空的迭代器,其中hasNext总是返回false。- Specified by:
-
iterator在接口Collection<E> - Specified by:
-
iterator在接口Iterable<E> - Specified by:
-
iterator在AbstractCollection<E> - 结果
- 一个空的迭代器
-
spliterator
public Spliterator<E> spliterator()
返回一个空的分配器,其中对Spliterator.trySplit()的调用总是返回null。- Specified by:
-
spliterator在接口Collection<E> - Specified by:
-
spliterator在接口Iterable<E> - 结果
- 一个空的拼写器
- 从以下版本开始:
- 1.8
-
toArray
public Object[] toArray()
返回零长度数组。- Specified by:
-
toArray在接口Collection<E> - 重写:
-
toArray在AbstractCollection<E> - 结果
- 一个零长度的数组
-
toArray
public <T> T[] toArray(T[] a)
将指定数组的第零个元素设置为null(如果数组具有非零长度)并返回。- Specified by:
-
toArray在接口Collection<E> - 重写:
-
toArray在AbstractCollection<E> - 参数类型
-
T- 包含集合的数组的运行时类型 - 参数
-
a- 数组 - 结果
- 指定的数组
- 异常
-
NullPointerException- 如果指定的数组为空
-
toString
public String toString()
始终返回"[]"。- 重写:
-
toString在AbstractCollection<E> - 结果
-
"[]"
-
drainTo
public int drainTo(Collection<? super E> c)
描述从接口BlockingQueue复制从该队列中删除所有可用的元素,并将它们添加到给定的集合中。 此操作可能比重复轮询此队列更有效。 尝试向集合c添加元素时遇到的失败可能会导致元素在抛出关联的异常时既不在两个集合中,也可能不是两个集合。 尝试将队列排入自身会导致IllegalArgumentException。 此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。- Specified by:
-
drainTo在接口BlockingQueue<E> - 参数
-
c- 将元素传输到的集合 - 结果
- 转移的元素数量
- 异常
-
UnsupportedOperationException- 如果指定的集合不支持添加元素 -
ClassCastException- 如果此队列的元素的类阻止将其添加到指定的集合 -
NullPointerException- 如果指定的集合为空 -
IllegalArgumentException- 如果指定的集合是此队列,或该队列的某个元素的某些属性会阻止将其添加到指定的集合
-
drainTo
public int drainTo(Collection<? super E> c, int maxElements)
说明从接口BlockingQueue复制最多从该队列中删除给定数量的可用元素,并将它们添加到给定的集合中。 尝试向集合c添加元素时遇到的失败可能导致在抛出关联的异常时,元素既不在两个集合中,也可能不是两个集合。 尝试将队列排入自身会导致IllegalArgumentException。 此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。- Specified by:
-
drainTo在接口BlockingQueue<E> - 参数
-
c- 将元素传输到的集合 -
maxElements- 要传输的元素的最大数量 - 结果
- 转移的元素数量
- 异常
-
UnsupportedOperationException- 如果指定的集合不支持元素的添加 -
ClassCastException- 如果此队列的元素的类阻止将其添加到指定的集合 -
NullPointerException- 如果指定的集合为空 -
IllegalArgumentException- 如果指定的集合是此队列,或该队列的某个元素的某些属性阻止将其添加到指定的集合
-
-