TransferQueue (Java Platform SE 7 ) (original) (raw)
- Type Parameters:
E
- the type of elements held in this collection
All Superinterfaces:
BlockingQueue, Collection, Iterable, Queue
All Known Implementing Classes:
LinkedTransferQueue
public interface TransferQueue
extends BlockingQueue
A BlockingQueue in which producers may wait for consumers to receive elements. A TransferQueue
may be useful for example in message passing applications in which producers sometimes (using method transfer(E)) await receipt of elements by consumers invoking take
or poll
, while at other times enqueue elements (via method put
) without waiting for receipt.Non-blocking andtime-out versions oftryTransfer
are also available. A TransferQueue
may also be queried, via hasWaitingConsumer(), whether there are any threads waiting for items, which is a converse analogy to a peek
operation.
Like other blocking queues, a TransferQueue
may be capacity bounded. If so, an attempted transfer operation may initially block waiting for available space, and/or subsequently block waiting for reception by a consumer. Note that in a queue with zero capacity, such as SynchronousQueue, put
and transfer
are effectively synonymous.
This interface is a member of the Java Collections Framework.
Since:
1.7
Method Summary
Methods
Modifier and Type Method and Description int getWaitingConsumerCount() Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timedpoll. boolean hasWaitingConsumer() Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take() or timed poll. void transfer(E e) Transfers the element to a consumer, waiting if necessary to do so. boolean tryTransfer(E e) Transfers the element to a waiting consumer immediately, if possible. boolean tryTransfer(E e, long timeout,TimeUnit unit) Transfers the element to a consumer if it is possible to do so before the timeout elapses. * ### Methods inherited from interface java.util.concurrent.[BlockingQueue](../../../java/util/concurrent/BlockingQueue.html "interface in java.util.concurrent") `[add](../../../java/util/concurrent/BlockingQueue.html#add%28E%29), [contains](../../../java/util/concurrent/BlockingQueue.html#contains%28java.lang.Object%29), [drainTo](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection%29), [drainTo](../../../java/util/concurrent/BlockingQueue.html#drainTo%28java.util.Collection,%20int%29), [offer](../../../java/util/concurrent/BlockingQueue.html#offer%28E%29), [offer](../../../java/util/concurrent/BlockingQueue.html#offer%28E,%20long,%20java.util.concurrent.TimeUnit%29), [poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29), [put](../../../java/util/concurrent/BlockingQueue.html#put%28E%29), [remainingCapacity](../../../java/util/concurrent/BlockingQueue.html#remainingCapacity%28%29), [remove](../../../java/util/concurrent/BlockingQueue.html#remove%28java.lang.Object%29), [take](../../../java/util/concurrent/BlockingQueue.html#take%28%29)` * ### Methods inherited from interface java.util.[Queue](../../../java/util/Queue.html "interface in java.util") `[element](../../../java/util/Queue.html#element%28%29), [peek](../../../java/util/Queue.html#peek%28%29), [poll](../../../java/util/Queue.html#poll%28%29), [remove](../../../java/util/Queue.html#remove%28%29)` * ### Methods inherited from interface java.util.[Collection](../../../java/util/Collection.html "interface in java.util") `[addAll](../../../java/util/Collection.html#addAll%28java.util.Collection%29), [clear](../../../java/util/Collection.html#clear%28%29), [containsAll](../../../java/util/Collection.html#containsAll%28java.util.Collection%29), [equals](../../../java/util/Collection.html#equals%28java.lang.Object%29), [hashCode](../../../java/util/Collection.html#hashCode%28%29), [isEmpty](../../../java/util/Collection.html#isEmpty%28%29), [iterator](../../../java/util/Collection.html#iterator%28%29), [removeAll](../../../java/util/Collection.html#removeAll%28java.util.Collection%29), [retainAll](../../../java/util/Collection.html#retainAll%28java.util.Collection%29), [size](../../../java/util/Collection.html#size%28%29), [toArray](../../../java/util/Collection.html#toArray%28%29), [toArray](../../../java/util/Collection.html#toArray%28T[]%29)`
Method Detail
* #### tryTransfer boolean tryTransfer([E](../../../java/util/concurrent/TransferQueue.html "type parameter in TransferQueue") e) Transfers the element to a waiting consumer immediately, if possible. More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in[BlockingQueue.take()](../../../java/util/concurrent/BlockingQueue.html#take%28%29) or timed [poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29)), otherwise returning `false` without enqueuing the element. Parameters: `e` \- the element to transfer Returns: `true` if the element was transferred, else`false` Throws: `[ClassCastException](../../../java/lang/ClassCastException.html "class in java.lang")` \- if the class of the specified element prevents it from being added to this queue `[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang")` \- if the specified element is null `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if some property of the specified element prevents it from being added to this queue * #### transfer void transfer([E](../../../java/util/concurrent/TransferQueue.html "type parameter in TransferQueue") e) throws [InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") Transfers the element to a consumer, waiting if necessary to do so. More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in[BlockingQueue.take()](../../../java/util/concurrent/BlockingQueue.html#take%28%29) or timed [poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29)), else waits until the element is received by a consumer. Parameters: `e` \- the element to transfer Throws: `[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang")` \- if interrupted while waiting, in which case the element is not left enqueued `[ClassCastException](../../../java/lang/ClassCastException.html "class in java.lang")` \- if the class of the specified element prevents it from being added to this queue `[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang")` \- if the specified element is null `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if some property of the specified element prevents it from being added to this queue * #### tryTransfer boolean tryTransfer([E](../../../java/util/concurrent/TransferQueue.html "type parameter in TransferQueue") e, long timeout, [TimeUnit](../../../java/util/concurrent/TimeUnit.html "enum in java.util.concurrent") unit) throws [InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang") Transfers the element to a consumer if it is possible to do so before the timeout elapses. More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in[BlockingQueue.take()](../../../java/util/concurrent/BlockingQueue.html#take%28%29) or timed [poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29)), else waits until the element is received by a consumer, returning `false` if the specified wait time elapses before the element can be transferred. Parameters: `e` \- the element to transfer `timeout` \- how long to wait before giving up, in units of`unit` `unit` \- a `TimeUnit` determining how to interpret the`timeout` parameter Returns: `true` if successful, or `false` if the specified waiting time elapses before completion, in which case the element is not left enqueued Throws: `[InterruptedException](../../../java/lang/InterruptedException.html "class in java.lang")` \- if interrupted while waiting, in which case the element is not left enqueued `[ClassCastException](../../../java/lang/ClassCastException.html "class in java.lang")` \- if the class of the specified element prevents it from being added to this queue `[NullPointerException](../../../java/lang/NullPointerException.html "class in java.lang")` \- if the specified element is null `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if some property of the specified element prevents it from being added to this queue * #### hasWaitingConsumer boolean hasWaitingConsumer() Returns `true` if there is at least one consumer waiting to receive an element via [BlockingQueue.take()](../../../java/util/concurrent/BlockingQueue.html#take%28%29) or timed [poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29). The return value represents a momentary state of affairs. Returns: `true` if there is at least one waiting consumer * #### getWaitingConsumerCount int getWaitingConsumerCount() Returns an estimate of the number of consumers waiting to receive elements via [BlockingQueue.take()](../../../java/util/concurrent/BlockingQueue.html#take%28%29) or timed[poll](../../../java/util/concurrent/BlockingQueue.html#poll%28long,%20java.util.concurrent.TimeUnit%29). The return value is an approximation of a momentary state of affairs, that may be inaccurate if consumers have completed or given up waiting. The value may be useful for monitoring and heuristics, but not for synchronization control. Implementations of this method are likely to be noticeably slower than those for[hasWaitingConsumer()](../../../java/util/concurrent/TransferQueue.html#hasWaitingConsumer%28%29). Returns: the number of consumers waiting to receive elements
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.