public interface TransferQueue
implements BlockingQueue<E>
| java.util.concurrent.TransferQueue<E> |
| |
生产者可以等待消费者接收元素的BlockingQueue 。 例如,在消息传递应用中, TransferQueue可能是有用的,其中生产者有时(使用方法transfer(E) )等待消费者接收调用take或poll的元素,而在其他时间将put元素(通过方法put )排队而不等待接收。 Non-blocking和time-out版本的tryTransfer也可用。 TransferQueue也可以通过hasWaitingConsumer()查询是否有任何线程在等待项目,这与peek操作是类推的。
像其他阻塞队列一样, TransferQueue可能受限于容量。 如果是,则尝试传送操作可能最初阻止等待可用空间,和/或随后阻止等待消费者的接收。 请注意,在与零容量的队列,例如SynchronousQueue , put和transfer是有效的代名词。
公共方法(Public methods) |
|
|---|---|
abstract int |
getWaitingConsumerCount() |
abstract boolean |
hasWaitingConsumer() |
abstract void |
transfer(E e) 将元素转移给消费者,如果需要的话等待。 |
abstract boolean |
tryTransfer(E e) 如果可能,立即将元素转移给等待的消费者。 |
abstract boolean |
tryTransfer(E e, long timeout, TimeUnit unit) 如果在超时时间之前可以这样做,则将该元素传输给消费者。 |
继承方法(Inherited methods) |
|
|---|---|
java.util.concurrent.BlockingQueue
|
|
java.util.Queue
|
|
java.util.Collection
|
|
java.lang.Iterable
|
|
int getWaitingConsumerCount ()
通过take()或定时poll返回等待接收元素的消费者数量的估计值。 返回值近似于一种事态,如果消费者已经完成或放弃等待,这可能是不准确的。 该值可能对监视和启发式有用,但不适用于同步控制。 这种方法的实现可能明显比hasWaitingConsumer()慢。
| 返回(Returns) | |
|---|---|
int |
the number of consumers waiting to receive elements |
boolean hasWaitingConsumer ()
返回true ,如果有至少一个消费者等待经由以接收元件take()或定时poll 。 返回值代表了一种瞬间的事态。
| 返回(Returns) | |
|---|---|
boolean |
true if there is at least one waiting consumer |
void transfer (E e)
将元素转移给消费者,如果需要的话等待。
更确切地说,如果存在消费者已经等待接收它(在 take()或定时 poll ),则立即传送指定的元素,否则等待直到消费者接收到该元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to transfer |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
if interrupted while waiting, in which case the element is not left enqueued |
ClassCastException |
if the class of the specified element prevents it from being added to this queue |
NullPointerException |
if the specified element is null |
IllegalArgumentException |
if some property of the specified element prevents it from being added to this queue |
boolean tryTransfer (E e)
如果可能,立即将元素转移给等待的消费者。
更确切地说,如果存在消费者已经等待接收它(在 take()或定时 poll ),则立即传送指定的元素,否则返回 false而不 false元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to transfer |
| 返回(Returns) | |
|---|---|
boolean |
true if the element was transferred, else false |
| 抛出异常(Throws) | |
|---|---|
ClassCastException |
if the class of the specified element prevents it from being added to this queue |
NullPointerException |
if the specified element is null |
IllegalArgumentException |
if some property of the specified element prevents it from being added to this queue |
boolean tryTransfer (E e,
long timeout,
TimeUnit unit)
如果在超时时间之前可以这样做,则将该元素传输给消费者。
更确切地说,如果消费者已经等待接收它( take()或定时 poll ),则立即传输指定的元素,否则等到消费者接收到该元素为止,如果在该元素可能之前经过了指定的等待时间,则返回 false转入。
| 参数(Parameters) | |
|---|---|
e |
E: the element to transfer |
timeout |
long: how long to wait before giving up, in units of unit |
unit |
TimeUnit: a TimeUnit determining how to interpret the timeout parameter |
| 返回(Returns) | |
|---|---|
boolean |
true if successful, or false if the specified waiting time elapses before completion, in which case the element is not left enqueued |
| 抛出异常(Throws) | |
|---|---|
InterruptedException |
if interrupted while waiting, in which case the element is not left enqueued |
ClassCastException |
if the class of the specified element prevents it from being added to this queue |
NullPointerException |
if the specified element is null |
IllegalArgumentException |
if some property of the specified element prevents it from being added to this queue |