1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.util.AbstractQueue; |
10 |
import java.util.Collection; |
11 |
import java.util.Iterator; |
12 |
import java.util.NoSuchElementException; |
13 |
import java.util.Spliterator; |
14 |
import java.util.Spliterators; |
15 |
import java.util.concurrent.locks.Condition; |
16 |
import java.util.concurrent.locks.ReentrantLock; |
17 |
import java.util.function.Consumer; |
18 |
|
19 |
/** |
20 |
* An optionally-bounded {@linkplain BlockingDeque blocking deque} based on |
21 |
* linked nodes. |
22 |
* |
23 |
* <p>The optional capacity bound constructor argument serves as a |
24 |
* way to prevent excessive expansion. The capacity, if unspecified, |
25 |
* is equal to {@link Integer#MAX_VALUE}. Linked nodes are |
26 |
* dynamically created upon each insertion unless this would bring the |
27 |
* deque above capacity. |
28 |
* |
29 |
* <p>Most operations run in constant time (ignoring time spent |
30 |
* blocking). Exceptions include {@link #remove(Object) remove}, |
31 |
* {@link #removeFirstOccurrence removeFirstOccurrence}, {@link |
32 |
* #removeLastOccurrence removeLastOccurrence}, {@link #contains |
33 |
* contains}, {@link #iterator iterator.remove()}, and the bulk |
34 |
* operations, all of which run in linear time. |
35 |
* |
36 |
* <p>This class and its iterator implement all of the |
37 |
* <em>optional</em> methods of the {@link Collection} and {@link |
38 |
* Iterator} interfaces. |
39 |
* |
40 |
* <p>This class is a member of the |
41 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
42 |
* Java Collections Framework</a>. |
43 |
* |
44 |
* @since 1.6 |
45 |
* @author Doug Lea |
46 |
* @param <E> the type of elements held in this deque |
47 |
*/ |
48 |
public class LinkedBlockingDeque<E> |
49 |
extends AbstractQueue<E> |
50 |
implements BlockingDeque<E>, java.io.Serializable { |
51 |
|
52 |
/* |
53 |
* Implemented as a simple doubly-linked list protected by a |
54 |
* single lock and using conditions to manage blocking. |
55 |
* |
56 |
* To implement weakly consistent iterators, it appears we need to |
57 |
* keep all Nodes GC-reachable from a predecessor dequeued Node. |
58 |
* That would cause two problems: |
59 |
* - allow a rogue Iterator to cause unbounded memory retention |
60 |
* - cause cross-generational linking of old Nodes to new Nodes if |
61 |
* a Node was tenured while live, which generational GCs have a |
62 |
* hard time dealing with, causing repeated major collections. |
63 |
* However, only non-deleted Nodes need to be reachable from |
64 |
* dequeued Nodes, and reachability does not necessarily have to |
65 |
* be of the kind understood by the GC. We use the trick of |
66 |
* linking a Node that has just been dequeued to itself. Such a |
67 |
* self-link implicitly means to jump to "first" (for next links) |
68 |
* or "last" (for prev links). |
69 |
*/ |
70 |
|
71 |
/* |
72 |
* We have "diamond" multiple interface/abstract class inheritance |
73 |
* here, and that introduces ambiguities. Often we want the |
74 |
* BlockingDeque javadoc combined with the AbstractQueue |
75 |
* implementation, so a lot of method specs are duplicated here. |
76 |
*/ |
77 |
|
78 |
private static final long serialVersionUID = -387911632671998426L; |
79 |
|
80 |
/** Doubly-linked list node class */ |
81 |
static final class Node<E> { |
82 |
/** |
83 |
* The item, or null if this node has been removed. |
84 |
*/ |
85 |
E item; |
86 |
|
87 |
/** |
88 |
* One of: |
89 |
* - the real predecessor Node |
90 |
* - this Node, meaning the predecessor is tail |
91 |
* - null, meaning there is no predecessor |
92 |
*/ |
93 |
Node<E> prev; |
94 |
|
95 |
/** |
96 |
* One of: |
97 |
* - the real successor Node |
98 |
* - this Node, meaning the successor is head |
99 |
* - null, meaning there is no successor |
100 |
*/ |
101 |
Node<E> next; |
102 |
|
103 |
Node(E x) { |
104 |
item = x; |
105 |
} |
106 |
} |
107 |
|
108 |
/** |
109 |
* Pointer to first node. |
110 |
* Invariant: (first == null && last == null) || |
111 |
* (first.prev == null && first.item != null) |
112 |
*/ |
113 |
transient Node<E> first; |
114 |
|
115 |
/** |
116 |
* Pointer to last node. |
117 |
* Invariant: (first == null && last == null) || |
118 |
* (last.next == null && last.item != null) |
119 |
*/ |
120 |
transient Node<E> last; |
121 |
|
122 |
/** Number of items in the deque */ |
123 |
private transient int count; |
124 |
|
125 |
/** Maximum number of items in the deque */ |
126 |
private final int capacity; |
127 |
|
128 |
/** Main lock guarding all access */ |
129 |
final ReentrantLock lock = new ReentrantLock(); |
130 |
|
131 |
/** Condition for waiting takes */ |
132 |
private final Condition notEmpty = lock.newCondition(); |
133 |
|
134 |
/** Condition for waiting puts */ |
135 |
private final Condition notFull = lock.newCondition(); |
136 |
|
137 |
/** |
138 |
* Creates a {@code LinkedBlockingDeque} with a capacity of |
139 |
* {@link Integer#MAX_VALUE}. |
140 |
*/ |
141 |
public LinkedBlockingDeque() { |
142 |
this(Integer.MAX_VALUE); |
143 |
} |
144 |
|
145 |
/** |
146 |
* Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity. |
147 |
* |
148 |
* @param capacity the capacity of this deque |
149 |
* @throws IllegalArgumentException if {@code capacity} is less than 1 |
150 |
*/ |
151 |
public LinkedBlockingDeque(int capacity) { |
152 |
if (capacity <= 0) throw new IllegalArgumentException(); |
153 |
this.capacity = capacity; |
154 |
} |
155 |
|
156 |
/** |
157 |
* Creates a {@code LinkedBlockingDeque} with a capacity of |
158 |
* {@link Integer#MAX_VALUE}, initially containing the elements of |
159 |
* the given collection, added in traversal order of the |
160 |
* collection's iterator. |
161 |
* |
162 |
* @param c the collection of elements to initially contain |
163 |
* @throws NullPointerException if the specified collection or any |
164 |
* of its elements are null |
165 |
*/ |
166 |
public LinkedBlockingDeque(Collection<? extends E> c) { |
167 |
this(Integer.MAX_VALUE); |
168 |
final ReentrantLock lock = this.lock; |
169 |
lock.lock(); // Never contended, but necessary for visibility |
170 |
try { |
171 |
for (E e : c) { |
172 |
if (e == null) |
173 |
throw new NullPointerException(); |
174 |
if (!linkLast(new Node<E>(e))) |
175 |
throw new IllegalStateException("Deque full"); |
176 |
} |
177 |
} finally { |
178 |
lock.unlock(); |
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
// Basic linking and unlinking operations, called only while holding lock |
184 |
|
185 |
/** |
186 |
* Links node as first element, or returns false if full. |
187 |
*/ |
188 |
private boolean linkFirst(Node<E> node) { |
189 |
// assert lock.isHeldByCurrentThread(); |
190 |
if (count >= capacity) |
191 |
return false; |
192 |
Node<E> f = first; |
193 |
node.next = f; |
194 |
first = node; |
195 |
if (last == null) |
196 |
last = node; |
197 |
else |
198 |
f.prev = node; |
199 |
++count; |
200 |
notEmpty.signal(); |
201 |
return true; |
202 |
} |
203 |
|
204 |
/** |
205 |
* Links node as last element, or returns false if full. |
206 |
*/ |
207 |
private boolean linkLast(Node<E> node) { |
208 |
// assert lock.isHeldByCurrentThread(); |
209 |
if (count >= capacity) |
210 |
return false; |
211 |
Node<E> l = last; |
212 |
node.prev = l; |
213 |
last = node; |
214 |
if (first == null) |
215 |
first = node; |
216 |
else |
217 |
l.next = node; |
218 |
++count; |
219 |
notEmpty.signal(); |
220 |
return true; |
221 |
} |
222 |
|
223 |
/** |
224 |
* Removes and returns first element, or null if empty. |
225 |
*/ |
226 |
private E unlinkFirst() { |
227 |
// assert lock.isHeldByCurrentThread(); |
228 |
Node<E> f = first; |
229 |
if (f == null) |
230 |
return null; |
231 |
Node<E> n = f.next; |
232 |
E item = f.item; |
233 |
f.item = null; |
234 |
f.next = f; // help GC |
235 |
first = n; |
236 |
if (n == null) |
237 |
last = null; |
238 |
else |
239 |
n.prev = null; |
240 |
--count; |
241 |
notFull.signal(); |
242 |
return item; |
243 |
} |
244 |
|
245 |
/** |
246 |
* Removes and returns last element, or null if empty. |
247 |
*/ |
248 |
private E unlinkLast() { |
249 |
// assert lock.isHeldByCurrentThread(); |
250 |
Node<E> l = last; |
251 |
if (l == null) |
252 |
return null; |
253 |
Node<E> p = l.prev; |
254 |
E item = l.item; |
255 |
l.item = null; |
256 |
l.prev = l; // help GC |
257 |
last = p; |
258 |
if (p == null) |
259 |
first = null; |
260 |
else |
261 |
p.next = null; |
262 |
--count; |
263 |
notFull.signal(); |
264 |
return item; |
265 |
} |
266 |
|
267 |
/** |
268 |
* Unlinks x. |
269 |
*/ |
270 |
void unlink(Node<E> x) { |
271 |
// assert lock.isHeldByCurrentThread(); |
272 |
Node<E> p = x.prev; |
273 |
Node<E> n = x.next; |
274 |
if (p == null) { |
275 |
unlinkFirst(); |
276 |
} else if (n == null) { |
277 |
unlinkLast(); |
278 |
} else { |
279 |
p.next = n; |
280 |
n.prev = p; |
281 |
x.item = null; |
282 |
// Don't mess with x's links. They may still be in use by |
283 |
// an iterator. |
284 |
--count; |
285 |
notFull.signal(); |
286 |
} |
287 |
} |
288 |
|
289 |
// BlockingDeque methods |
290 |
|
291 |
/** |
292 |
* @throws IllegalStateException if this deque is full |
293 |
* @throws NullPointerException {@inheritDoc} |
294 |
*/ |
295 |
public void addFirst(E e) { |
296 |
if (!offerFirst(e)) |
297 |
throw new IllegalStateException("Deque full"); |
298 |
} |
299 |
|
300 |
/** |
301 |
* @throws IllegalStateException if this deque is full |
302 |
* @throws NullPointerException {@inheritDoc} |
303 |
*/ |
304 |
public void addLast(E e) { |
305 |
if (!offerLast(e)) |
306 |
throw new IllegalStateException("Deque full"); |
307 |
} |
308 |
|
309 |
/** |
310 |
* @throws NullPointerException {@inheritDoc} |
311 |
*/ |
312 |
public boolean offerFirst(E e) { |
313 |
if (e == null) throw new NullPointerException(); |
314 |
Node<E> node = new Node<E>(e); |
315 |
final ReentrantLock lock = this.lock; |
316 |
lock.lock(); |
317 |
try { |
318 |
return linkFirst(node); |
319 |
} finally { |
320 |
lock.unlock(); |
321 |
} |
322 |
} |
323 |
|
324 |
/** |
325 |
* @throws NullPointerException {@inheritDoc} |
326 |
*/ |
327 |
public boolean offerLast(E e) { |
328 |
if (e == null) throw new NullPointerException(); |
329 |
Node<E> node = new Node<E>(e); |
330 |
final ReentrantLock lock = this.lock; |
331 |
lock.lock(); |
332 |
try { |
333 |
return linkLast(node); |
334 |
} finally { |
335 |
lock.unlock(); |
336 |
} |
337 |
} |
338 |
|
339 |
/** |
340 |
* @throws NullPointerException {@inheritDoc} |
341 |
* @throws InterruptedException {@inheritDoc} |
342 |
*/ |
343 |
public void putFirst(E e) throws InterruptedException { |
344 |
if (e == null) throw new NullPointerException(); |
345 |
Node<E> node = new Node<E>(e); |
346 |
final ReentrantLock lock = this.lock; |
347 |
lock.lock(); |
348 |
try { |
349 |
while (!linkFirst(node)) |
350 |
notFull.await(); |
351 |
} finally { |
352 |
lock.unlock(); |
353 |
} |
354 |
} |
355 |
|
356 |
/** |
357 |
* @throws NullPointerException {@inheritDoc} |
358 |
* @throws InterruptedException {@inheritDoc} |
359 |
*/ |
360 |
public void putLast(E e) throws InterruptedException { |
361 |
if (e == null) throw new NullPointerException(); |
362 |
Node<E> node = new Node<E>(e); |
363 |
final ReentrantLock lock = this.lock; |
364 |
lock.lock(); |
365 |
try { |
366 |
while (!linkLast(node)) |
367 |
notFull.await(); |
368 |
} finally { |
369 |
lock.unlock(); |
370 |
} |
371 |
} |
372 |
|
373 |
/** |
374 |
* @throws NullPointerException {@inheritDoc} |
375 |
* @throws InterruptedException {@inheritDoc} |
376 |
*/ |
377 |
public boolean offerFirst(E e, long timeout, TimeUnit unit) |
378 |
throws InterruptedException { |
379 |
if (e == null) throw new NullPointerException(); |
380 |
Node<E> node = new Node<E>(e); |
381 |
long nanos = unit.toNanos(timeout); |
382 |
final ReentrantLock lock = this.lock; |
383 |
lock.lockInterruptibly(); |
384 |
try { |
385 |
while (!linkFirst(node)) { |
386 |
if (nanos <= 0L) |
387 |
return false; |
388 |
nanos = notFull.awaitNanos(nanos); |
389 |
} |
390 |
return true; |
391 |
} finally { |
392 |
lock.unlock(); |
393 |
} |
394 |
} |
395 |
|
396 |
/** |
397 |
* @throws NullPointerException {@inheritDoc} |
398 |
* @throws InterruptedException {@inheritDoc} |
399 |
*/ |
400 |
public boolean offerLast(E e, long timeout, TimeUnit unit) |
401 |
throws InterruptedException { |
402 |
if (e == null) throw new NullPointerException(); |
403 |
Node<E> node = new Node<E>(e); |
404 |
long nanos = unit.toNanos(timeout); |
405 |
final ReentrantLock lock = this.lock; |
406 |
lock.lockInterruptibly(); |
407 |
try { |
408 |
while (!linkLast(node)) { |
409 |
if (nanos <= 0L) |
410 |
return false; |
411 |
nanos = notFull.awaitNanos(nanos); |
412 |
} |
413 |
return true; |
414 |
} finally { |
415 |
lock.unlock(); |
416 |
} |
417 |
} |
418 |
|
419 |
/** |
420 |
* @throws NoSuchElementException {@inheritDoc} |
421 |
*/ |
422 |
public E removeFirst() { |
423 |
E x = pollFirst(); |
424 |
if (x == null) throw new NoSuchElementException(); |
425 |
return x; |
426 |
} |
427 |
|
428 |
/** |
429 |
* @throws NoSuchElementException {@inheritDoc} |
430 |
*/ |
431 |
public E removeLast() { |
432 |
E x = pollLast(); |
433 |
if (x == null) throw new NoSuchElementException(); |
434 |
return x; |
435 |
} |
436 |
|
437 |
public E pollFirst() { |
438 |
final ReentrantLock lock = this.lock; |
439 |
lock.lock(); |
440 |
try { |
441 |
return unlinkFirst(); |
442 |
} finally { |
443 |
lock.unlock(); |
444 |
} |
445 |
} |
446 |
|
447 |
public E pollLast() { |
448 |
final ReentrantLock lock = this.lock; |
449 |
lock.lock(); |
450 |
try { |
451 |
return unlinkLast(); |
452 |
} finally { |
453 |
lock.unlock(); |
454 |
} |
455 |
} |
456 |
|
457 |
public E takeFirst() throws InterruptedException { |
458 |
final ReentrantLock lock = this.lock; |
459 |
lock.lock(); |
460 |
try { |
461 |
E x; |
462 |
while ( (x = unlinkFirst()) == null) |
463 |
notEmpty.await(); |
464 |
return x; |
465 |
} finally { |
466 |
lock.unlock(); |
467 |
} |
468 |
} |
469 |
|
470 |
public E takeLast() throws InterruptedException { |
471 |
final ReentrantLock lock = this.lock; |
472 |
lock.lock(); |
473 |
try { |
474 |
E x; |
475 |
while ( (x = unlinkLast()) == null) |
476 |
notEmpty.await(); |
477 |
return x; |
478 |
} finally { |
479 |
lock.unlock(); |
480 |
} |
481 |
} |
482 |
|
483 |
public E pollFirst(long timeout, TimeUnit unit) |
484 |
throws InterruptedException { |
485 |
long nanos = unit.toNanos(timeout); |
486 |
final ReentrantLock lock = this.lock; |
487 |
lock.lockInterruptibly(); |
488 |
try { |
489 |
E x; |
490 |
while ( (x = unlinkFirst()) == null) { |
491 |
if (nanos <= 0L) |
492 |
return null; |
493 |
nanos = notEmpty.awaitNanos(nanos); |
494 |
} |
495 |
return x; |
496 |
} finally { |
497 |
lock.unlock(); |
498 |
} |
499 |
} |
500 |
|
501 |
public E pollLast(long timeout, TimeUnit unit) |
502 |
throws InterruptedException { |
503 |
long nanos = unit.toNanos(timeout); |
504 |
final ReentrantLock lock = this.lock; |
505 |
lock.lockInterruptibly(); |
506 |
try { |
507 |
E x; |
508 |
while ( (x = unlinkLast()) == null) { |
509 |
if (nanos <= 0L) |
510 |
return null; |
511 |
nanos = notEmpty.awaitNanos(nanos); |
512 |
} |
513 |
return x; |
514 |
} finally { |
515 |
lock.unlock(); |
516 |
} |
517 |
} |
518 |
|
519 |
/** |
520 |
* @throws NoSuchElementException {@inheritDoc} |
521 |
*/ |
522 |
public E getFirst() { |
523 |
E x = peekFirst(); |
524 |
if (x == null) throw new NoSuchElementException(); |
525 |
return x; |
526 |
} |
527 |
|
528 |
/** |
529 |
* @throws NoSuchElementException {@inheritDoc} |
530 |
*/ |
531 |
public E getLast() { |
532 |
E x = peekLast(); |
533 |
if (x == null) throw new NoSuchElementException(); |
534 |
return x; |
535 |
} |
536 |
|
537 |
public E peekFirst() { |
538 |
final ReentrantLock lock = this.lock; |
539 |
lock.lock(); |
540 |
try { |
541 |
return (first == null) ? null : first.item; |
542 |
} finally { |
543 |
lock.unlock(); |
544 |
} |
545 |
} |
546 |
|
547 |
public E peekLast() { |
548 |
final ReentrantLock lock = this.lock; |
549 |
lock.lock(); |
550 |
try { |
551 |
return (last == null) ? null : last.item; |
552 |
} finally { |
553 |
lock.unlock(); |
554 |
} |
555 |
} |
556 |
|
557 |
public boolean removeFirstOccurrence(Object o) { |
558 |
if (o == null) return false; |
559 |
final ReentrantLock lock = this.lock; |
560 |
lock.lock(); |
561 |
try { |
562 |
for (Node<E> p = first; p != null; p = p.next) { |
563 |
if (o.equals(p.item)) { |
564 |
unlink(p); |
565 |
return true; |
566 |
} |
567 |
} |
568 |
return false; |
569 |
} finally { |
570 |
lock.unlock(); |
571 |
} |
572 |
} |
573 |
|
574 |
public boolean removeLastOccurrence(Object o) { |
575 |
if (o == null) return false; |
576 |
final ReentrantLock lock = this.lock; |
577 |
lock.lock(); |
578 |
try { |
579 |
for (Node<E> p = last; p != null; p = p.prev) { |
580 |
if (o.equals(p.item)) { |
581 |
unlink(p); |
582 |
return true; |
583 |
} |
584 |
} |
585 |
return false; |
586 |
} finally { |
587 |
lock.unlock(); |
588 |
} |
589 |
} |
590 |
|
591 |
// BlockingQueue methods |
592 |
|
593 |
/** |
594 |
* Inserts the specified element at the end of this deque unless it would |
595 |
* violate capacity restrictions. When using a capacity-restricted deque, |
596 |
* it is generally preferable to use method {@link #offer(Object) offer}. |
597 |
* |
598 |
* <p>This method is equivalent to {@link #addLast}. |
599 |
* |
600 |
* @throws IllegalStateException if this deque is full |
601 |
* @throws NullPointerException if the specified element is null |
602 |
*/ |
603 |
public boolean add(E e) { |
604 |
addLast(e); |
605 |
return true; |
606 |
} |
607 |
|
608 |
/** |
609 |
* @throws NullPointerException if the specified element is null |
610 |
*/ |
611 |
public boolean offer(E e) { |
612 |
return offerLast(e); |
613 |
} |
614 |
|
615 |
/** |
616 |
* @throws NullPointerException {@inheritDoc} |
617 |
* @throws InterruptedException {@inheritDoc} |
618 |
*/ |
619 |
public void put(E e) throws InterruptedException { |
620 |
putLast(e); |
621 |
} |
622 |
|
623 |
/** |
624 |
* @throws NullPointerException {@inheritDoc} |
625 |
* @throws InterruptedException {@inheritDoc} |
626 |
*/ |
627 |
public boolean offer(E e, long timeout, TimeUnit unit) |
628 |
throws InterruptedException { |
629 |
return offerLast(e, timeout, unit); |
630 |
} |
631 |
|
632 |
/** |
633 |
* Retrieves and removes the head of the queue represented by this deque. |
634 |
* This method differs from {@link #poll poll} only in that it throws an |
635 |
* exception if this deque is empty. |
636 |
* |
637 |
* <p>This method is equivalent to {@link #removeFirst() removeFirst}. |
638 |
* |
639 |
* @return the head of the queue represented by this deque |
640 |
* @throws NoSuchElementException if this deque is empty |
641 |
*/ |
642 |
public E remove() { |
643 |
return removeFirst(); |
644 |
} |
645 |
|
646 |
public E poll() { |
647 |
return pollFirst(); |
648 |
} |
649 |
|
650 |
public E take() throws InterruptedException { |
651 |
return takeFirst(); |
652 |
} |
653 |
|
654 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
655 |
return pollFirst(timeout, unit); |
656 |
} |
657 |
|
658 |
/** |
659 |
* Retrieves, but does not remove, the head of the queue represented by |
660 |
* this deque. This method differs from {@link #peek peek} only in that |
661 |
* it throws an exception if this deque is empty. |
662 |
* |
663 |
* <p>This method is equivalent to {@link #getFirst() getFirst}. |
664 |
* |
665 |
* @return the head of the queue represented by this deque |
666 |
* @throws NoSuchElementException if this deque is empty |
667 |
*/ |
668 |
public E element() { |
669 |
return getFirst(); |
670 |
} |
671 |
|
672 |
public E peek() { |
673 |
return peekFirst(); |
674 |
} |
675 |
|
676 |
/** |
677 |
* Returns the number of additional elements that this deque can ideally |
678 |
* (in the absence of memory or resource constraints) accept without |
679 |
* blocking. This is always equal to the initial capacity of this deque |
680 |
* less the current {@code size} of this deque. |
681 |
* |
682 |
* <p>Note that you <em>cannot</em> always tell if an attempt to insert |
683 |
* an element will succeed by inspecting {@code remainingCapacity} |
684 |
* because it may be the case that another thread is about to |
685 |
* insert or remove an element. |
686 |
*/ |
687 |
public int remainingCapacity() { |
688 |
final ReentrantLock lock = this.lock; |
689 |
lock.lock(); |
690 |
try { |
691 |
return capacity - count; |
692 |
} finally { |
693 |
lock.unlock(); |
694 |
} |
695 |
} |
696 |
|
697 |
/** |
698 |
* @throws UnsupportedOperationException {@inheritDoc} |
699 |
* @throws ClassCastException {@inheritDoc} |
700 |
* @throws NullPointerException {@inheritDoc} |
701 |
* @throws IllegalArgumentException {@inheritDoc} |
702 |
*/ |
703 |
public int drainTo(Collection<? super E> c) { |
704 |
return drainTo(c, Integer.MAX_VALUE); |
705 |
} |
706 |
|
707 |
/** |
708 |
* @throws UnsupportedOperationException {@inheritDoc} |
709 |
* @throws ClassCastException {@inheritDoc} |
710 |
* @throws NullPointerException {@inheritDoc} |
711 |
* @throws IllegalArgumentException {@inheritDoc} |
712 |
*/ |
713 |
public int drainTo(Collection<? super E> c, int maxElements) { |
714 |
if (c == null) |
715 |
throw new NullPointerException(); |
716 |
if (c == this) |
717 |
throw new IllegalArgumentException(); |
718 |
if (maxElements <= 0) |
719 |
return 0; |
720 |
final ReentrantLock lock = this.lock; |
721 |
lock.lock(); |
722 |
try { |
723 |
int n = Math.min(maxElements, count); |
724 |
for (int i = 0; i < n; i++) { |
725 |
c.add(first.item); // In this order, in case add() throws. |
726 |
unlinkFirst(); |
727 |
} |
728 |
return n; |
729 |
} finally { |
730 |
lock.unlock(); |
731 |
} |
732 |
} |
733 |
|
734 |
// Stack methods |
735 |
|
736 |
/** |
737 |
* @throws IllegalStateException if this deque is full |
738 |
* @throws NullPointerException {@inheritDoc} |
739 |
*/ |
740 |
public void push(E e) { |
741 |
addFirst(e); |
742 |
} |
743 |
|
744 |
/** |
745 |
* @throws NoSuchElementException {@inheritDoc} |
746 |
*/ |
747 |
public E pop() { |
748 |
return removeFirst(); |
749 |
} |
750 |
|
751 |
// Collection methods |
752 |
|
753 |
/** |
754 |
* Removes the first occurrence of the specified element from this deque. |
755 |
* If the deque does not contain the element, it is unchanged. |
756 |
* More formally, removes the first element {@code e} such that |
757 |
* {@code o.equals(e)} (if such an element exists). |
758 |
* Returns {@code true} if this deque contained the specified element |
759 |
* (or equivalently, if this deque changed as a result of the call). |
760 |
* |
761 |
* <p>This method is equivalent to |
762 |
* {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. |
763 |
* |
764 |
* @param o element to be removed from this deque, if present |
765 |
* @return {@code true} if this deque changed as a result of the call |
766 |
*/ |
767 |
public boolean remove(Object o) { |
768 |
return removeFirstOccurrence(o); |
769 |
} |
770 |
|
771 |
/** |
772 |
* Returns the number of elements in this deque. |
773 |
* |
774 |
* @return the number of elements in this deque |
775 |
*/ |
776 |
public int size() { |
777 |
final ReentrantLock lock = this.lock; |
778 |
lock.lock(); |
779 |
try { |
780 |
return count; |
781 |
} finally { |
782 |
lock.unlock(); |
783 |
} |
784 |
} |
785 |
|
786 |
/** |
787 |
* Returns {@code true} if this deque contains the specified element. |
788 |
* More formally, returns {@code true} if and only if this deque contains |
789 |
* at least one element {@code e} such that {@code o.equals(e)}. |
790 |
* |
791 |
* @param o object to be checked for containment in this deque |
792 |
* @return {@code true} if this deque contains the specified element |
793 |
*/ |
794 |
public boolean contains(Object o) { |
795 |
if (o == null) return false; |
796 |
final ReentrantLock lock = this.lock; |
797 |
lock.lock(); |
798 |
try { |
799 |
for (Node<E> p = first; p != null; p = p.next) |
800 |
if (o.equals(p.item)) |
801 |
return true; |
802 |
return false; |
803 |
} finally { |
804 |
lock.unlock(); |
805 |
} |
806 |
} |
807 |
|
808 |
/* |
809 |
* TODO: Add support for more efficient bulk operations. |
810 |
* |
811 |
* We don't want to acquire the lock for every iteration, but we |
812 |
* also want other threads a chance to interact with the |
813 |
* collection, especially when count is close to capacity. |
814 |
*/ |
815 |
|
816 |
// /** |
817 |
// * Adds all of the elements in the specified collection to this |
818 |
// * queue. Attempts to addAll of a queue to itself result in |
819 |
// * {@code IllegalArgumentException}. Further, the behavior of |
820 |
// * this operation is undefined if the specified collection is |
821 |
// * modified while the operation is in progress. |
822 |
// * |
823 |
// * @param c collection containing elements to be added to this queue |
824 |
// * @return {@code true} if this queue changed as a result of the call |
825 |
// * @throws ClassCastException {@inheritDoc} |
826 |
// * @throws NullPointerException {@inheritDoc} |
827 |
// * @throws IllegalArgumentException {@inheritDoc} |
828 |
// * @throws IllegalStateException if this deque is full |
829 |
// * @see #add(Object) |
830 |
// */ |
831 |
// public boolean addAll(Collection<? extends E> c) { |
832 |
// if (c == null) |
833 |
// throw new NullPointerException(); |
834 |
// if (c == this) |
835 |
// throw new IllegalArgumentException(); |
836 |
// final ReentrantLock lock = this.lock; |
837 |
// lock.lock(); |
838 |
// try { |
839 |
// boolean modified = false; |
840 |
// for (E e : c) |
841 |
// if (linkLast(e)) |
842 |
// modified = true; |
843 |
// return modified; |
844 |
// } finally { |
845 |
// lock.unlock(); |
846 |
// } |
847 |
// } |
848 |
|
849 |
/** |
850 |
* Returns an array containing all of the elements in this deque, in |
851 |
* proper sequence (from first to last element). |
852 |
* |
853 |
* <p>The returned array will be "safe" in that no references to it are |
854 |
* maintained by this deque. (In other words, this method must allocate |
855 |
* a new array). The caller is thus free to modify the returned array. |
856 |
* |
857 |
* <p>This method acts as bridge between array-based and collection-based |
858 |
* APIs. |
859 |
* |
860 |
* @return an array containing all of the elements in this deque |
861 |
*/ |
862 |
@SuppressWarnings("unchecked") |
863 |
public Object[] toArray() { |
864 |
final ReentrantLock lock = this.lock; |
865 |
lock.lock(); |
866 |
try { |
867 |
Object[] a = new Object[count]; |
868 |
int k = 0; |
869 |
for (Node<E> p = first; p != null; p = p.next) |
870 |
a[k++] = p.item; |
871 |
return a; |
872 |
} finally { |
873 |
lock.unlock(); |
874 |
} |
875 |
} |
876 |
|
877 |
/** |
878 |
* Returns an array containing all of the elements in this deque, in |
879 |
* proper sequence; the runtime type of the returned array is that of |
880 |
* the specified array. If the deque fits in the specified array, it |
881 |
* is returned therein. Otherwise, a new array is allocated with the |
882 |
* runtime type of the specified array and the size of this deque. |
883 |
* |
884 |
* <p>If this deque fits in the specified array with room to spare |
885 |
* (i.e., the array has more elements than this deque), the element in |
886 |
* the array immediately following the end of the deque is set to |
887 |
* {@code null}. |
888 |
* |
889 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
890 |
* array-based and collection-based APIs. Further, this method allows |
891 |
* precise control over the runtime type of the output array, and may, |
892 |
* under certain circumstances, be used to save allocation costs. |
893 |
* |
894 |
* <p>Suppose {@code x} is a deque known to contain only strings. |
895 |
* The following code can be used to dump the deque into a newly |
896 |
* allocated array of {@code String}: |
897 |
* |
898 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
899 |
* |
900 |
* Note that {@code toArray(new Object[0])} is identical in function to |
901 |
* {@code toArray()}. |
902 |
* |
903 |
* @param a the array into which the elements of the deque are to |
904 |
* be stored, if it is big enough; otherwise, a new array of the |
905 |
* same runtime type is allocated for this purpose |
906 |
* @return an array containing all of the elements in this deque |
907 |
* @throws ArrayStoreException if the runtime type of the specified array |
908 |
* is not a supertype of the runtime type of every element in |
909 |
* this deque |
910 |
* @throws NullPointerException if the specified array is null |
911 |
*/ |
912 |
@SuppressWarnings("unchecked") |
913 |
public <T> T[] toArray(T[] a) { |
914 |
final ReentrantLock lock = this.lock; |
915 |
lock.lock(); |
916 |
try { |
917 |
if (a.length < count) |
918 |
a = (T[])java.lang.reflect.Array.newInstance |
919 |
(a.getClass().getComponentType(), count); |
920 |
|
921 |
int k = 0; |
922 |
for (Node<E> p = first; p != null; p = p.next) |
923 |
a[k++] = (T)p.item; |
924 |
if (a.length > k) |
925 |
a[k] = null; |
926 |
return a; |
927 |
} finally { |
928 |
lock.unlock(); |
929 |
} |
930 |
} |
931 |
|
932 |
public String toString() { |
933 |
return Helpers.collectionToString(this); |
934 |
} |
935 |
|
936 |
/** |
937 |
* Atomically removes all of the elements from this deque. |
938 |
* The deque will be empty after this call returns. |
939 |
*/ |
940 |
public void clear() { |
941 |
final ReentrantLock lock = this.lock; |
942 |
lock.lock(); |
943 |
try { |
944 |
for (Node<E> f = first; f != null; ) { |
945 |
f.item = null; |
946 |
Node<E> n = f.next; |
947 |
f.prev = null; |
948 |
f.next = null; |
949 |
f = n; |
950 |
} |
951 |
first = last = null; |
952 |
count = 0; |
953 |
notFull.signalAll(); |
954 |
} finally { |
955 |
lock.unlock(); |
956 |
} |
957 |
} |
958 |
|
959 |
/** |
960 |
* Returns an iterator over the elements in this deque in proper sequence. |
961 |
* The elements will be returned in order from first (head) to last (tail). |
962 |
* |
963 |
* <p>The returned iterator is |
964 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
965 |
* |
966 |
* @return an iterator over the elements in this deque in proper sequence |
967 |
*/ |
968 |
public Iterator<E> iterator() { |
969 |
return new Itr(); |
970 |
} |
971 |
|
972 |
/** |
973 |
* Returns an iterator over the elements in this deque in reverse |
974 |
* sequential order. The elements will be returned in order from |
975 |
* last (tail) to first (head). |
976 |
* |
977 |
* <p>The returned iterator is |
978 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
979 |
* |
980 |
* @return an iterator over the elements in this deque in reverse order |
981 |
*/ |
982 |
public Iterator<E> descendingIterator() { |
983 |
return new DescendingItr(); |
984 |
} |
985 |
|
986 |
/** |
987 |
* Base class for LinkedBlockingDeque iterators. |
988 |
*/ |
989 |
private abstract class AbstractItr implements Iterator<E> { |
990 |
/** |
991 |
* The next node to return in next(). |
992 |
*/ |
993 |
Node<E> next; |
994 |
|
995 |
/** |
996 |
* nextItem holds on to item fields because once we claim that |
997 |
* an element exists in hasNext(), we must return item read |
998 |
* under lock (in advance()) even if it was in the process of |
999 |
* being removed when hasNext() was called. |
1000 |
*/ |
1001 |
E nextItem; |
1002 |
|
1003 |
/** |
1004 |
* Node returned by most recent call to next. Needed by remove. |
1005 |
* Reset to null if this element is deleted by a call to remove. |
1006 |
*/ |
1007 |
private Node<E> lastRet; |
1008 |
|
1009 |
abstract Node<E> firstNode(); |
1010 |
abstract Node<E> nextNode(Node<E> n); |
1011 |
|
1012 |
AbstractItr() { |
1013 |
// set to initial position |
1014 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
1015 |
lock.lock(); |
1016 |
try { |
1017 |
next = firstNode(); |
1018 |
nextItem = (next == null) ? null : next.item; |
1019 |
} finally { |
1020 |
lock.unlock(); |
1021 |
} |
1022 |
} |
1023 |
|
1024 |
/** |
1025 |
* Returns the successor node of the given non-null, but |
1026 |
* possibly previously deleted, node. |
1027 |
*/ |
1028 |
private Node<E> succ(Node<E> n) { |
1029 |
// Chains of deleted nodes ending in null or self-links |
1030 |
// are possible if multiple interior nodes are removed. |
1031 |
for (;;) { |
1032 |
Node<E> s = nextNode(n); |
1033 |
if (s == null) |
1034 |
return null; |
1035 |
else if (s.item != null) |
1036 |
return s; |
1037 |
else if (s == n) |
1038 |
return firstNode(); |
1039 |
else |
1040 |
n = s; |
1041 |
} |
1042 |
} |
1043 |
|
1044 |
/** |
1045 |
* Advances next. |
1046 |
*/ |
1047 |
void advance() { |
1048 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
1049 |
lock.lock(); |
1050 |
try { |
1051 |
// assert next != null; |
1052 |
next = succ(next); |
1053 |
nextItem = (next == null) ? null : next.item; |
1054 |
} finally { |
1055 |
lock.unlock(); |
1056 |
} |
1057 |
} |
1058 |
|
1059 |
public boolean hasNext() { |
1060 |
return next != null; |
1061 |
} |
1062 |
|
1063 |
public E next() { |
1064 |
if (next == null) |
1065 |
throw new NoSuchElementException(); |
1066 |
lastRet = next; |
1067 |
E x = nextItem; |
1068 |
advance(); |
1069 |
return x; |
1070 |
} |
1071 |
|
1072 |
public void remove() { |
1073 |
Node<E> n = lastRet; |
1074 |
if (n == null) |
1075 |
throw new IllegalStateException(); |
1076 |
lastRet = null; |
1077 |
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
1078 |
lock.lock(); |
1079 |
try { |
1080 |
if (n.item != null) |
1081 |
unlink(n); |
1082 |
} finally { |
1083 |
lock.unlock(); |
1084 |
} |
1085 |
} |
1086 |
} |
1087 |
|
1088 |
/** Forward iterator */ |
1089 |
private class Itr extends AbstractItr { |
1090 |
Node<E> firstNode() { return first; } |
1091 |
Node<E> nextNode(Node<E> n) { return n.next; } |
1092 |
} |
1093 |
|
1094 |
/** Descending iterator */ |
1095 |
private class DescendingItr extends AbstractItr { |
1096 |
Node<E> firstNode() { return last; } |
1097 |
Node<E> nextNode(Node<E> n) { return n.prev; } |
1098 |
} |
1099 |
|
1100 |
/** A customized variant of Spliterators.IteratorSpliterator */ |
1101 |
static final class LBDSpliterator<E> implements Spliterator<E> { |
1102 |
static final int MAX_BATCH = 1 << 25; // max batch array size; |
1103 |
final LinkedBlockingDeque<E> queue; |
1104 |
Node<E> current; // current node; null until initialized |
1105 |
int batch; // batch size for splits |
1106 |
boolean exhausted; // true when no more nodes |
1107 |
long est; // size estimate |
1108 |
LBDSpliterator(LinkedBlockingDeque<E> queue) { |
1109 |
this.queue = queue; |
1110 |
this.est = queue.size(); |
1111 |
} |
1112 |
|
1113 |
public long estimateSize() { return est; } |
1114 |
|
1115 |
public Spliterator<E> trySplit() { |
1116 |
Node<E> h; |
1117 |
final LinkedBlockingDeque<E> q = this.queue; |
1118 |
int b = batch; |
1119 |
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1; |
1120 |
if (!exhausted && |
1121 |
(((h = current) != null && h != h.next) |
1122 |
|| (h = q.first) != null) |
1123 |
&& h.next != null) { |
1124 |
Object[] a = new Object[n]; |
1125 |
final ReentrantLock lock = q.lock; |
1126 |
int i = 0; |
1127 |
Node<E> p = current; |
1128 |
lock.lock(); |
1129 |
try { |
1130 |
if ((p != null && p != p.next) || (p = q.first) != null) { |
1131 |
do { |
1132 |
if ((a[i] = p.item) != null) |
1133 |
++i; |
1134 |
} while ((p = p.next) != null && i < n); |
1135 |
} |
1136 |
} finally { |
1137 |
lock.unlock(); |
1138 |
} |
1139 |
if ((current = p) == null) { |
1140 |
est = 0L; |
1141 |
exhausted = true; |
1142 |
} |
1143 |
else if ((est -= i) < 0L) |
1144 |
est = 0L; |
1145 |
if (i > 0) { |
1146 |
batch = i; |
1147 |
return Spliterators.spliterator |
1148 |
(a, 0, i, (Spliterator.ORDERED | |
1149 |
Spliterator.NONNULL | |
1150 |
Spliterator.CONCURRENT)); |
1151 |
} |
1152 |
} |
1153 |
return null; |
1154 |
} |
1155 |
|
1156 |
public void forEachRemaining(Consumer<? super E> action) { |
1157 |
if (action == null) throw new NullPointerException(); |
1158 |
if (!exhausted) { |
1159 |
exhausted = true; |
1160 |
Node<E> p = current; |
1161 |
current = null; |
1162 |
final LinkedBlockingDeque<E> q = this.queue; |
1163 |
final ReentrantLock lock = q.lock; |
1164 |
do { |
1165 |
E e; |
1166 |
lock.lock(); |
1167 |
try { |
1168 |
if (p == null) |
1169 |
p = q.first; |
1170 |
do { |
1171 |
if (p == null) |
1172 |
return; |
1173 |
e = p.item; |
1174 |
if (p == (p = p.next)) p = q.first; |
1175 |
} while (e == null); |
1176 |
} finally { |
1177 |
lock.unlock(); |
1178 |
} |
1179 |
action.accept(e); |
1180 |
} while (p != null); |
1181 |
} |
1182 |
} |
1183 |
|
1184 |
public boolean tryAdvance(Consumer<? super E> action) { |
1185 |
if (action == null) throw new NullPointerException(); |
1186 |
if (!exhausted) findElement: { |
1187 |
final LinkedBlockingDeque<E> q = this.queue; |
1188 |
final ReentrantLock lock = q.lock; |
1189 |
E e; |
1190 |
Node<E> p = current; |
1191 |
lock.lock(); |
1192 |
try { |
1193 |
if (p == null) |
1194 |
p = q.first; |
1195 |
do { |
1196 |
if (p == null) break findElement; |
1197 |
e = p.item; |
1198 |
if (p == (p = p.next)) p = q.first; |
1199 |
} while (e == null); |
1200 |
} finally { |
1201 |
lock.unlock(); |
1202 |
} |
1203 |
action.accept(e); |
1204 |
if ((current = p) == null) |
1205 |
exhausted = true; |
1206 |
return true; |
1207 |
} |
1208 |
current = null; |
1209 |
exhausted = true; |
1210 |
return false; |
1211 |
} |
1212 |
|
1213 |
public int characteristics() { |
1214 |
return (Spliterator.ORDERED | |
1215 |
Spliterator.NONNULL | |
1216 |
Spliterator.CONCURRENT); |
1217 |
} |
1218 |
} |
1219 |
|
1220 |
/** |
1221 |
* Returns a {@link Spliterator} over the elements in this deque. |
1222 |
* |
1223 |
* <p>The returned spliterator is |
1224 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1225 |
* |
1226 |
* <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, |
1227 |
* {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. |
1228 |
* |
1229 |
* @implNote |
1230 |
* The {@code Spliterator} implements {@code trySplit} to permit limited |
1231 |
* parallelism. |
1232 |
* |
1233 |
* @return a {@code Spliterator} over the elements in this deque |
1234 |
* @since 1.8 |
1235 |
*/ |
1236 |
public Spliterator<E> spliterator() { |
1237 |
return new LBDSpliterator<E>(this); |
1238 |
} |
1239 |
|
1240 |
/** |
1241 |
* Saves this deque to a stream (that is, serializes it). |
1242 |
* |
1243 |
* @param s the stream |
1244 |
* @throws java.io.IOException if an I/O error occurs |
1245 |
* @serialData The capacity (int), followed by elements (each an |
1246 |
* {@code Object}) in the proper order, followed by a null |
1247 |
*/ |
1248 |
private void writeObject(java.io.ObjectOutputStream s) |
1249 |
throws java.io.IOException { |
1250 |
final ReentrantLock lock = this.lock; |
1251 |
lock.lock(); |
1252 |
try { |
1253 |
// Write out capacity and any hidden stuff |
1254 |
s.defaultWriteObject(); |
1255 |
// Write out all elements in the proper order. |
1256 |
for (Node<E> p = first; p != null; p = p.next) |
1257 |
s.writeObject(p.item); |
1258 |
// Use trailing null as sentinel |
1259 |
s.writeObject(null); |
1260 |
} finally { |
1261 |
lock.unlock(); |
1262 |
} |
1263 |
} |
1264 |
|
1265 |
/** |
1266 |
* Reconstitutes this deque from a stream (that is, deserializes it). |
1267 |
* @param s the stream |
1268 |
* @throws ClassNotFoundException if the class of a serialized object |
1269 |
* could not be found |
1270 |
* @throws java.io.IOException if an I/O error occurs |
1271 |
*/ |
1272 |
private void readObject(java.io.ObjectInputStream s) |
1273 |
throws java.io.IOException, ClassNotFoundException { |
1274 |
s.defaultReadObject(); |
1275 |
count = 0; |
1276 |
first = null; |
1277 |
last = null; |
1278 |
// Read in all elements and place in queue |
1279 |
for (;;) { |
1280 |
@SuppressWarnings("unchecked") |
1281 |
E item = (E)s.readObject(); |
1282 |
if (item == null) |
1283 |
break; |
1284 |
add(item); |
1285 |
} |
1286 |
} |
1287 |
|
1288 |
} |