ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingDeque.java
Revision: 1.10
Committed: Tue May 17 04:17:31 2005 UTC (19 years ago) by jsr166
Branch: MAIN
Changes since 1.9: +462 -110 lines
Log Message:
doc fixes

File Contents

# Content
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 java.util.concurrent;
8 import java.util.*;
9
10 /**
11 * A {@link Deque} that additionally supports blocking operations that wait
12 * for the deque to become non-empty when retrieving an element, and wait for
13 * space to become available in the deque when storing an element.
14 *
15 * <p><tt>BlockingDeque</tt> methods come in four forms, with different ways
16 * of handling operations that cannot be satisfied immediately, but may be
17 * satisfied at some point in the future:
18 * one throws an exception, the second returns a special value (either
19 * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
20 * blocks the current thread indefinitely until the operation can succeed,
21 * and the fourth blocks for only a given maximum time limit before giving
22 * up. These methods are summarized in the following table:
23 *
24 * <p>
25 * <table BORDER CELLPADDING=3 CELLSPACING=1>
26 * <tr>
27 * <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td>
28 * </tr>
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 #addFirst addFirst(e)}</td>
39 * <td>{@link #offerFirst(Object) offerFirst(e)}</td>
40 * <td>{@link #putFirst putFirst(e)}</td>
41 * <td>{@link #offerFirst(Object, long, TimeUnit) offerFirst(e, time, unit)}</td>
42 * </tr>
43 * <tr>
44 * <td><b>Remove</b></td>
45 * <td>{@link #removeFirst removeFirst()}</td>
46 * <td>{@link #pollFirst pollFirst()}</td>
47 * <td>{@link #takeFirst takeFirst()}</td>
48 * <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
49 * </tr>
50 * <tr>
51 * <td><b>Examine</b></td>
52 * <td>{@link #getFirst getFirst()}</td>
53 * <td>{@link #peekFirst peekFirst()}</td>
54 * <td><em>not applicable</em></td>
55 * <td><em>not applicable</em></td>
56 * </tr>
57 * <tr>
58 * <td ALIGN=CENTER COLSPAN = 5> <b>Last Element (Tail)</b></td>
59 * </tr>
60 * <tr>
61 * <td></td>
62 * <td ALIGN=CENTER><em>Throws exception</em></td>
63 * <td ALIGN=CENTER><em>Special value</em></td>
64 * <td ALIGN=CENTER><em>Blocks</em></td>
65 * <td ALIGN=CENTER><em>Times out</em></td>
66 * </tr>
67 * <tr>
68 * <td><b>Insert</b></td>
69 * <td>{@link #addLast addLast(e)}</td>
70 * <td>{@link #offerLast(Object) offerLast(e)}</td>
71 * <td>{@link #putLast putLast(e)}</td>
72 * <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td>
73 * </tr>
74 * <tr>
75 * <td><b>Remove</b></td>
76 * <td>{@link #removeLast() removeLast()}</td>
77 * <td>{@link #pollLast() pollLast()}</td>
78 * <td>{@link #takeLast takeLast()}</td>
79 * <td>{@link #pollLast(long, TimeUnit) pollLast(time, unit)}</td>
80 * </tr>
81 * <tr>
82 * <td><b>Examine</b></td>
83 * <td>{@link #getLast getLast()}</td>
84 * <td>{@link #peekLast peekLast()}</td>
85 * <td><em>not applicable</em></td>
86 * <td><em>not applicable</em></td>
87 * </tr>
88 * </table>
89 *
90 * <p>Like any {@link BlockingQueue}, a <tt>BlockingDeque</tt> is thread safe,
91 * does not permit null elements, and may (or may not) be
92 * capacity-constrained.
93 *
94 * <p>A <tt>BlockingDeque</tt> implementation may be used directly as a FIFO
95 * <tt>BlockingQueue</tt>. The methods inherited from the
96 * <tt>BlockingQueue</tt> interface are precisely equivalent to
97 * <tt>BlockingDeque</tt> methods as indicated in the following table:
98 *
99 * <p>
100 * <table BORDER CELLPADDING=3 CELLSPACING=1>
101 * <tr>
102 * <td ALIGN=CENTER> <b><tt>BlockingQueue</tt> Method</b></td>
103 * <td ALIGN=CENTER> <b>Equivalent <tt>BlockingDeque</tt> Method</b></td>
104 * </tr>
105 * <tr>
106 * <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td>
107 * </tr>
108 * <tr>
109 * <td>{@link #add(Object) add(e)}</td>
110 * <td>{@link #addLast(Object) addLast(e)}</td>
111 * </tr>
112 * <tr>
113 * <td>{@link #offer(Object) offer(e)}</td>
114 * <td>{@link #offerLast(Object) offerLast(e)}</td>
115 * </tr>
116 * <tr>
117 * <td>{@link #put(Object) put(e)}</td>
118 * <td>{@link #putLast(Object) putLast(e)}</td>
119 * </tr>
120 * <tr>
121 * <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
122 * <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td>
123 * </tr>
124 * <tr>
125 * <td ALIGN=CENTER COLSPAN = 2> <b>Remove</b></td>
126 * </tr>
127 * <tr>
128 * <td>{@link #remove() remove()}</td>
129 * <td>{@link #removeFirst() removeFirst()}</td>
130 * </tr>
131 * <tr>
132 * <td>{@link #poll() poll()}</td>
133 * <td>{@link #pollFirst() pollFirst()}</td>
134 * </tr>
135 * <tr>
136 * <td>{@link #take() take()}</td>
137 * <td>{@link #takeFirst() takeFirst()}</td>
138 * </tr>
139 * <tr>
140 * <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
141 * <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
142 * </tr>
143 * <tr>
144 * <td ALIGN=CENTER COLSPAN = 2> <b>Examine</b></td>
145 * </tr>
146 * <tr>
147 * <td>{@link #element() element()}</td>
148 * <td>{@link #getFirst() getFirst()}</td>
149 * </tr>
150 * <tr>
151 * <td>{@link #peek() peek()}</td>
152 * <td>{@link #peekFirst() peekFirst()}</td>
153 * </tr>
154 * </table>
155 *
156 * <p>This interface is a member of the
157 * <a href="{@docRoot}/../guide/collections/index.html">
158 * Java Collections Framework</a>.
159 *
160 * @since 1.6
161 * @author Doug Lea
162 * @param <E> the type of elements held in this collection
163 */
164 public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
165 /*
166 * We have "diamond" multiple interface inheritance here, and that
167 * introduces ambiguities. Methods might end up with different
168 * specs depending on the branch chosen by javadoc. Thus a lot of
169 * methods specs here are copied from superinterfaces.
170 */
171
172 /**
173 * Inserts the specified element at the front of this deque if it is
174 * possible to do so immediately without violating capacity restrictions,
175 * throwing an <tt>IllegalStateException</tt> if no space is currently
176 * available. When using a capacity-restricted deque, it is generally
177 * preferable to use {@link #offerFirst(Object) offerFirst}.
178 *
179 * @throws IllegalStateException {@inheritDoc}
180 * @throws ClassCastException {@inheritDoc}
181 * @throws NullPointerException if the specified element is null
182 * @throws IllegalArgumentException {@inheritDoc}
183 */
184 void addFirst(E e);
185
186 /**
187 * Inserts the specified element at the end of this deque if it is
188 * possible to do so immediately without violating capacity restrictions,
189 * throwing an <tt>IllegalStateException</tt> if no space is currently
190 * available. When using a capacity-restricted deque, it is generally
191 * preferable to use {@link #offerLast(Object) offerLast}.
192 *
193 * @throws IllegalStateException {@inheritDoc}
194 * @throws ClassCastException {@inheritDoc}
195 * @throws NullPointerException if the specified element is null
196 * @throws IllegalArgumentException {@inheritDoc}
197 */
198 void addLast(E e);
199
200 /**
201 * Inserts the specified element at the front of this deque if it is
202 * possible to do so immediately without violating capacity restrictions.
203 * When using a capacity-restricted deque, this method is generally
204 * preferable to the {@link #addFirst(Object) addFirst} method, which can
205 * fail to insert an element only by throwing an exception.
206 *
207 * @throws ClassCastException {@inheritDoc}
208 * @throws NullPointerException if the specified element is null
209 * @throws IllegalArgumentException {@inheritDoc}
210 */
211 boolean offerFirst(E e);
212
213 /**
214 * Inserts the specified element at the end of this deque if it is
215 * possible to do so immediately without violating capacity restrictions.
216 * When using a capacity-restricted deque, this method is generally
217 * preferable to the {@link #addLast(Object) addLast} method, which can
218 * fail to insert an element only by throwing an exception.
219 *
220 * @throws ClassCastException {@inheritDoc}
221 * @throws NullPointerException if the specified element is null
222 * @throws IllegalArgumentException {@inheritDoc}
223 */
224 boolean offerLast(E e);
225
226 /**
227 * Inserts the specified element as the first element of this deque,
228 * waiting if necessary for space to become available.
229 *
230 * @param e the element to add
231 * @throws InterruptedException if interrupted while waiting
232 * @throws ClassCastException if the class of the specified element
233 * prevents it from being added to this deque
234 * @throws NullPointerException if the specified element is null
235 * @throws IllegalArgumentException if some property of the specified
236 * element prevents it from being added to this deque
237 */
238 void putFirst(E e) throws InterruptedException;
239
240 /**
241 * Inserts the specified element as the last element of this deque,
242 * waiting if necessary for space to become available.
243 *
244 * @param e the element to add
245 * @throws InterruptedException if interrupted while waiting
246 * @throws ClassCastException if the class of the specified element
247 * prevents it from being added to this deque
248 * @throws NullPointerException if the specified element is null
249 * @throws IllegalArgumentException if some property of the specified
250 * element prevents it from being added to this deque
251 */
252 void putLast(E e) throws InterruptedException;
253
254 /**
255 * Inserts the specified element as the first element of this deque,
256 * waiting up to the specified wait time if necessary for space to
257 * become available.
258 *
259 * @param e the element to add
260 * @param timeout how long to wait before giving up, in units of
261 * <tt>unit</tt>
262 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
263 * <tt>timeout</tt> parameter
264 * @return <tt>true</tt> if successful, or <tt>false</tt> if
265 * the specified waiting time elapses before space is available
266 * @throws InterruptedException if interrupted while waiting
267 * @throws ClassCastException if the class of the specified element
268 * prevents it from being added to this deque
269 * @throws NullPointerException if the specified element is null
270 * @throws IllegalArgumentException if some property of the specified
271 * element prevents it from being added to this deque
272 */
273 boolean offerFirst(E e, long timeout, TimeUnit unit)
274 throws InterruptedException;
275
276 /**
277 * Inserts the specified element as the last element of this deque,
278 * waiting up to the specified wait time if necessary for space to
279 * become available.
280 *
281 * @param e the element to add
282 * @param timeout how long to wait before giving up, in units of
283 * <tt>unit</tt>
284 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
285 * <tt>timeout</tt> parameter
286 * @return <tt>true</tt> if successful, or <tt>false</tt> if
287 * the specified waiting time elapses before space is available
288 * @throws InterruptedException if interrupted while waiting
289 * @throws ClassCastException if the class of the specified element
290 * prevents it from being added to this deque
291 * @throws NullPointerException if the specified element is null
292 * @throws IllegalArgumentException if some property of the specified
293 * element prevents it from being added to this deque
294 */
295 boolean offerLast(E e, long timeout, TimeUnit unit)
296 throws InterruptedException;
297
298 /**
299 * Retrieves and removes the first element of this deque, waiting
300 * if necessary until an element becomes available.
301 *
302 * @return the head of this deque
303 * @throws InterruptedException if interrupted while waiting
304 */
305 E takeFirst() throws InterruptedException;
306
307 /**
308 * Retrieves and removes the last element of this deque, waiting
309 * if necessary until an element becomes available.
310 *
311 * @return the tail of this deque
312 * @throws InterruptedException if interrupted while waiting
313 */
314 E takeLast() throws InterruptedException;
315
316 /**
317 * Retrieves and removes the first element of this deque, waiting
318 * up to the specified wait time if necessary for an element to
319 * become available.
320 *
321 * @param timeout how long to wait before giving up, in units of
322 * <tt>unit</tt>
323 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
324 * <tt>timeout</tt> parameter
325 * @return the head of this deque, or <tt>null</tt> if the specified
326 * waiting time elapses before an element is available
327 * @throws InterruptedException if interrupted while waiting
328 */
329 E pollFirst(long timeout, TimeUnit unit)
330 throws InterruptedException;
331
332 /**
333 * Retrieves and removes the last element of this deque, waiting
334 * up to the specified wait time if necessary for an element to
335 * become available.
336 *
337 * @param timeout how long to wait before giving up, in units of
338 * <tt>unit</tt>
339 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
340 * <tt>timeout</tt> parameter
341 * @return the tail of this deque, or <tt>null</tt> if the specified
342 * waiting time elapses before an element is available
343 * @throws InterruptedException if interrupted while waiting
344 */
345 E pollLast(long timeout, TimeUnit unit)
346 throws InterruptedException;
347
348 /**
349 * Removes the first occurrence of the specified element from this deque.
350 * If the deque does not contain the element, it is unchanged.
351 * More formally, removes the first element <tt>e</tt> such that
352 * <tt>o.equals(e)</tt> (if such an element exists).
353 * Returns true if this deque contained the specified element (or
354 * equivalently, if this deque changed as a result of the call).
355 *
356 * @param o element to be removed from this deque, if present
357 * @return <tt>true</tt> if an element was removed as a result of this call
358 * @throws ClassCastException if the class of the specified element
359 * is incompatible with this deque (optional)
360 * @throws NullPointerException if the specified element is null (optional)
361 */
362 boolean removeFirstOccurrence(Object o);
363
364 /**
365 * Removes the last occurrence of the specified element from this deque.
366 * If the deque does not contain the element, it is unchanged.
367 * More formally, removes the last element <tt>e</tt> such that
368 * <tt>o.equals(e)</tt> (if such an element exists).
369 * Returns true if this deque contained the specified element (or
370 * equivalently, if this deque changed as a result of the call).
371 *
372 * @param o element to be removed from this deque, if present
373 * @return <tt>true</tt> if an element was removed as a result of this call
374 * @throws ClassCastException if the class of the specified element
375 * is incompatible with this deque (optional)
376 * @throws NullPointerException if the specified element is null (optional)
377 */
378 boolean removeLastOccurrence(Object o);
379
380 // *** BlockingQueue methods ***
381
382 /**
383 * Inserts the specified element into the queue represented by this deque
384 * (in other words, at the tail of this deque) if it is possible to do so
385 * immediately without violating capacity restrictions, returning
386 * <tt>true</tt> upon success and throwing an
387 * <tt>IllegalStateException</tt> if no space is currently available.
388 * When using a capacity-restricted deque, it is generally preferable to
389 * use {@link #offer(Object) offer}.
390 *
391 * <p>This method is equivalent to {@link #addLast(Object) addLast}.
392 *
393 * @param e the element to add
394 * @throws IllegalStateException {@inheritDoc}
395 * @throws ClassCastException if the class of the specified element
396 * prevents it from being added to this deque
397 * @throws NullPointerException if the specified element is null
398 * @throws IllegalArgumentException if some property of the specified
399 * element prevents it from being added to this deque
400 */
401 boolean add(E e);
402
403 /**
404 * Inserts the specified element into the queue represented by this deque
405 * (in other words, at the tail of this deque) if it is possible to do so
406 * immediately without violating capacity restrictions, returning
407 * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
408 * available. When using a capacity-restricted deque, this method is
409 * generally preferable to the {@link #add} method, which can fail to
410 * insert an element only by throwing an exception.
411 *
412 * <p>This method is equivalent to {@link #offerLast(Object) offerLast}.
413 *
414 * @param e the element to add
415 * @throws ClassCastException if the class of the specified element
416 * prevents it from being added to this deque
417 * @throws NullPointerException if the specified element is null
418 * @throws IllegalArgumentException if some property of the specified
419 * element prevents it from being added to this deque
420 */
421 boolean offer(E e);
422
423 /**
424 * Inserts the specified element into the queue represented by this deque
425 * (in other words, at the tail of this deque), waiting if necessary for
426 * space to become available.
427 *
428 * <p>This method is equivalent to {@link #putLast(Object) putLast}.
429 *
430 * @param e the element to add
431 * @throws InterruptedException {@inheritDoc}
432 * @throws ClassCastException if the class of the specified element
433 * prevents it from being added to this deque
434 * @throws NullPointerException if the specified element is null
435 * @throws IllegalArgumentException if some property of the specified
436 * element prevents it from being added to this deque
437 */
438 void put(E e) throws InterruptedException;
439
440 /**
441 * Inserts the specified element into the queue represented by this deque
442 * (in other words, at the tail of this deque), waiting up to the
443 * specified wait time if necessary for space to become available.
444 *
445 * <p>This method is equivalent to
446 * {@link #offerLast(Object,long,TimeUnit) #offerLast}.
447 *
448 * @param e the element to add
449 * @return <tt>true</tt> if it was possible to add the element to
450 * this deque, else <tt>false</tt>
451 * @throws InterruptedException {@inheritDoc}
452 * @throws ClassCastException if the class of the specified element
453 * prevents it from being added to this deque
454 * @throws NullPointerException if the specified element is null
455 * @throws IllegalArgumentException if some property of the specified
456 * element prevents it from being added to this deque
457 */
458 boolean offer(E e, long timeout, TimeUnit unit)
459 throws InterruptedException;
460
461 /**
462 * Retrieves and removes the head of the queue represented by this deque
463 * (in other words, the first element of this deque).
464 * This method differs from {@link #poll} only in that it throws an
465 * exception if this deque is empty.
466 *
467 * <p>This method is equivalent to {@link #removeFirst() removeFirst}.
468 *
469 * @return the head of the queue represented by this deque
470 * @throws NoSuchElementException if this deque is empty
471 */
472 E remove();
473
474 /**
475 * Retrieves and removes the head of the queue represented by this deque
476 * (in other words, the first element of this deque), or returns
477 * <tt>null</tt> if this deque is empty.
478 *
479 * <p>This method is equivalent to {@link #pollFirst()}.
480 *
481 * @return the head of this deque, or <tt>null</tt> if this deque is empty
482 */
483 E poll();
484
485 /**
486 * Retrieves and removes the head of the queue represented by this deque
487 * (in other words, the first element of this deque), waiting if
488 * necessary until an element becomes available.
489 *
490 * <p>This method is equivalent to {@link #takeFirst() takeFirst}.
491 *
492 * @return the head of this deque
493 * @throws InterruptedException if interrupted while waiting
494 */
495 E take() throws InterruptedException;
496
497 /**
498 * Retrieves and removes the head of the queue represented by this deque
499 * (in other words, the first element of this deque), waiting up to the
500 * specified wait time if necessary for an element to become available.
501 *
502 * <p>This method is equivalent to
503 * {@link #pollFirst(long,TimeUnit) pollFirst}.
504 *
505 * @return the head of this deque, or <tt>null</tt> if the
506 * specified waiting time elapses before an element is available
507 * @throws InterruptedException if interrupted while waiting
508 */
509 E poll(long timeout, TimeUnit unit)
510 throws InterruptedException;
511
512 /**
513 * Retrieves, but does not remove, the head of the queue represented by
514 * this deque (in other words, the first element of this deque).
515 * This method differs from {@link #peek peek} only in that it throws an
516 * exception if this deque is empty.
517 *
518 * <p>This method is equivalent to {@link #getFirst() getFirst}.
519 *
520 * @return the head of this deque
521 * @throws NoSuchElementException if this deque is empty
522 */
523 E element();
524
525 /**
526 * Retrieves, but does not remove, the head of the queue represented by
527 * this deque (in other words, the first element of this deque), or
528 * returns <tt>null</tt> if this deque is empty.
529 *
530 * <p>This method is equivalent to {@link #peekFirst() peekFirst}.
531 *
532 * @return the head of this deque, or <tt>null</tt> if this deque is empty
533 */
534 E peek();
535
536 /**
537 * Removes the first occurrence of the specified element from this deque.
538 * If the deque does not contain the element, it is unchanged.
539 * More formally, removes the first element <tt>e</tt> such that
540 * <tt>o.equals(e)</tt> (if such an element exists).
541 * Returns true if this deque contained the specified element (or
542 * equivalently, if this deque changed as a result of the call).
543 *
544 * <p>This method is equivalent to
545 * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
546 *
547 * @param o element to be removed from this deque, if present
548 * @return <tt>true</tt> if this deque changed as a result of the call
549 * @throws ClassCastException if the class of the specified element
550 * is incompatible with this deque (optional)
551 * @throws NullPointerException if the specified element is null (optional)
552 */
553 boolean remove(Object o);
554
555 /**
556 * Returns <tt>true</tt> if this deque contains the specified element.
557 * More formally, returns <tt>true</tt> if and only if this deque contains
558 * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
559 *
560 * @param o object to be checked for containment in this deque
561 * @return <tt>true</tt> if this deque contains the specified element
562 * @throws ClassCastException if the class of the specified element
563 * is incompatible with this deque (optional)
564 * @throws NullPointerException if the specified element is null (optional)
565 */
566 public boolean contains(Object o);
567
568 /**
569 * Returns the number of elements in this deque.
570 *
571 * @return the number of elements in this deque
572 */
573 public int size();
574
575 /**
576 * Returns an iterator over the elements in this deque. The elements
577 * will be ordered from first (head) to last (tail).
578 *
579 * @return an <tt>Iterator</tt> over the elements in this deque
580 */
581 Iterator<E> iterator();
582
583 // *** Stack methods ***
584
585 /**
586 * Pushes an element onto the stack represented by this deque. In other
587 * words, inserts the element at the front of this deque unless it would
588 * violate capacity restrictions.
589 *
590 * <p>This method is equivalent to {@link #addFirst(Object) addFirst}.
591 *
592 * @throws IllegalStateException {@inheritDoc}
593 * @throws ClassCastException {@inheritDoc}
594 * @throws NullPointerException if the specified element is null
595 * @throws IllegalArgumentException {@inheritDoc}
596 */
597 void push(E e);
598 }