2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
31 * Written by Doug Lea and Martin Buchholz with assistance from members of
32 * JCP JSR-166 Expert Group and released to the public domain, as explained
33 * at http://creativecommons.org/publicdomain/zero/1.0/
36 package java.util.concurrent;
38 import java.util.AbstractCollection;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Deque;
42 import java.util.Iterator;
43 import java.util.NoSuchElementException;
44 import java.util.Queue;
47 * An unbounded concurrent {@linkplain Deque deque} based on linked nodes.
48 * Concurrent insertion, removal, and access operations execute safely
49 * across multiple threads.
50 * A {@code ConcurrentLinkedDeque} is an appropriate choice when
51 * many threads will share access to a common collection.
52 * Like most other concurrent collection implementations, this class
53 * does not permit the use of {@code null} elements.
55 * <p>Iterators are <i>weakly consistent</i>, returning elements
56 * reflecting the state of the deque at some point at or since the
57 * creation of the iterator. They do <em>not</em> throw {@link
58 * java.util.ConcurrentModificationException
59 * ConcurrentModificationException}, and may proceed concurrently with
62 * <p>Beware that, unlike in most collections, the {@code size} method
63 * is <em>NOT</em> a constant-time operation. Because of the
64 * asynchronous nature of these deques, determining the current number
65 * of elements requires a traversal of the elements, and so may report
66 * inaccurate results if this collection is modified during traversal.
67 * Additionally, the bulk operations {@code addAll},
68 * {@code removeAll}, {@code retainAll}, {@code containsAll},
69 * {@code equals}, and {@code toArray} are <em>not</em> guaranteed
70 * to be performed atomically. For example, an iterator operating
71 * concurrently with an {@code addAll} operation might view only some
72 * of the added elements.
74 * <p>This class and its iterator implement all of the <em>optional</em>
75 * methods of the {@link Deque} and {@link Iterator} interfaces.
77 * <p>Memory consistency effects: As with other concurrent collections,
78 * actions in a thread prior to placing an object into a
79 * {@code ConcurrentLinkedDeque}
80 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
81 * actions subsequent to the access or removal of that element from
82 * the {@code ConcurrentLinkedDeque} in another thread.
84 * <p>This class is a member of the
85 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
86 * Java Collections Framework</a>.
90 * @author Martin Buchholz
91 * @param <E> the type of elements held in this collection
94 public class ConcurrentLinkedDeque<E>
95 extends AbstractCollection<E>
96 implements Deque<E>, java.io.Serializable {
99 * This is an implementation of a concurrent lock-free deque
100 * supporting interior removes but not interior insertions, as
101 * required to support the entire Deque interface.
103 * We extend the techniques developed for ConcurrentLinkedQueue and
104 * LinkedTransferQueue (see the internal docs for those classes).
105 * Understanding the ConcurrentLinkedQueue implementation is a
106 * prerequisite for understanding the implementation of this class.
108 * The data structure is a symmetrical doubly-linked "GC-robust"
109 * linked list of nodes. We minimize the number of volatile writes
110 * using two techniques: advancing multiple hops with a single CAS
111 * and mixing volatile and non-volatile writes of the same memory
114 * A node contains the expected E ("item") and links to predecessor
115 * ("prev") and successor ("next") nodes:
117 * class Node<E> { volatile Node<E> prev, next; volatile E item; }
119 * A node p is considered "live" if it contains a non-null item
120 * (p.item != null). When an item is CASed to null, the item is
121 * atomically logically deleted from the collection.
123 * At any time, there is precisely one "first" node with a null
124 * prev reference that terminates any chain of prev references
125 * starting at a live node. Similarly there is precisely one
126 * "last" node terminating any chain of next references starting at
127 * a live node. The "first" and "last" nodes may or may not be live.
128 * The "first" and "last" nodes are always mutually reachable.
130 * A new element is added atomically by CASing the null prev or
131 * next reference in the first or last node to a fresh node
132 * containing the element. The element's node atomically becomes
133 * "live" at that point.
135 * A node is considered "active" if it is a live node, or the
136 * first or last node. Active nodes cannot be unlinked.
138 * A "self-link" is a next or prev reference that is the same node:
139 * p.prev == p or p.next == p
140 * Self-links are used in the node unlinking process. Active nodes
141 * never have self-links.
143 * A node p is active if and only if:
146 * (p.prev == null && p.next != p) ||
147 * (p.next == null && p.prev != p)
149 * The deque object has two node references, "head" and "tail".
150 * The head and tail are only approximations to the first and last
151 * nodes of the deque. The first node can always be found by
152 * following prev pointers from head; likewise for tail. However,
153 * it is permissible for head and tail to be referring to deleted
154 * nodes that have been unlinked and so may not be reachable from
157 * There are 3 stages of node deletion;
158 * "logical deletion", "unlinking", and "gc-unlinking".
160 * 1. "logical deletion" by CASing item to null atomically removes
161 * the element from the collection, and makes the containing node
162 * eligible for unlinking.
164 * 2. "unlinking" makes a deleted node unreachable from active
165 * nodes, and thus eventually reclaimable by GC. Unlinked nodes
166 * may remain reachable indefinitely from an iterator.
168 * Physical node unlinking is merely an optimization (albeit a
169 * critical one), and so can be performed at our convenience. At
170 * any time, the set of live nodes maintained by prev and next
171 * links are identical, that is, the live nodes found via next
172 * links from the first node is equal to the elements found via
173 * prev links from the last node. However, this is not true for
174 * nodes that have already been logically deleted - such nodes may
175 * be reachable in one direction only.
177 * 3. "gc-unlinking" takes unlinking further by making active
178 * nodes unreachable from deleted nodes, making it easier for the
179 * GC to reclaim future deleted nodes. This step makes the data
180 * structure "gc-robust", as first described in detail by Boehm
181 * (http://portal.acm.org/citation.cfm?doid=503272.503282).
183 * GC-unlinked nodes may remain reachable indefinitely from an
184 * iterator, but unlike unlinked nodes, are never reachable from
187 * Making the data structure GC-robust will eliminate the risk of
188 * unbounded memory retention with conservative GCs and is likely
189 * to improve performance with generational GCs.
191 * When a node is dequeued at either end, e.g. via poll(), we would
192 * like to break any references from the node to active nodes. We
193 * develop further the use of self-links that was very effective in
194 * other concurrent collection classes. The idea is to replace
195 * prev and next pointers with special values that are interpreted
196 * to mean off-the-list-at-one-end. These are approximations, but
197 * good enough to preserve the properties we want in our
198 * traversals, e.g. we guarantee that a traversal will never visit
199 * the same element twice, but we don't guarantee whether a
200 * traversal that runs out of elements will be able to see more
201 * elements later after enqueues at that end. Doing gc-unlinking
202 * safely is particularly tricky, since any node can be in use
203 * indefinitely (for example by an iterator). We must ensure that
204 * the nodes pointed at by head/tail never get gc-unlinked, since
205 * head/tail are needed to get "back on track" by other nodes that
206 * are gc-unlinked. gc-unlinking accounts for much of the
207 * implementation complexity.
209 * Since neither unlinking nor gc-unlinking are necessary for
210 * correctness, there are many implementation choices regarding
211 * frequency (eagerness) of these operations. Since volatile
212 * reads are likely to be much cheaper than CASes, saving CASes by
213 * unlinking multiple adjacent nodes at a time may be a win.
214 * gc-unlinking can be performed rarely and still be effective,
215 * since it is most important that long chains of deleted nodes
216 * are occasionally broken.
218 * The actual representation we use is that p.next == p means to
219 * goto the first node (which in turn is reached by following prev
220 * pointers from head), and p.next == null && p.prev == p means
221 * that the iteration is at an end and that p is a (static final)
222 * dummy node, NEXT_TERMINATOR, and not the last active node.
223 * Finishing the iteration when encountering such a TERMINATOR is
224 * good enough for read-only traversals, so such traversals can use
225 * p.next == null as the termination condition. When we need to
226 * find the last (active) node, for enqueueing a new node, we need
227 * to check whether we have reached a TERMINATOR node; if so,
228 * restart traversal from tail.
230 * The implementation is completely directionally symmetrical,
231 * except that most public methods that iterate through the list
232 * follow next pointers ("forward" direction).
234 * We believe (without full proof) that all single-element deque
235 * operations (e.g., addFirst, peekLast, pollLast) are linearizable
236 * (see Herlihy and Shavit's book). However, some combinations of
237 * operations are known not to be linearizable. In particular,
238 * when an addFirst(A) is racing with pollFirst() removing B, it is
239 * possible for an observer iterating over the elements to observe
240 * A B C and subsequently observe A C, even though no interior
241 * removes are ever performed. Nevertheless, iterators behave
242 * reasonably, providing the "weakly consistent" guarantees.
244 * Empirically, microbenchmarks suggest that this class adds about
245 * 40% overhead relative to ConcurrentLinkedQueue, which feels as
246 * good as we can hope for.
249 private static final long serialVersionUID = 876323262645176354L;
252 * A node from which the first node on list (that is, the unique node p
253 * with p.prev == null && p.next != p) can be reached in O(1) time.
255 * - the first node is always O(1) reachable from head via prev links
256 * - all live nodes are reachable from the first node via succ()
258 * - (tmp = head).next != tmp || tmp != head
259 * - head is never gc-unlinked (but may be unlinked)
261 * - head.item may or may not be null
262 * - head may not be reachable from the first or last node, or from tail
264 private transient volatile Node<E> head;
267 * A node from which the last node on list (that is, the unique node p
268 * with p.next == null && p.prev != p) can be reached in O(1) time.
270 * - the last node is always O(1) reachable from tail via next links
271 * - all live nodes are reachable from the last node via pred()
273 * - tail is never gc-unlinked (but may be unlinked)
275 * - tail.item may or may not be null
276 * - tail may not be reachable from the first or last node, or from head
278 private transient volatile Node<E> tail;
280 private static final Node<Object> PREV_TERMINATOR, NEXT_TERMINATOR;
282 @SuppressWarnings("unchecked")
283 Node<E> prevTerminator() {
284 return (Node<E>) PREV_TERMINATOR;
287 @SuppressWarnings("unchecked")
288 Node<E> nextTerminator() {
289 return (Node<E>) NEXT_TERMINATOR;
292 static final class Node<E> {
293 volatile Node<E> prev;
295 volatile Node<E> next;
297 Node() { // default constructor for NEXT_TERMINATOR, PREV_TERMINATOR
301 * Constructs a new node. Uses relaxed write because item can
302 * only be seen after publication via casNext or casPrev.
308 boolean casItem(E cmp, E val) {
316 void lazySetNext(Node<E> val) {
320 boolean casNext(Node<E> cmp, Node<E> val) {
328 void lazySetPrev(Node<E> val) {
332 boolean casPrev(Node<E> cmp, Node<E> val) {
342 * Links e as first element.
344 private void linkFirst(E e) {
346 final Node<E> newNode = new Node<E>(e);
350 for (Node<E> h = head, p = h, q;;) {
351 if ((q = p.prev) != null &&
352 (q = (p = q).prev) != null)
353 // Check for head updates every other hop.
354 // If p == q, we are sure to follow head instead.
355 p = (h != (h = head)) ? h : q;
356 else if (p.next == p) // PREV_TERMINATOR
357 continue restartFromHead;
360 newNode.lazySetNext(p); // CAS piggyback
361 if (p.casPrev(null, newNode)) {
362 // Successful CAS is the linearization point
363 // for e to become an element of this deque,
364 // and for newNode to become "live".
365 if (p != h) // hop two nodes at a time
366 casHead(h, newNode); // Failure is OK.
369 // Lost CAS race to another thread; re-read prev
375 * Links e as last element.
377 private void linkLast(E e) {
379 final Node<E> newNode = new Node<E>(e);
383 for (Node<E> t = tail, p = t, q;;) {
384 if ((q = p.next) != null &&
385 (q = (p = q).next) != null)
386 // Check for tail updates every other hop.
387 // If p == q, we are sure to follow tail instead.
388 p = (t != (t = tail)) ? t : q;
389 else if (p.prev == p) // NEXT_TERMINATOR
390 continue restartFromTail;
393 newNode.lazySetPrev(p); // CAS piggyback
394 if (p.casNext(null, newNode)) {
395 // Successful CAS is the linearization point
396 // for e to become an element of this deque,
397 // and for newNode to become "live".
398 if (p != t) // hop two nodes at a time
399 casTail(t, newNode); // Failure is OK.
402 // Lost CAS race to another thread; re-read next
407 private static final int HOPS = 2;
410 * Unlinks non-null node x.
412 void unlink(Node<E> x) {
414 // assert x.item == null;
415 // assert x != PREV_TERMINATOR;
416 // assert x != NEXT_TERMINATOR;
418 final Node<E> prev = x.prev;
419 final Node<E> next = x.next;
421 unlinkFirst(x, next);
422 } else if (next == null) {
425 // Unlink interior node.
427 // This is the common case, since a series of polls at the
428 // same end will be "interior" removes, except perhaps for
429 // the first one, since end nodes cannot be unlinked.
431 // At any time, all active nodes are mutually reachable by
432 // following a sequence of either next or prev pointers.
434 // Our strategy is to find the unique active predecessor
435 // and successor of x. Try to fix up their links so that
436 // they point to each other, leaving x unreachable from
437 // active nodes. If successful, and if x has no live
438 // predecessor/successor, we additionally try to gc-unlink,
439 // leaving active nodes unreachable from x, by rechecking
440 // that the status of predecessor and successor are
441 // unchanged and ensuring that x is not reachable from
442 // tail/head, before setting x's prev/next links to their
443 // logical approximate replacements, self/TERMINATOR.
444 Node<E> activePred, activeSucc;
445 boolean isFirst, isLast;
448 // Find active predecessor
449 for (Node<E> p = prev; ; ++hops) {
450 if (p.item != null) {
469 // Find active successor
470 for (Node<E> p = next; ; ++hops) {
471 if (p.item != null) {
490 // TODO: better HOP heuristics
492 // always squeeze out interior deleted nodes
493 && (isFirst | isLast))
496 // Squeeze out deleted nodes between activePred and
497 // activeSucc, including x.
498 skipDeletedSuccessors(activePred);
499 skipDeletedPredecessors(activeSucc);
501 // Try to gc-unlink, if possible
502 if ((isFirst | isLast) &&
504 // Recheck expected state of predecessor and successor
505 (activePred.next == activeSucc) &&
506 (activeSucc.prev == activePred) &&
507 (isFirst ? activePred.prev == null : activePred.item != null) &&
508 (isLast ? activeSucc.next == null : activeSucc.item != null)) {
510 updateHead(); // Ensure x is not reachable from head
511 updateTail(); // Ensure x is not reachable from tail
513 // Finally, actually gc-unlink
514 x.lazySetPrev(isFirst ? prevTerminator() : x);
515 x.lazySetNext(isLast ? nextTerminator() : x);
521 * Unlinks non-null first node.
523 private void unlinkFirst(Node<E> first, Node<E> next) {
524 // assert first != null;
525 // assert next != null;
526 // assert first.item == null;
527 for (Node<E> o = null, p = next, q;;) {
528 if (p.item != null || (q = p.next) == null) {
529 if (o != null && p.prev != p && first.casNext(next, p)) {
530 skipDeletedPredecessors(p);
531 if (first.prev == null &&
532 (p.next == null || p.item != null) &&
535 updateHead(); // Ensure o is not reachable from head
536 updateTail(); // Ensure o is not reachable from tail
538 // Finally, actually gc-unlink
540 o.lazySetPrev(prevTerminator());
555 * Unlinks non-null last node.
557 private void unlinkLast(Node<E> last, Node<E> prev) {
558 // assert last != null;
559 // assert prev != null;
560 // assert last.item == null;
561 for (Node<E> o = null, p = prev, q;;) {
562 if (p.item != null || (q = p.prev) == null) {
563 if (o != null && p.next != p && last.casPrev(prev, p)) {
564 skipDeletedSuccessors(p);
565 if (last.next == null &&
566 (p.prev == null || p.item != null) &&
569 updateHead(); // Ensure o is not reachable from head
570 updateTail(); // Ensure o is not reachable from tail
572 // Finally, actually gc-unlink
574 o.lazySetNext(nextTerminator());
589 * Guarantees that any node which was unlinked before a call to
590 * this method will be unreachable from head after it returns.
591 * Does not guarantee to eliminate slack, only that head will
592 * point to a node that was active while this method was running.
594 private final void updateHead() {
595 // Either head already points to an active node, or we keep
596 // trying to cas it to the first node until it does.
599 while ((h = head).item == null && (p = h.prev) != null) {
601 if ((q = p.prev) == null ||
602 (q = (p = q).prev) == null) {
603 // It is possible that p is PREV_TERMINATOR,
604 // but if so, the CAS is guaranteed to fail.
608 continue restartFromHead;
611 continue restartFromHead;
619 * Guarantees that any node which was unlinked before a call to
620 * this method will be unreachable from tail after it returns.
621 * Does not guarantee to eliminate slack, only that tail will
622 * point to a node that was active while this method was running.
624 private final void updateTail() {
625 // Either tail already points to an active node, or we keep
626 // trying to cas it to the last node until it does.
629 while ((t = tail).item == null && (p = t.next) != null) {
631 if ((q = p.next) == null ||
632 (q = (p = q).next) == null) {
633 // It is possible that p is NEXT_TERMINATOR,
634 // but if so, the CAS is guaranteed to fail.
638 continue restartFromTail;
641 continue restartFromTail;
648 private void skipDeletedPredecessors(Node<E> x) {
651 Node<E> prev = x.prev;
652 // assert prev != null;
653 // assert x != NEXT_TERMINATOR;
654 // assert x != PREV_TERMINATOR;
663 continue whileActive;
667 continue whileActive;
672 // found active CAS target
673 if (prev == p || x.casPrev(prev, p))
676 } while (x.item != null || x.next == null);
679 private void skipDeletedSuccessors(Node<E> x) {
682 Node<E> next = x.next;
683 // assert next != null;
684 // assert x != NEXT_TERMINATOR;
685 // assert x != PREV_TERMINATOR;
694 continue whileActive;
698 continue whileActive;
703 // found active CAS target
704 if (next == p || x.casNext(next, p))
707 } while (x.item != null || x.prev == null);
711 * Returns the successor of p, or the first node if p.next has been
712 * linked to self, which will only be true if traversing with a
713 * stale pointer that is now off the list.
715 final Node<E> succ(Node<E> p) {
716 // TODO: should we skip deleted nodes here?
718 return (p == q) ? first() : q;
722 * Returns the predecessor of p, or the last node if p.prev has been
723 * linked to self, which will only be true if traversing with a
724 * stale pointer that is now off the list.
726 final Node<E> pred(Node<E> p) {
728 return (p == q) ? last() : q;
732 * Returns the first node, the unique node p for which:
733 * p.prev == null && p.next != p
734 * The returned node may or may not be logically deleted.
735 * Guarantees that head is set to the returned node.
740 for (Node<E> h = head, p = h, q;;) {
741 if ((q = p.prev) != null &&
742 (q = (p = q).prev) != null)
743 // Check for head updates every other hop.
744 // If p == q, we are sure to follow head instead.
745 p = (h != (h = head)) ? h : q;
747 // It is possible that p is PREV_TERMINATOR,
748 // but if so, the CAS is guaranteed to fail.
752 continue restartFromHead;
757 * Returns the last node, the unique node p for which:
758 * p.next == null && p.prev != p
759 * The returned node may or may not be logically deleted.
760 * Guarantees that tail is set to the returned node.
765 for (Node<E> t = tail, p = t, q;;) {
766 if ((q = p.next) != null &&
767 (q = (p = q).next) != null)
768 // Check for tail updates every other hop.
769 // If p == q, we are sure to follow tail instead.
770 p = (t != (t = tail)) ? t : q;
772 // It is possible that p is NEXT_TERMINATOR,
773 // but if so, the CAS is guaranteed to fail.
777 continue restartFromTail;
781 // Minor convenience utilities
784 * Throws NullPointerException if argument is null.
786 * @param v the element
788 private static void checkNotNull(Object v) {
790 throw new NullPointerException();
794 * Returns element unless it is null, in which case throws
795 * NoSuchElementException.
797 * @param v the element
798 * @return the element
800 private E screenNullResult(E v) {
802 throw new NoSuchElementException();
807 * Creates an array list and fills it with elements of this list.
810 * @return the arrayList
812 private ArrayList<E> toArrayList() {
813 ArrayList<E> list = new ArrayList<E>();
814 for (Node<E> p = first(); p != null; p = succ(p)) {
823 * Constructs an empty deque.
825 public ConcurrentLinkedDeque() {
826 head = tail = new Node<E>(null);
830 * Constructs a deque initially containing the elements of
831 * the given collection, added in traversal order of the
832 * collection's iterator.
834 * @param c the collection of elements to initially contain
835 * @throws NullPointerException if the specified collection or any
836 * of its elements are null
838 public ConcurrentLinkedDeque(Collection<? extends E> c) {
839 // Copy c into a private chain of Nodes
840 Node<E> h = null, t = null;
843 Node<E> newNode = new Node<E>(e);
847 t.lazySetNext(newNode);
848 newNode.lazySetPrev(t);
856 * Initializes head and tail, ensuring invariants hold.
858 private void initHeadTail(Node<E> h, Node<E> t) {
861 h = t = new Node<E>(null);
863 // Avoid edge case of a single Node with non-null item.
864 Node<E> newNode = new Node<E>(null);
865 t.lazySetNext(newNode);
866 newNode.lazySetPrev(t);
875 * Inserts the specified element at the front of this deque.
876 * As the deque is unbounded, this method will never throw
877 * {@link IllegalStateException}.
879 * @throws NullPointerException if the specified element is null
881 public void addFirst(E e) {
886 * Inserts the specified element at the end of this deque.
887 * As the deque is unbounded, this method will never throw
888 * {@link IllegalStateException}.
890 * <p>This method is equivalent to {@link #add}.
892 * @throws NullPointerException if the specified element is null
894 public void addLast(E e) {
899 * Inserts the specified element at the front of this deque.
900 * As the deque is unbounded, this method will never return {@code false}.
902 * @return {@code true} (as specified by {@link Deque#offerFirst})
903 * @throws NullPointerException if the specified element is null
905 public boolean offerFirst(E e) {
911 * Inserts the specified element at the end of this deque.
912 * As the deque is unbounded, this method will never return {@code false}.
914 * <p>This method is equivalent to {@link #add}.
916 * @return {@code true} (as specified by {@link Deque#offerLast})
917 * @throws NullPointerException if the specified element is null
919 public boolean offerLast(E e) {
924 public E peekFirst() {
925 for (Node<E> p = first(); p != null; p = succ(p)) {
933 public E peekLast() {
934 for (Node<E> p = last(); p != null; p = pred(p)) {
943 * @throws NoSuchElementException {@inheritDoc}
945 public E getFirst() {
946 return screenNullResult(peekFirst());
950 * @throws NoSuchElementException {@inheritDoc}
953 return screenNullResult(peekLast());
956 public E pollFirst() {
957 for (Node<E> p = first(); p != null; p = succ(p)) {
959 if (item != null && p.casItem(item, null)) {
967 public E pollLast() {
968 for (Node<E> p = last(); p != null; p = pred(p)) {
970 if (item != null && p.casItem(item, null)) {
979 * @throws NoSuchElementException {@inheritDoc}
981 public E removeFirst() {
982 return screenNullResult(pollFirst());
986 * @throws NoSuchElementException {@inheritDoc}
988 public E removeLast() {
989 return screenNullResult(pollLast());
992 // *** Queue and stack methods ***
995 * Inserts the specified element at the tail of this deque.
996 * As the deque is unbounded, this method will never return {@code false}.
998 * @return {@code true} (as specified by {@link Queue#offer})
999 * @throws NullPointerException if the specified element is null
1001 public boolean offer(E e) {
1002 return offerLast(e);
1006 * Inserts the specified element at the tail of this deque.
1007 * As the deque is unbounded, this method will never throw
1008 * {@link IllegalStateException} or return {@code false}.
1010 * @return {@code true} (as specified by {@link Collection#add})
1011 * @throws NullPointerException if the specified element is null
1013 public boolean add(E e) {
1014 return offerLast(e);
1017 public E poll() { return pollFirst(); }
1018 public E remove() { return removeFirst(); }
1019 public E peek() { return peekFirst(); }
1020 public E element() { return getFirst(); }
1021 public void push(E e) { addFirst(e); }
1022 public E pop() { return removeFirst(); }
1025 * Removes the first element {@code e} such that
1026 * {@code o.equals(e)}, if such an element exists in this deque.
1027 * If the deque does not contain the element, it is unchanged.
1029 * @param o element to be removed from this deque, if present
1030 * @return {@code true} if the deque contained the specified element
1031 * @throws NullPointerException if the specified element is null
1033 public boolean removeFirstOccurrence(Object o) {
1035 for (Node<E> p = first(); p != null; p = succ(p)) {
1037 if (item != null && o.equals(item) && p.casItem(item, null)) {
1046 * Removes the last element {@code e} such that
1047 * {@code o.equals(e)}, if such an element exists in this deque.
1048 * If the deque does not contain the element, it is unchanged.
1050 * @param o element to be removed from this deque, if present
1051 * @return {@code true} if the deque contained the specified element
1052 * @throws NullPointerException if the specified element is null
1054 public boolean removeLastOccurrence(Object o) {
1056 for (Node<E> p = last(); p != null; p = pred(p)) {
1058 if (item != null && o.equals(item) && p.casItem(item, null)) {
1067 * Returns {@code true} if this deque contains at least one
1068 * element {@code e} such that {@code o.equals(e)}.
1070 * @param o element whose presence in this deque is to be tested
1071 * @return {@code true} if this deque contains the specified element
1073 public boolean contains(Object o) {
1074 if (o == null) return false;
1075 for (Node<E> p = first(); p != null; p = succ(p)) {
1077 if (item != null && o.equals(item))
1084 * Returns {@code true} if this collection contains no elements.
1086 * @return {@code true} if this collection contains no elements
1088 public boolean isEmpty() {
1089 return peekFirst() == null;
1093 * Returns the number of elements in this deque. If this deque
1094 * contains more than {@code Integer.MAX_VALUE} elements, it
1095 * returns {@code Integer.MAX_VALUE}.
1097 * <p>Beware that, unlike in most collections, this method is
1098 * <em>NOT</em> a constant-time operation. Because of the
1099 * asynchronous nature of these deques, determining the current
1100 * number of elements requires traversing them all to count them.
1101 * Additionally, it is possible for the size to change during
1102 * execution of this method, in which case the returned result
1103 * will be inaccurate. Thus, this method is typically not very
1104 * useful in concurrent applications.
1106 * @return the number of elements in this deque
1110 for (Node<E> p = first(); p != null; p = succ(p))
1112 // Collection.size() spec says to max out
1113 if (++count == Integer.MAX_VALUE)
1119 * Removes the first element {@code e} such that
1120 * {@code o.equals(e)}, if such an element exists in this deque.
1121 * If the deque does not contain the element, it is unchanged.
1123 * @param o element to be removed from this deque, if present
1124 * @return {@code true} if the deque contained the specified element
1125 * @throws NullPointerException if the specified element is null
1127 public boolean remove(Object o) {
1128 return removeFirstOccurrence(o);
1132 * Appends all of the elements in the specified collection to the end of
1133 * this deque, in the order that they are returned by the specified
1134 * collection's iterator. Attempts to {@code addAll} of a deque to
1135 * itself result in {@code IllegalArgumentException}.
1137 * @param c the elements to be inserted into this deque
1138 * @return {@code true} if this deque changed as a result of the call
1139 * @throws NullPointerException if the specified collection or any
1140 * of its elements are null
1141 * @throws IllegalArgumentException if the collection is this deque
1143 public boolean addAll(Collection<? extends E> c) {
1145 // As historically specified in AbstractQueue#addAll
1146 throw new IllegalArgumentException();
1148 // Copy c into a private chain of Nodes
1149 Node<E> beginningOfTheEnd = null, last = null;
1152 Node<E> newNode = new Node<E>(e);
1153 if (beginningOfTheEnd == null)
1154 beginningOfTheEnd = last = newNode;
1156 last.lazySetNext(newNode);
1157 newNode.lazySetPrev(last);
1161 if (beginningOfTheEnd == null)
1164 // Atomically append the chain at the tail of this collection
1167 for (Node<E> t = tail, p = t, q;;) {
1168 if ((q = p.next) != null &&
1169 (q = (p = q).next) != null)
1170 // Check for tail updates every other hop.
1171 // If p == q, we are sure to follow tail instead.
1172 p = (t != (t = tail)) ? t : q;
1173 else if (p.prev == p) // NEXT_TERMINATOR
1174 continue restartFromTail;
1177 beginningOfTheEnd.lazySetPrev(p); // CAS piggyback
1178 if (p.casNext(null, beginningOfTheEnd)) {
1179 // Successful CAS is the linearization point
1180 // for all elements to be added to this deque.
1181 if (!casTail(t, last)) {
1182 // Try a little harder to update tail,
1183 // since we may be adding many elements.
1185 if (last.next == null)
1190 // Lost CAS race to another thread; re-read next
1196 * Removes all of the elements from this deque.
1198 public void clear() {
1199 while (pollFirst() != null)
1204 * Returns an array containing all of the elements in this deque, in
1205 * proper sequence (from first to last element).
1207 * <p>The returned array will be "safe" in that no references to it are
1208 * maintained by this deque. (In other words, this method must allocate
1209 * a new array). The caller is thus free to modify the returned array.
1211 * <p>This method acts as bridge between array-based and collection-based
1214 * @return an array containing all of the elements in this deque
1216 public Object[] toArray() {
1217 return toArrayList().toArray();
1221 * Returns an array containing all of the elements in this deque,
1222 * in proper sequence (from first to last element); the runtime
1223 * type of the returned array is that of the specified array. If
1224 * the deque fits in the specified array, it is returned therein.
1225 * Otherwise, a new array is allocated with the runtime type of
1226 * the specified array and the size of this deque.
1228 * <p>If this deque fits in the specified array with room to spare
1229 * (i.e., the array has more elements than this deque), the element in
1230 * the array immediately following the end of the deque is set to
1233 * <p>Like the {@link #toArray()} method, this method acts as
1234 * bridge between array-based and collection-based APIs. Further,
1235 * this method allows precise control over the runtime type of the
1236 * output array, and may, under certain circumstances, be used to
1237 * save allocation costs.
1239 * <p>Suppose {@code x} is a deque known to contain only strings.
1240 * The following code can be used to dump the deque into a newly
1241 * allocated array of {@code String}:
1244 * String[] y = x.toArray(new String[0]);</pre>
1246 * Note that {@code toArray(new Object[0])} is identical in function to
1247 * {@code toArray()}.
1249 * @param a the array into which the elements of the deque are to
1250 * be stored, if it is big enough; otherwise, a new array of the
1251 * same runtime type is allocated for this purpose
1252 * @return an array containing all of the elements in this deque
1253 * @throws ArrayStoreException if the runtime type of the specified array
1254 * is not a supertype of the runtime type of every element in
1256 * @throws NullPointerException if the specified array is null
1258 public <T> T[] toArray(T[] a) {
1259 return toArrayList().toArray(a);
1263 * Returns an iterator over the elements in this deque in proper sequence.
1264 * The elements will be returned in order from first (head) to last (tail).
1266 * <p>The returned iterator is a "weakly consistent" iterator that
1267 * will never throw {@link java.util.ConcurrentModificationException
1268 * ConcurrentModificationException}, and guarantees to traverse
1269 * elements as they existed upon construction of the iterator, and
1270 * may (but is not guaranteed to) reflect any modifications
1271 * subsequent to construction.
1273 * @return an iterator over the elements in this deque in proper sequence
1275 public Iterator<E> iterator() {
1280 * Returns an iterator over the elements in this deque in reverse
1281 * sequential order. The elements will be returned in order from
1282 * last (tail) to first (head).
1284 * <p>The returned iterator is a "weakly consistent" iterator that
1285 * will never throw {@link java.util.ConcurrentModificationException
1286 * ConcurrentModificationException}, and guarantees to traverse
1287 * elements as they existed upon construction of the iterator, and
1288 * may (but is not guaranteed to) reflect any modifications
1289 * subsequent to construction.
1291 * @return an iterator over the elements in this deque in reverse order
1293 public Iterator<E> descendingIterator() {
1294 return new DescendingItr();
1297 private abstract class AbstractItr implements Iterator<E> {
1299 * Next node to return item for.
1301 private Node<E> nextNode;
1304 * nextItem holds on to item fields because once we claim
1305 * that an element exists in hasNext(), we must return it in
1306 * the following next() call even if it was in the process of
1307 * being removed when hasNext() was called.
1312 * Node returned by most recent call to next. Needed by remove.
1313 * Reset to null if this element is deleted by a call to remove.
1315 private Node<E> lastRet;
1317 abstract Node<E> startNode();
1318 abstract Node<E> nextNode(Node<E> p);
1325 * Sets nextNode and nextItem to next valid node, or to null
1328 private void advance() {
1331 Node<E> p = (nextNode == null) ? startNode() : nextNode(nextNode);
1332 for (;; p = nextNode(p)) {
1334 // p might be active end or TERMINATOR node; both are OK
1348 public boolean hasNext() {
1349 return nextItem != null;
1354 if (item == null) throw new NoSuchElementException();
1359 public void remove() {
1360 Node<E> l = lastRet;
1361 if (l == null) throw new IllegalStateException();
1368 /** Forward iterator */
1369 private class Itr extends AbstractItr {
1370 Node<E> startNode() { return first(); }
1371 Node<E> nextNode(Node<E> p) { return succ(p); }
1374 /** Descending iterator */
1375 private class DescendingItr extends AbstractItr {
1376 Node<E> startNode() { return last(); }
1377 Node<E> nextNode(Node<E> p) { return pred(p); }
1381 * Saves the state to a stream (that is, serializes it).
1383 * @serialData All of the elements (each an {@code E}) in
1384 * the proper order, followed by a null
1385 * @param s the stream
1387 private void writeObject(java.io.ObjectOutputStream s)
1388 throws java.io.IOException {
1390 // Write out any hidden stuff
1391 s.defaultWriteObject();
1393 // Write out all elements in the proper order.
1394 for (Node<E> p = first(); p != null; p = succ(p)) {
1397 s.writeObject(item);
1400 // Use trailing null as sentinel
1401 s.writeObject(null);
1405 * Reconstitutes the instance from a stream (that is, deserializes it).
1406 * @param s the stream
1408 private void readObject(java.io.ObjectInputStream s)
1409 throws java.io.IOException, ClassNotFoundException {
1410 s.defaultReadObject();
1412 // Read in elements until trailing null sentinel found
1413 Node<E> h = null, t = null;
1415 while ((item = s.readObject()) != null) {
1416 @SuppressWarnings("unchecked")
1417 Node<E> newNode = new Node<E>((E) item);
1421 t.lazySetNext(newNode);
1422 newNode.lazySetPrev(t);
1430 private boolean casHead(Node<E> cmp, Node<E> val) {
1438 private boolean casTail(Node<E> cmp, Node<E> val) {
1447 PREV_TERMINATOR = new Node<Object>();
1448 PREV_TERMINATOR.next = PREV_TERMINATOR;
1449 NEXT_TERMINATOR = new Node<Object>();
1450 NEXT_TERMINATOR.prev = NEXT_TERMINATOR;