1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.26 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
tim |
1.9 |
|
9 |
|
|
import java.util.Collection; |
10 |
tim |
1.1 |
import java.util.Queue; |
11 |
|
|
|
12 |
|
|
/** |
13 |
dl |
1.4 |
* A {@link java.util.Queue} that additionally supports operations |
14 |
dl |
1.27 |
* that wait for the queue to become non-empty when retrieving an element, |
15 |
|
|
* and wait for space to become available in the queue when storing an |
16 |
dl |
1.31 |
* element. The names |
17 |
|
|
* of the operations vary with their policies: |
18 |
|
|
* |
19 |
|
|
*<table BORDER CELLPADDING=3 CELLSPACING=1> |
20 |
|
|
* <tr> |
21 |
|
|
* <td><em>Operation</em></td> |
22 |
|
|
* <td ALIGN=CENTER><em>Attempt</em></td> |
23 |
|
|
* <td ALIGN=CENTER><em>Throw</em></td> |
24 |
|
|
* <td ALIGN=CENTER><em>Block</em></td> |
25 |
|
|
* <td ALIGN=CENTER><em>Timeout</em></td> |
26 |
|
|
* </tr> |
27 |
|
|
* <tr> |
28 |
|
|
* <td><em>insert</em></td> |
29 |
|
|
* <td>offer(x)</td> |
30 |
|
|
* <td>add(x)</td> |
31 |
|
|
* <td>put(x)</td> |
32 |
|
|
* <td>offer(x,time,unit)</td> |
33 |
|
|
* </tr> |
34 |
|
|
* <tr> |
35 |
|
|
* <td><em>extract</em></td> |
36 |
|
|
* <td>poll()</td> |
37 |
|
|
* <td>remove()</td> |
38 |
|
|
* <td>take()</td> |
39 |
|
|
* <td>poll(time,unit)</td> |
40 |
|
|
* </tr> |
41 |
|
|
* <tr> |
42 |
|
|
* <td><em>inspect</em></td> |
43 |
|
|
* <td>peek()</td> |
44 |
|
|
* <td>element()</td> |
45 |
|
|
* <td></td> |
46 |
|
|
* <td></td> |
47 |
|
|
* </tr> |
48 |
|
|
*</table> |
49 |
dl |
1.2 |
* |
50 |
dholmes |
1.7 |
* <p>A <tt>BlockingQueue</tt> does not accept <tt>null</tt> elements. |
51 |
dholmes |
1.10 |
* Implementations throw <tt>NullPointerException</tt> on attempts |
52 |
dl |
1.2 |
* to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A |
53 |
|
|
* <tt>null</tt> is used as a sentinel value to indicate failure of |
54 |
|
|
* <tt>poll</tt> operations. |
55 |
|
|
* |
56 |
dholmes |
1.7 |
* <p>A <tt>BlockingQueue</tt> may be capacity bounded. At any given |
57 |
|
|
* time it may have a <tt>remainingCapacity</tt> beyond which no |
58 |
dl |
1.2 |
* additional elements can be <tt>put</tt> without blocking. |
59 |
dholmes |
1.7 |
* A <tt>BlockingQueue</tt> without any intrinsic capacity constraints always |
60 |
|
|
* reports a remaining capacity of <tt>Integer.MAX_VALUE</tt>. |
61 |
dl |
1.2 |
* |
62 |
dl |
1.29 |
* <p> <tt>BlockingQueue</tt> implementations are designed to be used |
63 |
|
|
* primarily for producer-consumer queues, but additionally support |
64 |
|
|
* the {@link java.util.Collection} interface. So, for example, it is |
65 |
|
|
* possible to remove an arbitrary element from a queue using |
66 |
dl |
1.6 |
* <tt>remove(x)</tt>. However, such operations are in general |
67 |
dl |
1.18 |
* <em>not</em> performed very efficiently, and are intended for only |
68 |
dl |
1.29 |
* occasional use, such as when a queued message is cancelled. |
69 |
|
|
* |
70 |
|
|
* <p> <tt>BlockingQueue</tt> implementations are thread-safe. All |
71 |
|
|
* queuing methods achieve their effects atomically using internal |
72 |
|
|
* locks or other forms of concurrency control. However, the |
73 |
|
|
* <em>bulk</em> Collection operations <tt>addAll</tt>, |
74 |
|
|
* <tt>containsAll</tt>, <tt>retainAll</tt> and <tt>removeAll</tt> are |
75 |
|
|
* <em>not</em> necessarily performed atomically unless specified |
76 |
|
|
* otherwise in an implementation. So it is possible, for example, for |
77 |
|
|
* <tt>addAll(c)</tt> to fail (throwing an exception) after adding |
78 |
dl |
1.18 |
* only some of the elements in <tt>c</tt>. |
79 |
dl |
1.2 |
* |
80 |
dholmes |
1.7 |
* <p>A <tt>BlockingQueue</tt> does <em>not</em> intrinsically support |
81 |
dl |
1.2 |
* any kind of "close" or "shutdown" operation to |
82 |
|
|
* indicate that no more items will be added. The needs and usage of |
83 |
brian |
1.5 |
* such features tend to be implementation-dependent. For example, a |
84 |
dl |
1.2 |
* common tactic is for producers to insert special |
85 |
|
|
* <em>end-of-stream</em> or <em>poison</em> objects, that are |
86 |
|
|
* interpreted accordingly when taken by consumers. |
87 |
tim |
1.1 |
* |
88 |
|
|
* <p> |
89 |
brian |
1.5 |
* Usage example, based on a typical producer-consumer scenario. |
90 |
tim |
1.9 |
* Note that a <tt>BlockingQueue</tt> can safely be used with multiple |
91 |
dholmes |
1.7 |
* producers and multiple consumers. |
92 |
tim |
1.1 |
* <pre> |
93 |
|
|
* class Producer implements Runnable { |
94 |
|
|
* private final BlockingQueue queue; |
95 |
|
|
* Producer(BlockingQueue q) { queue = q; } |
96 |
|
|
* public void run() { |
97 |
|
|
* try { |
98 |
|
|
* while(true) { queue.put(produce()); } |
99 |
tim |
1.15 |
* } catch (InterruptedException ex) { ... handle ...} |
100 |
tim |
1.1 |
* } |
101 |
|
|
* Object produce() { ... } |
102 |
|
|
* } |
103 |
|
|
* |
104 |
|
|
* class Consumer implements Runnable { |
105 |
|
|
* private final BlockingQueue queue; |
106 |
dl |
1.21 |
* Consumer(BlockingQueue q) { queue = q; } |
107 |
tim |
1.1 |
* public void run() { |
108 |
|
|
* try { |
109 |
|
|
* while(true) { consume(queue.take()); } |
110 |
tim |
1.15 |
* } catch (InterruptedException ex) { ... handle ...} |
111 |
tim |
1.1 |
* } |
112 |
|
|
* void consume(Object x) { ... } |
113 |
|
|
* } |
114 |
|
|
* |
115 |
|
|
* class Setup { |
116 |
|
|
* void main() { |
117 |
|
|
* BlockingQueue q = new SomeQueueImplementation(); |
118 |
|
|
* Producer p = new Producer(q); |
119 |
brian |
1.5 |
* Consumer c1 = new Consumer(q); |
120 |
|
|
* Consumer c2 = new Consumer(q); |
121 |
tim |
1.1 |
* new Thread(p).start(); |
122 |
brian |
1.5 |
* new Thread(c1).start(); |
123 |
|
|
* new Thread(c2).start(); |
124 |
tim |
1.1 |
* } |
125 |
|
|
* } |
126 |
|
|
* </pre> |
127 |
|
|
* |
128 |
dl |
1.28 |
* <p>This interface is a member of the |
129 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
130 |
|
|
* Java Collections Framework</a>. |
131 |
|
|
* |
132 |
tim |
1.1 |
* @since 1.5 |
133 |
dl |
1.6 |
* @author Doug Lea |
134 |
dl |
1.24 |
* @param <E> the type of elements held in this collection |
135 |
tim |
1.1 |
*/ |
136 |
|
|
public interface BlockingQueue<E> extends Queue<E> { |
137 |
dholmes |
1.7 |
|
138 |
|
|
/** |
139 |
dl |
1.20 |
* Inserts the specified element into this queue, if possible. When |
140 |
dl |
1.18 |
* using queues that may impose insertion restrictions (for |
141 |
|
|
* example capacity bounds), method <tt>offer</tt> is generally |
142 |
|
|
* preferable to method {@link Collection#add}, which can fail to |
143 |
dl |
1.19 |
* insert an element only by throwing an exception. |
144 |
dl |
1.18 |
* |
145 |
dl |
1.19 |
* @param o the element to add. |
146 |
dl |
1.18 |
* @return <tt>true</tt> if it was possible to add the element to |
147 |
dl |
1.19 |
* this queue, else <tt>false</tt> |
148 |
|
|
* @throws NullPointerException if the specified element is <tt>null</tt> |
149 |
dholmes |
1.7 |
*/ |
150 |
tim |
1.14 |
boolean offer(E o); |
151 |
dl |
1.20 |
|
152 |
tim |
1.1 |
/** |
153 |
dl |
1.20 |
* Inserts the specified element into this queue, waiting if necessary |
154 |
dl |
1.18 |
* up to the specified wait time for space to become available. |
155 |
dholmes |
1.10 |
* @param o the element to add |
156 |
dholmes |
1.7 |
* @param timeout how long to wait before giving up, in units of |
157 |
|
|
* <tt>unit</tt> |
158 |
tim |
1.9 |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
159 |
dholmes |
1.7 |
* <tt>timeout</tt> parameter |
160 |
|
|
* @return <tt>true</tt> if successful, or <tt>false</tt> if |
161 |
|
|
* the specified waiting time elapses before space is available. |
162 |
tim |
1.1 |
* @throws InterruptedException if interrupted while waiting. |
163 |
dholmes |
1.11 |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
164 |
tim |
1.1 |
*/ |
165 |
dholmes |
1.10 |
boolean offer(E o, long timeout, TimeUnit unit) |
166 |
dholmes |
1.7 |
throws InterruptedException; |
167 |
tim |
1.1 |
|
168 |
|
|
/** |
169 |
dholmes |
1.12 |
* Retrieves and removes the head of this queue, waiting |
170 |
dholmes |
1.7 |
* if necessary up to the specified wait time if no elements are |
171 |
|
|
* present on this queue. |
172 |
dl |
1.6 |
* @param timeout how long to wait before giving up, in units of |
173 |
|
|
* <tt>unit</tt> |
174 |
tim |
1.9 |
* @param unit a <tt>TimeUnit</tt> determining how to interpret the |
175 |
dholmes |
1.7 |
* <tt>timeout</tt> parameter |
176 |
tim |
1.9 |
* @return the head of this queue, or <tt>null</tt> if the |
177 |
dholmes |
1.7 |
* specified waiting time elapses before an element is present. |
178 |
tim |
1.1 |
* @throws InterruptedException if interrupted while waiting. |
179 |
|
|
*/ |
180 |
tim |
1.9 |
E poll(long timeout, TimeUnit unit) |
181 |
tim |
1.1 |
throws InterruptedException; |
182 |
|
|
|
183 |
|
|
/** |
184 |
dholmes |
1.12 |
* Retrieves and removes the head of this queue, waiting |
185 |
dholmes |
1.7 |
* if no elements are present on this queue. |
186 |
|
|
* @return the head of this queue |
187 |
tim |
1.1 |
* @throws InterruptedException if interrupted while waiting. |
188 |
|
|
*/ |
189 |
dholmes |
1.7 |
E take() throws InterruptedException; |
190 |
|
|
|
191 |
tim |
1.1 |
/** |
192 |
dholmes |
1.12 |
* Adds the specified element to this queue, waiting if necessary for |
193 |
dholmes |
1.7 |
* space to become available. |
194 |
dholmes |
1.10 |
* @param o the element to add |
195 |
tim |
1.1 |
* @throws InterruptedException if interrupted while waiting. |
196 |
dholmes |
1.13 |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
197 |
tim |
1.1 |
*/ |
198 |
dholmes |
1.10 |
void put(E o) throws InterruptedException; |
199 |
dholmes |
1.7 |
|
200 |
dl |
1.2 |
/** |
201 |
dholmes |
1.12 |
* Returns the number of elements that this queue can ideally (in |
202 |
dl |
1.2 |
* the absence of memory or resource constraints) accept without |
203 |
|
|
* blocking, or <tt>Integer.MAX_VALUE</tt> if there is no |
204 |
tim |
1.9 |
* intrinsic limit. |
205 |
dholmes |
1.7 |
* <p>Note that you <em>cannot</em> always tell if |
206 |
dl |
1.2 |
* an attempt to <tt>add</tt> an element will succeed by |
207 |
|
|
* inspecting <tt>remainingCapacity</tt> because it may be the |
208 |
dl |
1.30 |
* case that another thread is about to <tt>put</tt> or <tt>take</tt> an |
209 |
|
|
* element. |
210 |
dl |
1.2 |
* @return the remaining capacity |
211 |
dl |
1.6 |
*/ |
212 |
|
|
int remainingCapacity(); |
213 |
tim |
1.1 |
|
214 |
dl |
1.18 |
/** |
215 |
|
|
* Adds the specified element to this queue if it is possible to |
216 |
|
|
* do so immediately, returning <tt>true</tt> upon success, else |
217 |
|
|
* throwing an IllegalStateException. |
218 |
|
|
* @param o the element |
219 |
|
|
* @return <tt>true</tt> (as per the general contract of |
220 |
|
|
* <tt>Collection.add</tt>). |
221 |
|
|
* |
222 |
|
|
* @throws NullPointerException if the specified element is <tt>null</tt> |
223 |
|
|
* @throws IllegalStateException if element cannot be added |
224 |
|
|
*/ |
225 |
|
|
boolean add(E o); |
226 |
dholmes |
1.7 |
|
227 |
dl |
1.18 |
/** |
228 |
dl |
1.22 |
* Removes all available elements from this queue and adds them |
229 |
|
|
* into the given collection. This operation may be more |
230 |
|
|
* efficient than repeatedly polling this queue. A failure |
231 |
|
|
* encountered while attempting to <tt>add</tt> elements to |
232 |
|
|
* collection <tt>c</tt> may result in elements being in neither, |
233 |
|
|
* either or both collections when the associated exception is |
234 |
|
|
* thrown. Attempts to drain a queue to itself result in |
235 |
|
|
* <tt>IllegalArgumentException</tt>. Further, the behavior of |
236 |
|
|
* this operation is undefined if the specified collection is |
237 |
|
|
* modified while the operation is in progress. |
238 |
dl |
1.18 |
* |
239 |
dl |
1.22 |
* @param c the collection to transfer elements into |
240 |
|
|
* @return the number of elements transferred. |
241 |
|
|
* @throws NullPointerException if c is null |
242 |
|
|
* @throws IllegalArgumentException if c is this queue |
243 |
dl |
1.18 |
* |
244 |
|
|
*/ |
245 |
dl |
1.22 |
int drainTo(Collection<? super E> c); |
246 |
|
|
|
247 |
|
|
/** |
248 |
|
|
* Removes at most the given number of available elements from |
249 |
|
|
* this queue and adds them into the given collection. A failure |
250 |
|
|
* encountered while attempting to <tt>add</tt> elements to |
251 |
|
|
* collection <tt>c</tt> may result in elements being in neither, |
252 |
|
|
* either or both collections when the associated exception is |
253 |
|
|
* thrown. Attempts to drain a queue to itself result in |
254 |
|
|
* <tt>IllegalArgumentException</tt>. Further, the behavior of |
255 |
|
|
* this operation is undefined if the specified collection is |
256 |
|
|
* modified while the operation is in progress. |
257 |
|
|
* |
258 |
|
|
* @param c the collection to transfer elements into |
259 |
|
|
* @param maxElements the maximum number of elements to transfer |
260 |
|
|
* @return the number of elements transferred. |
261 |
|
|
* @throws NullPointerException if c is null |
262 |
|
|
* @throws IllegalArgumentException if c is this queue |
263 |
|
|
*/ |
264 |
|
|
int drainTo(Collection<? super E> c, int maxElements); |
265 |
dl |
1.18 |
} |