A 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.
百度翻译结果:一个阻塞队列,其中每个插入操作必须等待另一个线程相应的删除操作,反之亦然。同步队列没有任何内部容量,甚至没有1的容量。您不能偷看同步队列,因为元素只有在您试图删除它时才会出现;你不能插入一个元素(使用任何方法),除非另一个线程试图删除它;你不能迭代,因为没有什么可迭代的。队列的头是第一个排队的插入线程试图添加到队列中的元素;如果没有这样的队列线程,则没有可删除的元素,poll()将返回null。对于其他Collection方法(例如contains), SynchronousQueue充当空集合。这个队列不允许空元素。
同步队列类似于CSP和Ada中使用的汇合通道。它们非常适合于切换设计,在这种设计中,在一个线程中运行的对象必须与在另一个线程中运行的对象同步,以便向它传递一些信息、事件或任务。
这个类支持一个可选的公平策略来排序等待的生产者和消费者线程。默认情况下,不保证这种顺序。然而,将公平性设置为true的队列以FIFO顺序授予线程访问权。
SynchronousQueue
最新推荐文章于 2024-10-11 08:02:24 发布