ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingDeque.java
Revision: 1.18
Committed: Wed Sep 14 21:56:17 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.17: +7 -6 lines
Log Message:
happens-before

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>Memory consistency effects: As with other concurrent
158 * collections, actions in a thread prior to placing an object into a
159 * {@code BlockingDeque}
160 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
161 * actions subsequent to the access or removal of that element from
162 * the {@code BlockingDeque} in another thread.
163 *
164 * <p>This interface is a member of the
165 * <a href="{@docRoot}/../guide/collections/index.html">
166 * Java Collections Framework</a>.
167 *
168 * @since 1.6
169 * @author Doug Lea
170 * @param <E> the type of elements held in this collection
171 */
172 public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
173 /*
174 * We have "diamond" multiple interface inheritance here, and that
175 * introduces ambiguities. Methods might end up with different
176 * specs depending on the branch chosen by javadoc. Thus a lot of
177 * methods specs here are copied from superinterfaces.
178 */
179
180 /**
181 * Inserts the specified element at the front of this deque if it is
182 * possible to do so immediately without violating capacity restrictions,
183 * throwing an <tt>IllegalStateException</tt> if no space is currently
184 * available. When using a capacity-restricted deque, it is generally
185 * preferable to use {@link #offerFirst(Object) offerFirst}.
186 *
187 * @param e the element to add
188 * @throws IllegalStateException {@inheritDoc}
189 * @throws ClassCastException {@inheritDoc}
190 * @throws NullPointerException if the specified element is null
191 * @throws IllegalArgumentException {@inheritDoc}
192 */
193 void addFirst(E e);
194
195 /**
196 * Inserts the specified element at the end of this deque if it is
197 * possible to do so immediately without violating capacity restrictions,
198 * throwing an <tt>IllegalStateException</tt> if no space is currently
199 * available. When using a capacity-restricted deque, it is generally
200 * preferable to use {@link #offerLast(Object) offerLast}.
201 *
202 * @param e the element to add
203 * @throws IllegalStateException {@inheritDoc}
204 * @throws ClassCastException {@inheritDoc}
205 * @throws NullPointerException if the specified element is null
206 * @throws IllegalArgumentException {@inheritDoc}
207 */
208 void addLast(E e);
209
210 /**
211 * Inserts the specified element at the front of this deque if it is
212 * possible to do so immediately without violating capacity restrictions,
213 * returning <tt>true</tt> upon success and <tt>false</tt> if no space is
214 * currently available.
215 * When using a capacity-restricted deque, this method is generally
216 * preferable to the {@link #addFirst(Object) addFirst} method, which can
217 * fail to insert an element only by throwing an exception.
218 *
219 * @param e the element to add
220 * @throws ClassCastException {@inheritDoc}
221 * @throws NullPointerException if the specified element is null
222 * @throws IllegalArgumentException {@inheritDoc}
223 */
224 boolean offerFirst(E e);
225
226 /**
227 * Inserts the specified element at the end of this deque if it is
228 * possible to do so immediately without violating capacity restrictions,
229 * returning <tt>true</tt> upon success and <tt>false</tt> if no space is
230 * currently available.
231 * When using a capacity-restricted deque, this method is generally
232 * preferable to the {@link #addLast(Object) addLast} method, which can
233 * fail to insert an element only by throwing an exception.
234 *
235 * @param e the element to add
236 * @throws ClassCastException {@inheritDoc}
237 * @throws NullPointerException if the specified element is null
238 * @throws IllegalArgumentException {@inheritDoc}
239 */
240 boolean offerLast(E e);
241
242 /**
243 * Inserts the specified element at the front of this deque,
244 * waiting if necessary for space to become available.
245 *
246 * @param e the element to add
247 * @throws InterruptedException if interrupted while waiting
248 * @throws ClassCastException if the class of the specified element
249 * prevents it from being added to this deque
250 * @throws NullPointerException if the specified element is null
251 * @throws IllegalArgumentException if some property of the specified
252 * element prevents it from being added to this deque
253 */
254 void putFirst(E e) throws InterruptedException;
255
256 /**
257 * Inserts the specified element at the end of this deque,
258 * waiting if necessary for space to become available.
259 *
260 * @param e the element to add
261 * @throws InterruptedException if interrupted while waiting
262 * @throws ClassCastException if the class of the specified element
263 * prevents it from being added to this deque
264 * @throws NullPointerException if the specified element is null
265 * @throws IllegalArgumentException if some property of the specified
266 * element prevents it from being added to this deque
267 */
268 void putLast(E e) throws InterruptedException;
269
270 /**
271 * Inserts the specified element at the front of this deque,
272 * waiting up to the specified wait time if necessary for space to
273 * become available.
274 *
275 * @param e the element to add
276 * @param timeout how long to wait before giving up, in units of
277 * <tt>unit</tt>
278 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
279 * <tt>timeout</tt> parameter
280 * @return <tt>true</tt> if successful, or <tt>false</tt> if
281 * the specified waiting time elapses before space is available
282 * @throws InterruptedException if interrupted while waiting
283 * @throws ClassCastException if the class of the specified element
284 * prevents it from being added to this deque
285 * @throws NullPointerException if the specified element is null
286 * @throws IllegalArgumentException if some property of the specified
287 * element prevents it from being added to this deque
288 */
289 boolean offerFirst(E e, long timeout, TimeUnit unit)
290 throws InterruptedException;
291
292 /**
293 * Inserts the specified element at the end of this deque,
294 * waiting up to the specified wait time if necessary for space to
295 * become available.
296 *
297 * @param e the element to add
298 * @param timeout how long to wait before giving up, in units of
299 * <tt>unit</tt>
300 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
301 * <tt>timeout</tt> parameter
302 * @return <tt>true</tt> if successful, or <tt>false</tt> if
303 * the specified waiting time elapses before space is available
304 * @throws InterruptedException if interrupted while waiting
305 * @throws ClassCastException if the class of the specified element
306 * prevents it from being added to this deque
307 * @throws NullPointerException if the specified element is null
308 * @throws IllegalArgumentException if some property of the specified
309 * element prevents it from being added to this deque
310 */
311 boolean offerLast(E e, long timeout, TimeUnit unit)
312 throws InterruptedException;
313
314 /**
315 * Retrieves and removes the first element of this deque, waiting
316 * if necessary until an element becomes available.
317 *
318 * @return the head of this deque
319 * @throws InterruptedException if interrupted while waiting
320 */
321 E takeFirst() throws InterruptedException;
322
323 /**
324 * Retrieves and removes the last element of this deque, waiting
325 * if necessary until an element becomes available.
326 *
327 * @return the tail of this deque
328 * @throws InterruptedException if interrupted while waiting
329 */
330 E takeLast() throws InterruptedException;
331
332 /**
333 * Retrieves and removes the first 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 head 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 pollFirst(long timeout, TimeUnit unit)
346 throws InterruptedException;
347
348 /**
349 * Retrieves and removes the last element of this deque, waiting
350 * up to the specified wait time if necessary for an element to
351 * become available.
352 *
353 * @param timeout how long to wait before giving up, in units of
354 * <tt>unit</tt>
355 * @param unit a <tt>TimeUnit</tt> determining how to interpret the
356 * <tt>timeout</tt> parameter
357 * @return the tail of this deque, or <tt>null</tt> if the specified
358 * waiting time elapses before an element is available
359 * @throws InterruptedException if interrupted while waiting
360 */
361 E pollLast(long timeout, TimeUnit unit)
362 throws InterruptedException;
363
364 /**
365 * Removes the first 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 first element <tt>e</tt> such that
368 * <tt>o.equals(e)</tt> (if such an element exists).
369 * Returns <tt>true</tt> if this deque contained the specified element
370 * (or 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 removeFirstOccurrence(Object o);
379
380 /**
381 * Removes the last occurrence of the specified element from this deque.
382 * If the deque does not contain the element, it is unchanged.
383 * More formally, removes the last element <tt>e</tt> such that
384 * <tt>o.equals(e)</tt> (if such an element exists).
385 * Returns <tt>true</tt> if this deque contained the specified element
386 * (or equivalently, if this deque changed as a result of the call).
387 *
388 * @param o element to be removed from this deque, if present
389 * @return <tt>true</tt> if an element was removed as a result of this call
390 * @throws ClassCastException if the class of the specified element
391 * is incompatible with this deque (optional)
392 * @throws NullPointerException if the specified element is null (optional)
393 */
394 boolean removeLastOccurrence(Object o);
395
396 // *** BlockingQueue methods ***
397
398 /**
399 * Inserts the specified element into the queue represented by this deque
400 * (in other words, at the tail of this deque) if it is possible to do so
401 * immediately without violating capacity restrictions, returning
402 * <tt>true</tt> upon success and throwing an
403 * <tt>IllegalStateException</tt> if no space is currently available.
404 * When using a capacity-restricted deque, it is generally preferable to
405 * use {@link #offer(Object) offer}.
406 *
407 * <p>This method is equivalent to {@link #addLast(Object) addLast}.
408 *
409 * @param e the element to add
410 * @throws IllegalStateException {@inheritDoc}
411 * @throws ClassCastException if the class of the specified element
412 * prevents it from being added to this deque
413 * @throws NullPointerException if the specified element is null
414 * @throws IllegalArgumentException if some property of the specified
415 * element prevents it from being added to this deque
416 */
417 boolean add(E e);
418
419 /**
420 * Inserts the specified element into the queue represented by this deque
421 * (in other words, at the tail of this deque) if it is possible to do so
422 * immediately without violating capacity restrictions, returning
423 * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
424 * available. When using a capacity-restricted deque, this method is
425 * generally preferable to the {@link #add} method, which can fail to
426 * insert an element only by throwing an exception.
427 *
428 * <p>This method is equivalent to {@link #offerLast(Object) offerLast}.
429 *
430 * @param e the element to add
431 * @throws ClassCastException if the class of the specified element
432 * prevents it from being added to this deque
433 * @throws NullPointerException if the specified element is null
434 * @throws IllegalArgumentException if some property of the specified
435 * element prevents it from being added to this deque
436 */
437 boolean offer(E e);
438
439 /**
440 * Inserts the specified element into the queue represented by this deque
441 * (in other words, at the tail of this deque), waiting if necessary for
442 * space to become available.
443 *
444 * <p>This method is equivalent to {@link #putLast(Object) putLast}.
445 *
446 * @param e the element to add
447 * @throws InterruptedException {@inheritDoc}
448 * @throws ClassCastException if the class of the specified element
449 * prevents it from being added to this deque
450 * @throws NullPointerException if the specified element is null
451 * @throws IllegalArgumentException if some property of the specified
452 * element prevents it from being added to this deque
453 */
454 void put(E e) throws InterruptedException;
455
456 /**
457 * Inserts the specified element into the queue represented by this deque
458 * (in other words, at the tail of this deque), waiting up to the
459 * specified wait time if necessary for space to become available.
460 *
461 * <p>This method is equivalent to
462 * {@link #offerLast(Object,long,TimeUnit) offerLast}.
463 *
464 * @param e the element to add
465 * @return <tt>true</tt> if the element was added to this deque, else
466 * <tt>false</tt>
467 * @throws InterruptedException {@inheritDoc}
468 * @throws ClassCastException if the class of the specified element
469 * prevents it from being added to this deque
470 * @throws NullPointerException if the specified element is null
471 * @throws IllegalArgumentException if some property of the specified
472 * element prevents it from being added to this deque
473 */
474 boolean offer(E e, long timeout, TimeUnit unit)
475 throws InterruptedException;
476
477 /**
478 * Retrieves and removes the head of the queue represented by this deque
479 * (in other words, the first element of this deque).
480 * This method differs from {@link #poll poll} only in that it
481 * throws an exception if this deque is empty.
482 *
483 * <p>This method is equivalent to {@link #removeFirst() removeFirst}.
484 *
485 * @return the head of the queue represented by this deque
486 * @throws NoSuchElementException if this deque is empty
487 */
488 E remove();
489
490 /**
491 * Retrieves and removes the head of the queue represented by this deque
492 * (in other words, the first element of this deque), or returns
493 * <tt>null</tt> if this deque is empty.
494 *
495 * <p>This method is equivalent to {@link #pollFirst()}.
496 *
497 * @return the head of this deque, or <tt>null</tt> if this deque is empty
498 */
499 E poll();
500
501 /**
502 * Retrieves and removes the head of the queue represented by this deque
503 * (in other words, the first element of this deque), waiting if
504 * necessary until an element becomes available.
505 *
506 * <p>This method is equivalent to {@link #takeFirst() takeFirst}.
507 *
508 * @return the head of this deque
509 * @throws InterruptedException if interrupted while waiting
510 */
511 E take() throws InterruptedException;
512
513 /**
514 * Retrieves and removes the head of the queue represented by this deque
515 * (in other words, the first element of this deque), waiting up to the
516 * specified wait time if necessary for an element to become available.
517 *
518 * <p>This method is equivalent to
519 * {@link #pollFirst(long,TimeUnit) pollFirst}.
520 *
521 * @return the head of this deque, or <tt>null</tt> if the
522 * specified waiting time elapses before an element is available
523 * @throws InterruptedException if interrupted while waiting
524 */
525 E poll(long timeout, TimeUnit unit)
526 throws InterruptedException;
527
528 /**
529 * Retrieves, but does not remove, the head of the queue represented by
530 * this deque (in other words, the first element of this deque).
531 * This method differs from {@link #peek peek} only in that it throws an
532 * exception if this deque is empty.
533 *
534 * <p>This method is equivalent to {@link #getFirst() getFirst}.
535 *
536 * @return the head of this deque
537 * @throws NoSuchElementException if this deque is empty
538 */
539 E element();
540
541 /**
542 * Retrieves, but does not remove, the head of the queue represented by
543 * this deque (in other words, the first element of this deque), or
544 * returns <tt>null</tt> if this deque is empty.
545 *
546 * <p>This method is equivalent to {@link #peekFirst() peekFirst}.
547 *
548 * @return the head of this deque, or <tt>null</tt> if this deque is empty
549 */
550 E peek();
551
552 /**
553 * Removes the first occurrence of the specified element from this deque.
554 * If the deque does not contain the element, it is unchanged.
555 * More formally, removes the first element <tt>e</tt> such that
556 * <tt>o.equals(e)</tt> (if such an element exists).
557 * Returns <tt>true</tt> if this deque contained the specified element
558 * (or equivalently, if this deque changed as a result of the call).
559 *
560 * <p>This method is equivalent to
561 * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
562 *
563 * @param o element to be removed from this deque, if present
564 * @return <tt>true</tt> if this deque changed as a result of the call
565 * @throws ClassCastException if the class of the specified element
566 * is incompatible with this deque (optional)
567 * @throws NullPointerException if the specified element is null (optional)
568 */
569 boolean remove(Object o);
570
571 /**
572 * Returns <tt>true</tt> if this deque contains the specified element.
573 * More formally, returns <tt>true</tt> if and only if this deque contains
574 * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
575 *
576 * @param o object to be checked for containment in this deque
577 * @return <tt>true</tt> if this deque contains the specified element
578 * @throws ClassCastException if the class of the specified element
579 * is incompatible with this deque (optional)
580 * @throws NullPointerException if the specified element is null (optional)
581 */
582 public boolean contains(Object o);
583
584 /**
585 * Returns the number of elements in this deque.
586 *
587 * @return the number of elements in this deque
588 */
589 public int size();
590
591 /**
592 * Returns an iterator over the elements in this deque in proper sequence.
593 * The elements will be returned in order from first (head) to last (tail).
594 *
595 * @return an iterator over the elements in this deque in proper sequence
596 */
597 Iterator<E> iterator();
598
599 // *** Stack methods ***
600
601 /**
602 * Pushes an element onto the stack represented by this deque. In other
603 * words, inserts the element at the front of this deque unless it would
604 * violate capacity restrictions.
605 *
606 * <p>This method is equivalent to {@link #addFirst(Object) addFirst}.
607 *
608 * @throws IllegalStateException {@inheritDoc}
609 * @throws ClassCastException {@inheritDoc}
610 * @throws NullPointerException if the specified element is null
611 * @throws IllegalArgumentException {@inheritDoc}
612 */
613 void push(E e);
614 }