1 |
dl |
1.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 |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package jsr166y; |
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* A {@link BlockingQueue} in which producers may wait for consumers |
12 |
jsr166 |
1.4 |
* to receive elements. A {@code TransferQueue} may be useful for |
13 |
dl |
1.1 |
* example in message passing applications in which producers |
14 |
jsr166 |
1.6 |
* sometimes (using method {@link #transfer}) await receipt of |
15 |
|
|
* elements by consumers invoking {@code take} or {@code poll}, while |
16 |
|
|
* at other times enqueue elements (via method {@code put}) without |
17 |
|
|
* waiting for receipt. {@linkplain |
18 |
|
|
* #tryTransfer(Object) Non-blocking} and {@linkplain |
19 |
|
|
* #tryTransfer(Object,long,TimeUnit) time-out} versions of {@code |
20 |
|
|
* tryTransfer} are also available. A {@code TransferQueue} may also |
21 |
|
|
* be queried, via {@link #hasWaitingConsumer}, whether there are any |
22 |
|
|
* threads waiting for items, which is a converse analogy to a {@code |
23 |
|
|
* peek} operation. |
24 |
dl |
1.1 |
* |
25 |
jsr166 |
1.6 |
* <p>Like other blocking queues, a {@code TransferQueue} may be |
26 |
jsr166 |
1.10 |
* capacity bounded. If so, an attempted transfer operation may |
27 |
|
|
* initially block waiting for available space, and/or subsequently |
28 |
|
|
* block waiting for reception by a consumer. Note that in a queue |
29 |
|
|
* with zero capacity, such as {@link SynchronousQueue}, {@code put} |
30 |
|
|
* and {@code transfer} are effectively synonymous. |
31 |
dl |
1.1 |
* |
32 |
|
|
* <p>This interface is a member of the |
33 |
|
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
34 |
|
|
* Java Collections Framework</a>. |
35 |
|
|
* |
36 |
|
|
* @since 1.7 |
37 |
|
|
* @author Doug Lea |
38 |
|
|
* @param <E> the type of elements held in this collection |
39 |
|
|
*/ |
40 |
|
|
public interface TransferQueue<E> extends BlockingQueue<E> { |
41 |
|
|
/** |
42 |
jsr166 |
1.9 |
* Transfers the element to a waiting consumer immediately, if possible. |
43 |
|
|
* |
44 |
|
|
* <p>More precisely, transfers the specified element immediately |
45 |
|
|
* if there exists a consumer already waiting to receive it (in |
46 |
|
|
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
47 |
|
|
* otherwise returning {@code false} without enqueuing the element. |
48 |
dl |
1.1 |
* |
49 |
|
|
* @param e the element to transfer |
50 |
jsr166 |
1.4 |
* @return {@code true} if the element was transferred, else |
51 |
|
|
* {@code false} |
52 |
dl |
1.1 |
* @throws ClassCastException if the class of the specified element |
53 |
|
|
* prevents it from being added to this queue |
54 |
|
|
* @throws NullPointerException if the specified element is null |
55 |
|
|
* @throws IllegalArgumentException if some property of the specified |
56 |
|
|
* element prevents it from being added to this queue |
57 |
|
|
*/ |
58 |
|
|
boolean tryTransfer(E e); |
59 |
|
|
|
60 |
|
|
/** |
61 |
jsr166 |
1.9 |
* Transfers the element to a consumer, waiting if necessary to do so. |
62 |
|
|
* |
63 |
|
|
* <p>More precisely, transfers the specified element immediately |
64 |
|
|
* if there exists a consumer already waiting to receive it (in |
65 |
|
|
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
66 |
|
|
* else waits until the element is received by a consumer. |
67 |
dl |
1.1 |
* |
68 |
|
|
* @param e the element to transfer |
69 |
|
|
* @throws InterruptedException if interrupted while waiting, |
70 |
jsr166 |
1.9 |
* in which case the element is not left enqueued |
71 |
dl |
1.1 |
* @throws ClassCastException if the class of the specified element |
72 |
|
|
* prevents it from being added to this queue |
73 |
|
|
* @throws NullPointerException if the specified element is null |
74 |
|
|
* @throws IllegalArgumentException if some property of the specified |
75 |
|
|
* element prevents it from being added to this queue |
76 |
|
|
*/ |
77 |
|
|
void transfer(E e) throws InterruptedException; |
78 |
|
|
|
79 |
|
|
/** |
80 |
jsr166 |
1.9 |
* Transfers the element to a consumer if it is possible to do so |
81 |
|
|
* before the timeout elapses. |
82 |
|
|
* |
83 |
|
|
* <p>More precisely, transfers the specified element immediately |
84 |
|
|
* if there exists a consumer already waiting to receive it (in |
85 |
|
|
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
86 |
|
|
* else waits until the element is received by a consumer, |
87 |
|
|
* returning {@code false} if the specified wait time elapses |
88 |
|
|
* before the element can be transferred. |
89 |
dl |
1.1 |
* |
90 |
|
|
* @param e the element to transfer |
91 |
|
|
* @param timeout how long to wait before giving up, in units of |
92 |
jsr166 |
1.4 |
* {@code unit} |
93 |
|
|
* @param unit a {@code TimeUnit} determining how to interpret the |
94 |
|
|
* {@code timeout} parameter |
95 |
|
|
* @return {@code true} if successful, or {@code false} if |
96 |
dl |
1.1 |
* the specified waiting time elapses before completion, |
97 |
jsr166 |
1.9 |
* in which case the element is not left enqueued |
98 |
dl |
1.1 |
* @throws InterruptedException if interrupted while waiting, |
99 |
jsr166 |
1.9 |
* in which case the element is not left enqueued |
100 |
dl |
1.1 |
* @throws ClassCastException if the class of the specified element |
101 |
|
|
* prevents it from being added to this queue |
102 |
|
|
* @throws NullPointerException if the specified element is null |
103 |
|
|
* @throws IllegalArgumentException if some property of the specified |
104 |
|
|
* element prevents it from being added to this queue |
105 |
|
|
*/ |
106 |
jsr166 |
1.3 |
boolean tryTransfer(E e, long timeout, TimeUnit unit) |
107 |
dl |
1.1 |
throws InterruptedException; |
108 |
|
|
|
109 |
|
|
/** |
110 |
jsr166 |
1.4 |
* Returns {@code true} if there is at least one consumer waiting |
111 |
jsr166 |
1.7 |
* to receive an element via {@link #take} or |
112 |
jsr166 |
1.8 |
* timed {@link #poll(long,TimeUnit) poll}. |
113 |
jsr166 |
1.4 |
* The return value represents a momentary state of affairs. |
114 |
|
|
* |
115 |
|
|
* @return {@code true} if there is at least one waiting consumer |
116 |
dl |
1.1 |
*/ |
117 |
|
|
boolean hasWaitingConsumer(); |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Returns an estimate of the number of consumers waiting to |
121 |
jsr166 |
1.8 |
* receive elements via {@link #take} or timed |
122 |
|
|
* {@link #poll(long,TimeUnit) poll}. The return value is an |
123 |
|
|
* approximation of a momentary state of affairs, that may be |
124 |
|
|
* inaccurate if consumers have completed or given up waiting. |
125 |
|
|
* The value may be useful for monitoring and heuristics, but |
126 |
|
|
* not for synchronization control. Implementations of this |
127 |
dl |
1.2 |
* method are likely to be noticeably slower than those for |
128 |
jsr166 |
1.4 |
* {@link #hasWaitingConsumer}. |
129 |
|
|
* |
130 |
jsr166 |
1.7 |
* @return the number of consumers waiting to receive elements |
131 |
dl |
1.1 |
*/ |
132 |
|
|
int getWaitingConsumerCount(); |
133 |
|
|
} |