BlockingQueue---SynchronousQueue

发布时间 2023-10-31 11:04:53作者: anpeiyong

概述

  

  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 {@code 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 <em>head</em> 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 {@code poll()} will return {@code null}.
  For purposes of other {@code Collection} methods (for example {@code contains}), a {@code SynchronousQueue} acts as an empty collection.
  This queue does not permit {@code null} elements.

    

    一个 BlockingQueue,其中每个插入操作 都必须等待另一个线程执行相应的删除操作,反之亦然。
    同步队列没有任何内部容量,甚至没有 1 的容量。
    您不能peek同步队列,因为只有在您尝试删除元素时才存在该元素; 你不能插入一个元素,除非另一个线程试图删除它;你不能迭代,因为没有什么可迭代的。
    队列<em>头部</em>是第一个排队的插入线程尝试添加到队列中的元素;如果没有这样的排队线程,则没有元素可供删除,{@code poll()} 将返回 {@code null}。
    对于其他 {@code Collection} 方法(例如 {@code contains}),{@code SynchronousQueue} 充当空集合。
    此队列不允许 {@code null} 元素;

 

  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.

    

    同步队列类似于 CSP 和 Ada 中使用的集合通道。
    它们非常适合切换设计,在这种设计中,在一个线程中运行的对象必须与在另一个线程中运行的对象同步,以便将一些信息、事件或任务传递给它;

 

  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 {@code true} grants threads access in FIFO order.

    

    此类支持可选的公平性策略,用于对等待的生产者和使用者线程进行排序。
    默认情况下,不保证此排序。
    但是,在公平性设置为 {@code true} 的情况下构造的队列按 FIFO 顺序授予线程访问权限;