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