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