ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingDeque.java
Revision: 1.25
Committed: Mon Feb 11 17:27:45 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.24: +2 -0 lines
Log Message:
add <caption> tags to all tables

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