src/java.base/share/classes/java/util/concurrent/LinkedBlockingQueue.java
author dl
Sat, 10 Feb 2018 09:23:41 -0800
changeset 48843 21efc1774302
parent 48541 946e34c2dec9
child 49433 b6671a111395
permissions -rw-r--r--
8195590: Miscellaneous changes imported from jsr166 CVS 2018-02 Reviewed-by: martin, psandoz, dholmes
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: 3707
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: 3707
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: 3707
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3707
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;
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
    42
import java.util.Objects;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    43
import java.util.Spliterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    44
import java.util.Spliterators;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    45
import java.util.concurrent.atomic.AtomicInteger;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    46
import java.util.concurrent.locks.Condition;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    47
import java.util.concurrent.locks.ReentrantLock;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    48
import java.util.function.Consumer;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
    49
import java.util.function.Predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * An optionally-bounded {@linkplain BlockingQueue blocking queue} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This queue orders elements FIFO (first-in-first-out).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * queue the longest time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The <em>tail</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Linked queues typically have higher throughput than array-based queues but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * less predictable performance in most concurrent applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    64
 * <p>The optional capacity bound constructor argument serves as a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * way to prevent excessive queue expansion. The capacity, if unspecified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * is equal to {@link Integer#MAX_VALUE}.  Linked nodes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * dynamically created upon each insertion unless this would bring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * queue above capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
    70
 * <p>This class and its iterator implement all of the <em>optional</em>
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
    71
 * methods of the {@link Collection} and {@link Iterator} interfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>This class is a member of the
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 43521
diff changeset
    74
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    79
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final long serialVersionUID = -6903933977591709194L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * A variant of the "two lock queue" algorithm.  The putLock gates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * entry to put (and offer), and has an associated condition for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * waiting puts.  Similarly for the takeLock.  The "count" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * that they both rely on is maintained as an atomic to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * needing to get both locks in most cases. Also, to minimize need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * for puts to get takeLock and vice-versa, cascading notifies are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * used. When a put notices that it has enabled at least one take,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * it signals taker. That taker in turn signals others if more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * items have been entered since the signal. And symmetrically for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * takes signalling puts. Operations such as remove(Object) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * iterators acquire both locks.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    97
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    98
     * Visibility between writers and readers is provided as follows:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    99
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   100
     * Whenever an element is enqueued, the putLock is acquired and
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   101
     * count updated.  A subsequent reader guarantees visibility to the
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   102
     * enqueued Node by either acquiring the putLock (via fullyLock)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   103
     * or by acquiring the takeLock, and then reading n = count.get();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   104
     * this gives visibility to the first n items.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   105
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   106
     * 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
   107
     * 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
   108
     * That would cause two problems:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   109
     * - allow a rogue Iterator to cause unbounded memory retention
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   110
     * - 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
   111
     *   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
   112
     *   hard time dealing with, causing repeated major collections.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   113
     * 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
   114
     * dequeued Nodes, and reachability does not necessarily have to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   115
     * 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
   116
     * 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
   117
     * self-link implicitly means to advance to head.next.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   121
     * Linked list node class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static class Node<E> {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   124
        E item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   125
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   126
        /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   127
         * One of:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   128
         * - the real successor Node
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   129
         * - this Node, meaning the successor is head.next
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   130
         * - null, meaning there is no successor (this is the last node)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   131
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Node<E> next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Node(E x) { item = x; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /** The capacity bound, or Integer.MAX_VALUE if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final int capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /** Current number of elements */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   141
    private final AtomicInteger count = new AtomicInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
3415
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
     * Head of linked list.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   145
     * Invariant: head.item == null
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   146
     */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   147
    transient Node<E> head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   149
    /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   150
     * Tail of linked list.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   151
     * Invariant: last.next == null
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   152
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private transient Node<E> last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /** Lock held by take, poll, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private final ReentrantLock takeLock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /** Wait queue for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private final Condition notEmpty = takeLock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /** Lock held by put, offer, etc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private final ReentrantLock putLock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /** Wait queue for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private final Condition notFull = putLock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Signals a waiting take. Called only from put/offer (which do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * otherwise ordinarily lock takeLock.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private void signalNotEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Signals a waiting put. Called only from take/poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private void signalNotFull() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   195
     * Links node at end of queue.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   196
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   197
     * @param node the node
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   199
    private void enqueue(Node<E> node) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   200
        // assert putLock.isHeldByCurrentThread();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   201
        // assert last.next == null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   202
        last = last.next = node;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   206
     * Removes a node from head of queue.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   207
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   210
    private E dequeue() {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   211
        // assert takeLock.isHeldByCurrentThread();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   212
        // assert head.item == null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   213
        Node<E> h = head;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   214
        Node<E> first = h.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   215
        h.next = h; // help GC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        head = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        E x = first.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        first.item = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   223
     * Locks to prevent both puts and takes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   225
    void fullyLock() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   231
     * Unlocks to allow both puts and takes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   233
    void fullyUnlock() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   239
     * Creates a {@code LinkedBlockingQueue} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * {@link Integer#MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public LinkedBlockingQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        this(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   247
     * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param capacity the capacity of this queue
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   250
     * @throws IllegalArgumentException if {@code capacity} is not greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *         than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public LinkedBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (capacity <= 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this.capacity = capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        last = head = new Node<E>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   260
     * Creates a {@code LinkedBlockingQueue} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * {@link Integer#MAX_VALUE}, initially containing the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public LinkedBlockingQueue(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        this(Integer.MAX_VALUE);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   271
        final ReentrantLock putLock = this.putLock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   272
        putLock.lock(); // Never contended, but necessary for visibility
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   273
        try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   274
            int n = 0;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   275
            for (E e : c) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   276
                if (e == null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   277
                    throw new NullPointerException();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   278
                if (n == capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   279
                    throw new IllegalStateException("Queue full");
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   280
                enqueue(new Node<E>(e));
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   281
                ++n;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   282
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   283
            count.set(n);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   284
        } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   285
            putLock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   286
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * blocking. This is always equal to the initial capacity of this queue
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   306
     * less the current {@code size} of this queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <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
   309
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return capacity - count.get();
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
     * Inserts the specified element at the tail of this queue, waiting if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * necessary for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void put(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (e == null) throw new NullPointerException();
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   326
        final int c;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   327
        final Node<E> node = new Node<E>(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        putLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
             * Note that count is used in wait guard even though it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
             * not protected by lock. This works because count can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
             * only decrease at this point (all other puts are shut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
             * out by lock), and we (or some other waiting put) are
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   337
             * signalled if it ever changes from capacity. Similarly
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   338
             * for all other uses of count in other wait guards.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
             */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   340
            while (count.get() == capacity) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   341
                notFull.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   343
            enqueue(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            c = count.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (c + 1 < capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            signalNotEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Inserts the specified element at the tail of this queue, waiting if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * necessary up to the specified wait time for space to become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   358
     * @return {@code true} if successful, or {@code false} if
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   359
     *         the specified waiting time elapses before space is available
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        long nanos = unit.toNanos(timeout);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   368
        final int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        putLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   373
            while (count.get() == capacity) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   374
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    return false;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   376
                nanos = notFull.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   378
            enqueue(new Node<E>(e));
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   379
            c = count.getAndIncrement();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   380
            if (c + 1 < capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   381
                notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            signalNotEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * possible to do so immediately without exceeding the queue's capacity,
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   393
     * returning {@code true} upon success and {@code false} if this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * When using a capacity-restricted queue, this method is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * preferable to method {@link BlockingQueue#add add}, which can fail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (count.get() == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return false;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   406
        final int c;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   407
        final Node<E> node = new Node<E>(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        final ReentrantLock putLock = this.putLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        putLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        try {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   411
            if (count.get() == capacity)
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   412
                return false;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   413
            enqueue(node);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   414
            c = count.getAndIncrement();
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   415
            if (c + 1 < capacity)
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   416
                notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            putLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            signalNotEmpty();
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   422
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public E take() throws InterruptedException {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   426
        final E x;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   427
        final int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        takeLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   432
            while (count.get() == 0) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   433
                notEmpty.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   435
            x = dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            c = count.getAndDecrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (c > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   448
        final E x;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   449
        final int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        takeLock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   455
            while (count.get() == 0) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   456
                if (nanos <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    return null;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   458
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   460
            x = dequeue();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   461
            c = count.getAndDecrement();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   462
            if (c > 1)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   463
                notEmpty.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        final AtomicInteger count = this.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (count.get() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return null;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   476
        final E x;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   477
        final int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        try {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   481
            if (count.get() == 0)
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   482
                return null;
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   483
            x = dequeue();
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   484
            c = count.getAndDecrement();
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   485
            if (c > 1)
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   486
                notEmpty.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (c == capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            signalNotFull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public E peek() {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   496
        final AtomicInteger count = this.count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (count.get() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        final ReentrantLock takeLock = this.takeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        takeLock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        try {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   502
            return (count.get() > 0) ? head.next.item : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            takeLock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   509
     * Unlinks interior Node p with predecessor pred.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   510
     */
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   511
    void unlink(Node<E> p, Node<E> pred) {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   512
        // assert putLock.isHeldByCurrentThread();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   513
        // assert takeLock.isHeldByCurrentThread();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   514
        // p.next is not changed, to allow iterators that are
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   515
        // traversing p to maintain their weak-consistency guarantee.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   516
        p.item = null;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   517
        pred.next = p.next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   518
        if (last == p)
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   519
            last = pred;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   520
        if (count.getAndDecrement() == capacity)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   521
            notFull.signal();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   522
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   523
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   524
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Removes a single instance of the specified element from this queue,
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   526
     * if it is present.  More formally, removes an element {@code e} such
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   527
     * that {@code o.equals(e)}, if this queue contains one or more such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * elements.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   529
     * Returns {@code true} if this queue contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param o element to be removed from this queue, if present
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   533
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        try {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   539
            for (Node<E> pred = head, p = pred.next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   540
                 p != null;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   541
                 pred = p, p = p.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (o.equals(p.item)) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   543
                    unlink(p, pred);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   544
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   547
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   554
     * Returns {@code true} if this queue contains the specified element.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   555
     * More formally, returns {@code true} if and only if this queue contains
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   556
     * at least one element {@code e} such that {@code o.equals(e)}.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   557
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   558
     * @param o object to be checked for containment in this queue
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   559
     * @return {@code true} if this queue contains the specified element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   560
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   561
    public boolean contains(Object o) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   562
        if (o == null) return false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   563
        fullyLock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   564
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   565
            for (Node<E> p = head.next; p != null; p = p.next)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   566
                if (o.equals(p.item))
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   567
                    return true;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   568
            return false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   569
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   570
            fullyUnlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   571
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   572
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   573
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   574
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int size = count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            Object[] a = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            for (Node<E> p = head.next; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                a[k++] = p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * the array immediately following the end of the queue is set to
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   611
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   618
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * The following code can be used to dump the queue into a newly
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   620
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   622
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   624
     * 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
   625
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   636
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            int size = count.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                a = (T[])java.lang.reflect.Array.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    (a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            int k = 0;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   646
            for (Node<E> p = head.next; p != null; p = p.next)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                a[k++] = (T)p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (a.length > k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                a[k] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    public String toString() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   657
        return Helpers.collectionToString(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   667
            for (Node<E> p, h = head; (p = h.next) != null; h = p) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   668
                h.next = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   669
                p.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   670
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   671
            head = last;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   672
            // assert head.item == null && head.next == null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (count.getAndSet(0) == capacity)
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   674
                notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public int drainTo(Collection<? super E> c) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   687
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public int drainTo(Collection<? super E> c, int maxElements) {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   697
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            throw new IllegalArgumentException();
11013
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   700
        if (maxElements <= 0)
27f7a2f3be20 7107516: LinkedBlockingQueue/Deque.drainTo(Collection, int) returns 'maxElements' if its value is negative
dl
parents: 9242
diff changeset
   701
            return 0;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   702
        boolean signalNotFull = false;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   703
        final ReentrantLock takeLock = this.takeLock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   704
        takeLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   706
            int n = Math.min(maxElements, count.get());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   707
            // count.get provides visibility to first n Nodes
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   708
            Node<E> h = head;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   709
            int i = 0;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   710
            try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   711
                while (i < n) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   712
                    Node<E> p = h.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   713
                    c.add(p.item);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   714
                    p.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   715
                    h.next = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   716
                    h = p;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   717
                    ++i;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   718
                }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   719
                return n;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   720
            } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   721
                // Restore invariants even if c.add() threw
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   722
                if (i > 0) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   723
                    // assert h.item == null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   724
                    head = h;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   725
                    signalNotFull = (count.getAndAdd(-i) == capacity);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   726
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   729
            takeLock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   730
            if (signalNotFull)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   731
                signalNotFull();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   736
     * Used for any element traversal that is not entirely under lock.
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   737
     * Such traversals must handle both:
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   738
     * - dequeued nodes (p.next == p)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   739
     * - (possibly multiple) interior removed nodes (p.item == null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   740
     */
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   741
    Node<E> succ(Node<E> p) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   742
        if (p == (p = p.next))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   743
            p = head.next;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   744
        return p;
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   745
    }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   746
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   747
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Returns an iterator over the elements in this queue in proper sequence.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   749
     * The elements will be returned in order from first (head) to last (tail).
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   750
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   751
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   752
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public Iterator<E> iterator() {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
   757
        return new Itr();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   760
    /**
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   761
     * Weakly-consistent iterator.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   762
     *
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   763
     * Lazily updated ancestor field provides expected O(1) remove(),
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   764
     * but still O(n) in the worst case, whenever the saved ancestor
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   765
     * is concurrently deleted.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   766
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private class Itr implements Iterator<E> {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   768
        private Node<E> next;           // Node holding nextItem
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   769
        private E nextItem;             // next item to hand out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        private Node<E> lastRet;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   771
        private Node<E> ancestor;       // Helps unlink lastRet on remove()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        Itr() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   774
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            try {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   776
                if ((next = head.next) != null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   777
                    nextItem = next.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   779
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public boolean hasNext() {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   784
            return next != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        public E next() {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   788
            Node<E> p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   789
            if ((p = next) == null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   790
                throw new NoSuchElementException();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   791
            lastRet = p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   792
            E x = nextItem;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   793
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            try {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   795
                E e = null;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   796
                for (p = p.next; p != null && (e = p.item) == null; )
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   797
                    p = succ(p);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   798
                next = p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   799
                nextItem = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   801
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   803
            return x;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   804
        }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   805
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   806
        public void forEachRemaining(Consumer<? super E> action) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   807
            // A variant of forEachFrom
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   808
            Objects.requireNonNull(action);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   809
            Node<E> p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   810
            if ((p = next) == null) return;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   811
            lastRet = p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   812
            next = null;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   813
            final int batchSize = 64;
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   814
            Object[] es = null;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   815
            int n, len = 1;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   816
            do {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   817
                fullyLock();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   818
                try {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   819
                    if (es == null) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   820
                        p = p.next;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   821
                        for (Node<E> q = p; q != null; q = succ(q))
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   822
                            if (q.item != null && ++len == batchSize)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   823
                                break;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   824
                        es = new Object[len];
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   825
                        es[0] = nextItem;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   826
                        nextItem = null;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   827
                        n = 1;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   828
                    } else
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   829
                        n = 0;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   830
                    for (; p != null && n < len; p = succ(p))
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   831
                        if ((es[n] = p.item) != null) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   832
                            lastRet = p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   833
                            n++;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   834
                        }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   835
                } finally {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   836
                    fullyUnlock();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   837
                }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   838
                for (int i = 0; i < n; i++) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   839
                    @SuppressWarnings("unchecked") E e = (E) es[i];
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   840
                    action.accept(e);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   841
                }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   842
            } while (n > 0 && p != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        public void remove() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   846
            Node<E> p = lastRet;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   847
            if (p == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                throw new IllegalStateException();
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   849
            lastRet = null;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   850
            fullyLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            try {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   852
                if (p.item != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   853
                    if (ancestor == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   854
                        ancestor = head;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   855
                    ancestor = findPred(p, ancestor);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   856
                    unlink(p, ancestor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            } finally {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   859
                fullyUnlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   864
    /**
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   865
     * A customized variant of Spliterators.IteratorSpliterator.
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   866
     * Keep this class in sync with (very similar) LBDSpliterator.
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   867
     */
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   868
    private final class LBQSpliterator implements Spliterator<E> {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   869
        static final int MAX_BATCH = 1 << 25;  // max batch array size;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   870
        Node<E> current;    // current node; null until initialized
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   871
        int batch;          // batch size for splits
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   872
        boolean exhausted;  // true when no more nodes
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   873
        long est = size();  // size estimate
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   874
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   875
        LBQSpliterator() {}
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   876
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   877
        public long estimateSize() { return est; }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   878
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   879
        public Spliterator<E> trySplit() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   880
            Node<E> h;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   881
            if (!exhausted &&
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   882
                ((h = current) != null || (h = head.next) != null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   883
                && h.next != null) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   884
                int n = batch = Math.min(batch + 1, MAX_BATCH);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   885
                Object[] a = new Object[n];
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   886
                int i = 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   887
                Node<E> p = current;
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   888
                fullyLock();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   889
                try {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   890
                    if (p != null || (p = head.next) != null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   891
                        for (; p != null && i < n; p = succ(p))
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   892
                            if ((a[i] = p.item) != null)
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   893
                                i++;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   894
                } finally {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   895
                    fullyUnlock();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   896
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   897
                if ((current = p) == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   898
                    est = 0L;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   899
                    exhausted = true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   900
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   901
                else if ((est -= i) < 0L)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   902
                    est = 0L;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   903
                if (i > 0)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   904
                    return Spliterators.spliterator
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   905
                        (a, 0, i, (Spliterator.ORDERED |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   906
                                   Spliterator.NONNULL |
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   907
                                   Spliterator.CONCURRENT));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   908
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   909
            return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   910
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   911
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   912
        public boolean tryAdvance(Consumer<? super E> action) {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   913
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   914
            if (!exhausted) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   915
                E e = null;
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   916
                fullyLock();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   917
                try {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   918
                    Node<E> p;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   919
                    if ((p = current) != null || (p = head.next) != null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   920
                        do {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   921
                            e = p.item;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   922
                            p = succ(p);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   923
                        } while (e == null && p != null);
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   924
                    if ((current = p) == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   925
                        exhausted = true;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   926
                } finally {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   927
                    fullyUnlock();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   928
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   929
                if (e != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   930
                    action.accept(e);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   931
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   932
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   933
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   934
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   935
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   936
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   937
        public void forEachRemaining(Consumer<? super E> action) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   938
            Objects.requireNonNull(action);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   939
            if (!exhausted) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   940
                exhausted = true;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   941
                Node<E> p = current;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   942
                current = null;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   943
                forEachFrom(action, p);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   944
            }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   945
        }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   946
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   947
        public int characteristics() {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   948
            return (Spliterator.ORDERED |
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   949
                    Spliterator.NONNULL |
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   950
                    Spliterator.CONCURRENT);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   951
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   952
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   953
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   954
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   955
     * Returns a {@link Spliterator} over the elements in this queue.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   956
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   957
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   958
     * <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
   959
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   960
     * <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
   961
     * {@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
   962
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   963
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   964
     * 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
   965
     * parallelism.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   966
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   967
     * @return a {@code Spliterator} over the elements in this queue
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   968
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   969
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   970
    public Spliterator<E> spliterator() {
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   971
        return new LBQSpliterator();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   972
    }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   973
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   974
    /**
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   975
     * @throws NullPointerException {@inheritDoc}
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   976
     */
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   977
    public void forEach(Consumer<? super E> action) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   978
        Objects.requireNonNull(action);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   979
        forEachFrom(action, null);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   980
    }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   981
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   982
    /**
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   983
     * Runs action on each element found during a traversal starting at p.
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   984
     * If p is null, traversal starts at head.
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   985
     */
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   986
    void forEachFrom(Consumer<? super E> action, Node<E> p) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   987
        // Extract batches of elements while holding the lock; then
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   988
        // run the action on the elements while not
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
   989
        final int batchSize = 64;       // max number of elements per batch
42926
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   990
        Object[] es = null;             // container for batch of elements
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   991
        int n, len = 0;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   992
        do {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   993
            fullyLock();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   994
            try {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   995
                if (es == null) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   996
                    if (p == null) p = head.next;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   997
                    for (Node<E> q = p; q != null; q = succ(q))
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   998
                        if (q.item != null && ++len == batchSize)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
   999
                            break;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1000
                    es = new Object[len];
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1001
                }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1002
                for (n = 0; p != null && n < len; p = succ(p))
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1003
                    if ((es[n] = p.item) != null)
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1004
                        n++;
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1005
            } finally {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1006
                fullyUnlock();
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1007
            }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1008
            for (int i = 0; i < n; i++) {
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1009
                @SuppressWarnings("unchecked") E e = (E) es[i];
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1010
                action.accept(e);
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1011
            }
8b9cacdadb2d 8171051: LinkedBlockingQueue spliterator needs to support node self-linking
dl
parents: 32991
diff changeset
  1012
        } while (n > 0 && p != null);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1013
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1014
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1016
     * @throws NullPointerException {@inheritDoc}
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1017
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1018
    public boolean removeIf(Predicate<? super E> filter) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1019
        Objects.requireNonNull(filter);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1020
        return bulkRemove(filter);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1021
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1022
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1023
    /**
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1024
     * @throws NullPointerException {@inheritDoc}
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1025
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1026
    public boolean removeAll(Collection<?> c) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1027
        Objects.requireNonNull(c);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1028
        return bulkRemove(e -> c.contains(e));
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1029
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1030
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1031
    /**
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1032
     * @throws NullPointerException {@inheritDoc}
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1033
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1034
    public boolean retainAll(Collection<?> c) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1035
        Objects.requireNonNull(c);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1036
        return bulkRemove(e -> !c.contains(e));
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1037
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1038
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1039
    /**
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1040
     * Returns the predecessor of live node p, given a node that was
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1041
     * once a live ancestor of p (or head); allows unlinking of p.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1042
     */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1043
    Node<E> findPred(Node<E> p, Node<E> ancestor) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1044
        // assert p.item != null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1045
        if (ancestor.item == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1046
            ancestor = head;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1047
        // Fails with NPE if precondition not satisfied
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1048
        for (Node<E> q; (q = ancestor.next) != p; )
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1049
            ancestor = q;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1050
        return ancestor;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1051
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1052
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1053
    /** Implementation of bulk remove methods. */
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1054
    @SuppressWarnings("unchecked")
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1055
    private boolean bulkRemove(Predicate<? super E> filter) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1056
        boolean removed = false;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1057
        Node<E> p = null, ancestor = head;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1058
        Node<E>[] nodes = null;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1059
        int n, len = 0;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1060
        do {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1061
            // 1. Extract batch of up to 64 elements while holding the lock.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1062
            fullyLock();
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1063
            try {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
  1064
                if (nodes == null) {  // first batch; initialize
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
  1065
                    p = head.next;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1066
                    for (Node<E> q = p; q != null; q = succ(q))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1067
                        if (q.item != null && ++len == 64)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1068
                            break;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1069
                    nodes = (Node<E>[]) new Node<?>[len];
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1070
                }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1071
                for (n = 0; p != null && n < len; p = succ(p))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1072
                    nodes[n++] = p;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1073
            } finally {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1074
                fullyUnlock();
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1075
            }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1076
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1077
            // 2. Run the filter on the elements while lock is free.
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
  1078
            long deathRow = 0L;       // "bitset" of size 64
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1079
            for (int i = 0; i < n; i++) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1080
                final E e;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1081
                if ((e = nodes[i].item) != null && filter.test(e))
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1082
                    deathRow |= 1L << i;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1083
            }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1084
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1085
            // 3. Remove any filtered elements while holding the lock.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1086
            if (deathRow != 0) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1087
                fullyLock();
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1088
                try {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1089
                    for (int i = 0; i < n; i++) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1090
                        final Node<E> q;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1091
                        if ((deathRow & (1L << i)) != 0L
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1092
                            && (q = nodes[i]).item != null) {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1093
                            ancestor = findPred(q, ancestor);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1094
                            unlink(q, ancestor);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1095
                            removed = true;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1096
                        }
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
  1097
                        nodes[i] = null; // help GC
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1098
                    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1099
                } finally {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1100
                    fullyUnlock();
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1101
                }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1102
            }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1103
        } while (n > 0 && p != null);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1104
        return removed;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1105
    }
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1106
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42926
diff changeset
  1107
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
  1108
     * Saves this queue to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1110
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1111
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @serialData The capacity is emitted (int), followed by all of
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1113
     * its elements (each an {@code Object}) in the proper order,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * followed by a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        fullyLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            // Write out any hidden stuff, plus capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // Write out all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            for (Node<E> p = head.next; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                s.writeObject(p.item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            fullyUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11013
diff changeset
  1136
     * Reconstitutes this queue 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
  1137
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1138
     * @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
  1139
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1140
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        // Read in capacity, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        count.set(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        last = head = new Node<E>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        // Read in all elements and place in queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        for (;;) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1152
            @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            E item = (E)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
}