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