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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.util.Collection; |
10 |
import java.util.Queue; |
11 |
|
12 |
/** |
13 |
* A {@link java.util.Queue} that additionally supports operations |
14 |
* that wait for the queue to become non-empty when retrieving an |
15 |
* element, and wait for space to become available in the queue when |
16 |
* storing an element. |
17 |
* |
18 |
* <p>{@code BlockingQueue} methods come in four forms, with different ways |
19 |
* of handling operations that cannot be satisfied immediately, but may be |
20 |
* satisfied at some point in the future: |
21 |
* one throws an exception, the second returns a special value (either |
22 |
* {@code null} or {@code false}, depending on the operation), the third |
23 |
* blocks the current thread indefinitely until the operation can succeed, |
24 |
* and the fourth blocks for only a given maximum time limit before giving |
25 |
* up. These methods are summarized in the following table: |
26 |
* |
27 |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
28 |
* <caption>Summary of BlockingQueue methods</caption> |
29 |
* <tr> |
30 |
* <td></td> |
31 |
* <td ALIGN=CENTER><em>Throws exception</em></td> |
32 |
* <td ALIGN=CENTER><em>Special value</em></td> |
33 |
* <td ALIGN=CENTER><em>Blocks</em></td> |
34 |
* <td ALIGN=CENTER><em>Times out</em></td> |
35 |
* </tr> |
36 |
* <tr> |
37 |
* <td><b>Insert</b></td> |
38 |
* <td>{@link #add add(e)}</td> |
39 |
* <td>{@link #offer offer(e)}</td> |
40 |
* <td>{@link #put put(e)}</td> |
41 |
* <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td> |
42 |
* </tr> |
43 |
* <tr> |
44 |
* <td><b>Remove</b></td> |
45 |
* <td>{@link #remove remove()}</td> |
46 |
* <td>{@link #poll poll()}</td> |
47 |
* <td>{@link #take take()}</td> |
48 |
* <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td> |
49 |
* </tr> |
50 |
* <tr> |
51 |
* <td><b>Examine</b></td> |
52 |
* <td>{@link #element element()}</td> |
53 |
* <td>{@link #peek peek()}</td> |
54 |
* <td><em>not applicable</em></td> |
55 |
* <td><em>not applicable</em></td> |
56 |
* </tr> |
57 |
* </table> |
58 |
* |
59 |
* <p>A {@code BlockingQueue} does not accept {@code null} elements. |
60 |
* Implementations throw {@code NullPointerException} on attempts |
61 |
* to {@code add}, {@code put} or {@code offer} a {@code null}. A |
62 |
* {@code null} is used as a sentinel value to indicate failure of |
63 |
* {@code poll} operations. |
64 |
* |
65 |
* <p>A {@code BlockingQueue} may be capacity bounded. At any given |
66 |
* time it may have a {@code remainingCapacity} beyond which no |
67 |
* additional elements can be {@code put} without blocking. |
68 |
* A {@code BlockingQueue} without any intrinsic capacity constraints always |
69 |
* reports a remaining capacity of {@code Integer.MAX_VALUE}. |
70 |
* |
71 |
* <p>{@code BlockingQueue} implementations are designed to be used |
72 |
* primarily for producer-consumer queues, but additionally support |
73 |
* the {@link java.util.Collection} interface. So, for example, it is |
74 |
* possible to remove an arbitrary element from a queue using |
75 |
* {@code remove(x)}. However, such operations are in general |
76 |
* <em>not</em> performed very efficiently, and are intended for only |
77 |
* occasional use, such as when a queued message is cancelled. |
78 |
* |
79 |
* <p>{@code BlockingQueue} implementations are thread-safe. All |
80 |
* queuing methods achieve their effects atomically using internal |
81 |
* locks or other forms of concurrency control. However, the |
82 |
* <em>bulk</em> Collection operations {@code addAll}, |
83 |
* {@code containsAll}, {@code retainAll} and {@code removeAll} are |
84 |
* <em>not</em> necessarily performed atomically unless specified |
85 |
* otherwise in an implementation. So it is possible, for example, for |
86 |
* {@code addAll(c)} to fail (throwing an exception) after adding |
87 |
* only some of the elements in {@code c}. |
88 |
* |
89 |
* <p>A {@code BlockingQueue} does <em>not</em> intrinsically support |
90 |
* any kind of "close" or "shutdown" operation to |
91 |
* indicate that no more items will be added. The needs and usage of |
92 |
* such features tend to be implementation-dependent. For example, a |
93 |
* common tactic is for producers to insert special |
94 |
* <em>end-of-stream</em> or <em>poison</em> objects, that are |
95 |
* interpreted accordingly when taken by consumers. |
96 |
* |
97 |
* <p> |
98 |
* Usage example, based on a typical producer-consumer scenario. |
99 |
* Note that a {@code BlockingQueue} can safely be used with multiple |
100 |
* producers and multiple consumers. |
101 |
* <pre> {@code |
102 |
* class Producer implements Runnable { |
103 |
* private final BlockingQueue queue; |
104 |
* Producer(BlockingQueue q) { queue = q; } |
105 |
* public void run() { |
106 |
* try { |
107 |
* while (true) { queue.put(produce()); } |
108 |
* } catch (InterruptedException ex) { ... handle ...} |
109 |
* } |
110 |
* Object produce() { ... } |
111 |
* } |
112 |
* |
113 |
* class Consumer implements Runnable { |
114 |
* private final BlockingQueue queue; |
115 |
* Consumer(BlockingQueue q) { queue = q; } |
116 |
* public void run() { |
117 |
* try { |
118 |
* while (true) { consume(queue.take()); } |
119 |
* } catch (InterruptedException ex) { ... handle ...} |
120 |
* } |
121 |
* void consume(Object x) { ... } |
122 |
* } |
123 |
* |
124 |
* class Setup { |
125 |
* void main() { |
126 |
* BlockingQueue q = new SomeQueueImplementation(); |
127 |
* Producer p = new Producer(q); |
128 |
* Consumer c1 = new Consumer(q); |
129 |
* Consumer c2 = new Consumer(q); |
130 |
* new Thread(p).start(); |
131 |
* new Thread(c1).start(); |
132 |
* new Thread(c2).start(); |
133 |
* } |
134 |
* }}</pre> |
135 |
* |
136 |
* <p>Memory consistency effects: As with other concurrent |
137 |
* collections, actions in a thread prior to placing an object into a |
138 |
* {@code BlockingQueue} |
139 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
140 |
* actions subsequent to the access or removal of that element from |
141 |
* the {@code BlockingQueue} in another thread. |
142 |
* |
143 |
* <p>This interface is a member of the |
144 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
145 |
* Java Collections Framework</a>. |
146 |
* |
147 |
* @since 1.5 |
148 |
* @author Doug Lea |
149 |
* @param <E> the type of elements held in this queue |
150 |
*/ |
151 |
public interface BlockingQueue<E> extends Queue<E> { |
152 |
/** |
153 |
* Inserts the specified element into this queue if it is possible to do |
154 |
* so immediately without violating capacity restrictions, returning |
155 |
* {@code true} upon success and throwing an |
156 |
* {@code IllegalStateException} if no space is currently available. |
157 |
* When using a capacity-restricted queue, it is generally preferable to |
158 |
* use {@link #offer(Object) offer}. |
159 |
* |
160 |
* @param e the element to add |
161 |
* @return {@code true} (as specified by {@link Collection#add}) |
162 |
* @throws IllegalStateException if the element cannot be added at this |
163 |
* time due to capacity restrictions |
164 |
* @throws ClassCastException if the class of the specified element |
165 |
* prevents it from being added to this queue |
166 |
* @throws NullPointerException if the specified element is null |
167 |
* @throws IllegalArgumentException if some property of the specified |
168 |
* element prevents it from being added to this queue |
169 |
*/ |
170 |
boolean add(E e); |
171 |
|
172 |
/** |
173 |
* Inserts the specified element into this queue if it is possible to do |
174 |
* so immediately without violating capacity restrictions, returning |
175 |
* {@code true} upon success and {@code false} if no space is currently |
176 |
* available. When using a capacity-restricted queue, this method is |
177 |
* generally preferable to {@link #add}, which can fail to insert an |
178 |
* element only by throwing an exception. |
179 |
* |
180 |
* @param e the element to add |
181 |
* @return {@code true} if the element was added to this queue, else |
182 |
* {@code false} |
183 |
* @throws ClassCastException if the class of the specified element |
184 |
* prevents it from being added to this queue |
185 |
* @throws NullPointerException if the specified element is null |
186 |
* @throws IllegalArgumentException if some property of the specified |
187 |
* element prevents it from being added to this queue |
188 |
*/ |
189 |
boolean offer(E e); |
190 |
|
191 |
/** |
192 |
* Inserts the specified element into this queue, waiting if necessary |
193 |
* for space to become available. |
194 |
* |
195 |
* @param e the element to add |
196 |
* @throws InterruptedException if interrupted while waiting |
197 |
* @throws ClassCastException if the class of the specified element |
198 |
* prevents it from being added to this queue |
199 |
* @throws NullPointerException if the specified element is null |
200 |
* @throws IllegalArgumentException if some property of the specified |
201 |
* element prevents it from being added to this queue |
202 |
*/ |
203 |
void put(E e) throws InterruptedException; |
204 |
|
205 |
/** |
206 |
* Inserts the specified element into this queue, waiting up to the |
207 |
* specified wait time if necessary for space to become available. |
208 |
* |
209 |
* @param e the element to add |
210 |
* @param timeout how long to wait before giving up, in units of |
211 |
* {@code unit} |
212 |
* @param unit a {@code TimeUnit} determining how to interpret the |
213 |
* {@code timeout} parameter |
214 |
* @return {@code true} if successful, or {@code false} if |
215 |
* the specified waiting time elapses before space is available |
216 |
* @throws InterruptedException if interrupted while waiting |
217 |
* @throws ClassCastException if the class of the specified element |
218 |
* prevents it from being added to this queue |
219 |
* @throws NullPointerException if the specified element is null |
220 |
* @throws IllegalArgumentException if some property of the specified |
221 |
* element prevents it from being added to this queue |
222 |
*/ |
223 |
boolean offer(E e, long timeout, TimeUnit unit) |
224 |
throws InterruptedException; |
225 |
|
226 |
/** |
227 |
* Retrieves and removes the head of this queue, waiting if necessary |
228 |
* until an element becomes available. |
229 |
* |
230 |
* @return the head of this queue |
231 |
* @throws InterruptedException if interrupted while waiting |
232 |
*/ |
233 |
E take() throws InterruptedException; |
234 |
|
235 |
/** |
236 |
* Retrieves and removes the head of this queue, waiting up to the |
237 |
* specified wait time if necessary for an element to become available. |
238 |
* |
239 |
* @param timeout how long to wait before giving up, in units of |
240 |
* {@code unit} |
241 |
* @param unit a {@code TimeUnit} determining how to interpret the |
242 |
* {@code timeout} parameter |
243 |
* @return the head of this queue, or {@code null} if the |
244 |
* specified waiting time elapses before an element is available |
245 |
* @throws InterruptedException if interrupted while waiting |
246 |
*/ |
247 |
E poll(long timeout, TimeUnit unit) |
248 |
throws InterruptedException; |
249 |
|
250 |
/** |
251 |
* Returns the number of additional elements that this queue can ideally |
252 |
* (in the absence of memory or resource constraints) accept without |
253 |
* blocking, or {@code Integer.MAX_VALUE} if there is no intrinsic |
254 |
* limit. |
255 |
* |
256 |
* <p>Note that you <em>cannot</em> always tell if an attempt to insert |
257 |
* an element will succeed by inspecting {@code remainingCapacity} |
258 |
* because it may be the case that another thread is about to |
259 |
* insert or remove an element. |
260 |
* |
261 |
* @return the remaining capacity |
262 |
*/ |
263 |
int remainingCapacity(); |
264 |
|
265 |
/** |
266 |
* Removes a single instance of the specified element from this queue, |
267 |
* if it is present. More formally, removes an element {@code e} such |
268 |
* that {@code o.equals(e)}, if this queue contains one or more such |
269 |
* elements. |
270 |
* Returns {@code true} if this queue contained the specified element |
271 |
* (or equivalently, if this queue changed as a result of the call). |
272 |
* |
273 |
* @param o element to be removed from this queue, if present |
274 |
* @return {@code true} if this queue changed as a result of the call |
275 |
* @throws ClassCastException if the class of the specified element |
276 |
* is incompatible with this queue |
277 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
278 |
* @throws NullPointerException if the specified element is null |
279 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
280 |
*/ |
281 |
boolean remove(Object o); |
282 |
|
283 |
/** |
284 |
* Returns {@code true} if this queue contains the specified element. |
285 |
* More formally, returns {@code true} if and only if this queue contains |
286 |
* at least one element {@code e} such that {@code o.equals(e)}. |
287 |
* |
288 |
* @param o object to be checked for containment in this queue |
289 |
* @return {@code true} if this queue contains the specified element |
290 |
* @throws ClassCastException if the class of the specified element |
291 |
* is incompatible with this queue |
292 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
293 |
* @throws NullPointerException if the specified element is null |
294 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
295 |
*/ |
296 |
boolean contains(Object o); |
297 |
|
298 |
/** |
299 |
* Removes all available elements from this queue and adds them |
300 |
* to the given collection. This operation may be more |
301 |
* efficient than repeatedly polling this queue. A failure |
302 |
* encountered while attempting to add elements to |
303 |
* collection {@code c} may result in elements being in neither, |
304 |
* either or both collections when the associated exception is |
305 |
* thrown. Attempts to drain a queue to itself result in |
306 |
* {@code IllegalArgumentException}. Further, the behavior of |
307 |
* this operation is undefined if the specified collection is |
308 |
* modified while the operation is in progress. |
309 |
* |
310 |
* @param c the collection to transfer elements into |
311 |
* @return the number of elements transferred |
312 |
* @throws UnsupportedOperationException if addition of elements |
313 |
* is not supported by the specified collection |
314 |
* @throws ClassCastException if the class of an element of this queue |
315 |
* prevents it from being added to the specified collection |
316 |
* @throws NullPointerException if the specified collection is null |
317 |
* @throws IllegalArgumentException if the specified collection is this |
318 |
* queue, or some property of an element of this queue prevents |
319 |
* it from being added to the specified collection |
320 |
*/ |
321 |
int drainTo(Collection<? super E> c); |
322 |
|
323 |
/** |
324 |
* Removes at most the given number of available elements from |
325 |
* this queue and adds them to the given collection. A failure |
326 |
* encountered while attempting to add elements to |
327 |
* collection {@code c} may result in elements being in neither, |
328 |
* either or both collections when the associated exception is |
329 |
* thrown. Attempts to drain a queue to itself result in |
330 |
* {@code IllegalArgumentException}. Further, the behavior of |
331 |
* this operation is undefined if the specified collection is |
332 |
* modified while the operation is in progress. |
333 |
* |
334 |
* @param c the collection to transfer elements into |
335 |
* @param maxElements the maximum number of elements to transfer |
336 |
* @return the number of elements transferred |
337 |
* @throws UnsupportedOperationException if addition of elements |
338 |
* is not supported by the specified collection |
339 |
* @throws ClassCastException if the class of an element of this queue |
340 |
* prevents it from being added to the specified collection |
341 |
* @throws NullPointerException if the specified collection is null |
342 |
* @throws IllegalArgumentException if the specified collection is this |
343 |
* queue, or some property of an element of this queue prevents |
344 |
* it from being added to the specified collection |
345 |
*/ |
346 |
int drainTo(Collection<? super E> c, int maxElements); |
347 |
} |