SynchronousQueue

本文介绍同步队列(SynchronousQueue),一种特殊的阻塞队列,其中每个插入操作必须等待另一个线程的相应删除操作。同步队列没有内部容量,主要用于线程间的同步交换,支持公平策略选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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顺序授予线程访问权。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值