Most visited

Recently visited

Added in API level 21

TransferQueue

public interface TransferQueue
implements BlockingQueue<E>

java.util.concurrent.TransferQueue<E>
Known Indirect Subclasses


生产者可以等待消费者接收元素的BlockingQueue 例如,在消息传递应用中, TransferQueue可能是有用的,其中生产者有时(使用方法transfer(E) )等待消费者接收调用takepoll的元素,而在其他时间将put元素(通过方法put )排队而不等待接收。 Non-blockingtime-out版本的tryTransfer也可用。 TransferQueue也可以通过hasWaitingConsumer()查询是否有任何线程在等待项目,这与peek操作是类推的。

像其他阻塞队列一样, TransferQueue可能受限于容量。 如果是,则尝试传送操作可能最初阻止等待可用空间,和/或随后阻止等待消费者的接收。 请注意,在与零容量的队列,例如SynchronousQueueputtransfer是有效的代名词。

Summary

Public methods

abstract int getWaitingConsumerCount()

通过 take()或定时 poll返回等待接收元素的消费者数量的估计值。

abstract boolean hasWaitingConsumer()

返回 true ,如果有至少一个消费者等待经由以接收元件 take()或定时 poll

abstract void transfer(E e)

将元素转移给消费者,如果需要的话等待。

abstract boolean tryTransfer(E e)

如果可能,立即将元素转移给等待的消费者。

abstract boolean tryTransfer(E e, long timeout, TimeUnit unit)

如果在超时时间之前可以这样做,则将该元素传输给消费者。

Inherited methods

From interface java.util.concurrent.BlockingQueue
From interface java.util.Queue
From interface java.util.Collection
From interface java.lang.Iterable

Public methods

getWaitingConsumerCount

Added in API level 21
int getWaitingConsumerCount ()

通过take()或定时poll返回等待接收元素的消费者数量的估计值。 返回值近似于一种事态,如果消费者已经完成或放弃等待,这可能是不准确的。 该值可能对监视和启发式有用,但不适用于同步控制。 这种方法的实现可能明显比hasWaitingConsumer()慢。

Returns
int the number of consumers waiting to receive elements

hasWaitingConsumer

Added in API level 21
boolean hasWaitingConsumer ()

返回true ,如果有至少一个消费者等待经由以接收元件take()或定时poll 返回值代表了一种瞬间的事态。

Returns
boolean true if there is at least one waiting consumer

transfer

Added in API level 21
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

tryTransfer

Added in API level 21
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

tryTransfer

Added in API level 21
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

Hooray!