1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
+ |
|
9 |
|
import java.util.concurrent.*; |
10 |
|
|
11 |
|
/** |
15 |
|
* sometimes (using method {@link #transfer}) await receipt of |
16 |
|
* elements by consumers invoking {@code take} or {@code poll}, while |
17 |
|
* at other times enqueue elements (via method {@code put}) without |
18 |
< |
* waiting for receipt. {@linkplain |
19 |
< |
* #tryTransfer(Object) Non-blocking} and {@linkplain |
20 |
< |
* #tryTransfer(Object,long,TimeUnit) time-out} versions of {@code |
21 |
< |
* tryTransfer} are also available. A {@code TransferQueue} may also |
22 |
< |
* be queried, via {@link #hasWaitingConsumer}, whether there are any |
23 |
< |
* threads waiting for items, which is a converse analogy to a {@code |
24 |
< |
* peek} operation. |
18 |
> |
* waiting for receipt. |
19 |
> |
* {@linkplain #tryTransfer(Object) Non-blocking} and |
20 |
> |
* {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of |
21 |
> |
* {@code tryTransfer} are also available. |
22 |
> |
* A {@code TransferQueue} may also be queried, via {@link |
23 |
> |
* #hasWaitingConsumer}, whether there are any threads waiting for |
24 |
> |
* items, which is a converse analogy to a {@code peek} operation. |
25 |
|
* |
26 |
|
* <p>Like other blocking queues, a {@code TransferQueue} may be |
27 |
< |
* capacity bounded. If so, an attempted {@code transfer} operation |
28 |
< |
* may initially block waiting for available space, and/or |
29 |
< |
* subsequently block waiting for reception by a consumer. Note that |
30 |
< |
* in a queue with zero capacity, such as {@link SynchronousQueue}, |
31 |
< |
* {@code put} and {@code transfer} are effectively synonymous. |
27 |
> |
* capacity bounded. If so, an attempted transfer operation may |
28 |
> |
* initially block waiting for available space, and/or subsequently |
29 |
> |
* block waiting for reception by a consumer. Note that in a queue |
30 |
> |
* with zero capacity, such as {@link SynchronousQueue}, {@code put} |
31 |
> |
* and {@code transfer} are effectively synonymous. |
32 |
|
* |
33 |
|
* <p>This interface is a member of the |
34 |
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
40 |
|
*/ |
41 |
|
public interface TransferQueue<E> extends BlockingQueue<E> { |
42 |
|
/** |
43 |
< |
* Transfers the specified element immediately if there exists a |
44 |
< |
* consumer already waiting to receive it (in {@link #take} or |
45 |
< |
* timed {@link #poll(long,TimeUnit) poll}), otherwise returning |
46 |
< |
* {@code false} without enqueuing the element. |
43 |
> |
* Transfers the element to a waiting consumer immediately, if possible. |
44 |
> |
* |
45 |
> |
* <p>More precisely, transfers the specified element immediately |
46 |
> |
* if there exists a consumer already waiting to receive it (in |
47 |
> |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
48 |
> |
* otherwise returning {@code false} without enqueuing the element. |
49 |
|
* |
50 |
|
* @param e the element to transfer |
51 |
|
* @return {@code true} if the element was transferred, else |
59 |
|
boolean tryTransfer(E e); |
60 |
|
|
61 |
|
/** |
62 |
< |
* Inserts the specified element into this queue, waiting if |
63 |
< |
* necessary for space to become available and the element to be |
64 |
< |
* received by a consumer invoking {@code take} or {@code poll}. |
62 |
> |
* Transfers the element to a consumer, waiting if necessary to do so. |
63 |
> |
* |
64 |
> |
* <p>More precisely, transfers the specified element immediately |
65 |
> |
* if there exists a consumer already waiting to receive it (in |
66 |
> |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
67 |
> |
* else waits until the element is received by a consumer. |
68 |
|
* |
69 |
|
* @param e the element to transfer |
70 |
|
* @throws InterruptedException if interrupted while waiting, |
71 |
< |
* in which case the element is not enqueued |
71 |
> |
* in which case the element is not left enqueued |
72 |
|
* @throws ClassCastException if the class of the specified element |
73 |
|
* prevents it from being added to this queue |
74 |
|
* @throws NullPointerException if the specified element is null |
78 |
|
void transfer(E e) throws InterruptedException; |
79 |
|
|
80 |
|
/** |
81 |
< |
* Inserts the specified element into this queue, waiting up to |
82 |
< |
* the specified wait time if necessary for space to become |
83 |
< |
* available and the element to be received by a consumer invoking |
84 |
< |
* {@code take} or {@code poll}. |
81 |
> |
* Transfers the element to a consumer if it is possible to do so |
82 |
> |
* before the timeout elapses. |
83 |
> |
* |
84 |
> |
* <p>More precisely, transfers the specified element immediately |
85 |
> |
* if there exists a consumer already waiting to receive it (in |
86 |
> |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
87 |
> |
* else waits until the element is received by a consumer, |
88 |
> |
* returning {@code false} if the specified wait time elapses |
89 |
> |
* before the element can be transferred. |
90 |
|
* |
91 |
|
* @param e the element to transfer |
92 |
|
* @param timeout how long to wait before giving up, in units of |
95 |
|
* {@code timeout} parameter |
96 |
|
* @return {@code true} if successful, or {@code false} if |
97 |
|
* the specified waiting time elapses before completion, |
98 |
< |
* in which case the element is not enqueued |
98 |
> |
* in which case the element is not left enqueued |
99 |
|
* @throws InterruptedException if interrupted while waiting, |
100 |
< |
* in which case the element is not enqueued |
100 |
> |
* in which case the element is not left enqueued |
101 |
|
* @throws ClassCastException if the class of the specified element |
102 |
|
* prevents it from being added to this queue |
103 |
|
* @throws NullPointerException if the specified element is null |