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