src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java
author darcy
Wed, 16 Oct 2019 16:55:52 -0700
changeset 58657 6252605fb005
parent 52427 3c6aa484536c
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: 2
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: 2
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: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 8544
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
    39
import java.lang.invoke.VarHandle;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    40
import java.util.AbstractQueue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    41
import java.util.Arrays;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    42
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    43
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    44
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    45
import java.util.NoSuchElementException;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    46
import java.util.Objects;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    47
import java.util.PriorityQueue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    48
import java.util.Queue;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    49
import java.util.SortedSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    50
import java.util.Spliterator;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    51
import java.util.concurrent.locks.Condition;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    52
import java.util.concurrent.locks.ReentrantLock;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
    53
import java.util.function.Consumer;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
    54
import java.util.function.Predicate;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50229
diff changeset
    55
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * An unbounded {@linkplain BlockingQueue blocking queue} that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the same ordering rules as class {@link PriorityQueue} and supplies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * blocking retrieval operations.  While this queue is logically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * unbounded, attempted additions may fail due to resource exhaustion
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    62
 * (causing {@code OutOfMemoryError}). This class does not permit
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    63
 * {@code null} elements.  A priority queue relying on {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Comparable natural ordering} also does not permit insertion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * non-comparable objects (doing so results in
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    66
 * {@code ClassCastException}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    68
 * <p>This class and its iterator implement all of the <em>optional</em>
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    69
 * methods of the {@link Collection} and {@link Iterator} interfaces.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    70
 * The Iterator provided in method {@link #iterator()} and the
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    71
 * Spliterator provided in method {@link #spliterator()} are <em>not</em>
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    72
 * guaranteed to traverse the elements of the PriorityBlockingQueue in
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    73
 * any particular order. If you need ordered traversal, consider using
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    74
 * {@code Arrays.sort(pq.toArray())}.  Also, method {@code drainTo} can
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    75
 * be used to <em>remove</em> some or all elements in priority order and
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    76
 * place them in another collection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>Operations on this class make no guarantees about the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * of elements with equal priority. If you need to enforce an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * ordering, you can define custom classes or comparators that use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * secondary key to break ties in primary priority values.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * example, here is a class that applies first-in-first-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * tie-breaking to comparable elements. To use it, you would insert a
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    84
 * {@code new FIFOEntry(anEntry)} instead of a plain entry object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    86
 * <pre> {@code
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    87
 * class FIFOEntry<E extends Comparable<? super E>>
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    88
 *     implements Comparable<FIFOEntry<E>> {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    89
 *   static final AtomicLong seq = new AtomicLong(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   final long seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   final E entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   public FIFOEntry(E entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     seqNum = seq.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   public E getEntry() { return entry; }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    97
 *   public int compareTo(FIFOEntry<E> other) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     int res = entry.compareTo(other.entry);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    99
 *     if (res == 0 && other.entry != this.entry)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   100
 *       res = (seqNum < other.seqNum ? -1 : 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <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: 48541
diff changeset
   106
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   111
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   113
@SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public class PriorityBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static final long serialVersionUID = 5595510919245408276L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   118
    /*
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   119
     * The implementation uses an array-based binary heap, with public
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   120
     * operations protected with a single lock. However, allocation
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   121
     * during resizing uses a simple spinlock (used only while not
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   122
     * holding main lock) in order to allow takes to operate
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   123
     * concurrently with allocation.  This avoids repeated
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   124
     * postponement of waiting consumers and consequent element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   125
     * build-up. The need to back away from lock during allocation
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   126
     * makes it impossible to simply wrap delegated
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   127
     * java.util.PriorityQueue operations within a lock, as was done
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   128
     * in a previous version of this class. To maintain
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   129
     * interoperability, a plain PriorityQueue is still used during
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   130
     * serialization, which maintains compatibility at the expense of
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   131
     * transiently doubling overhead.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   132
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   133
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   134
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   135
     * Default array capacity.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   136
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   137
    private static final int DEFAULT_INITIAL_CAPACITY = 11;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   138
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   139
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   140
     * The maximum size of array to allocate.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   141
     * Some VMs reserve some header words in an array.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   142
     * Attempts to allocate larger arrays may result in
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   143
     * OutOfMemoryError: Requested array size exceeds VM limit
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   144
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   145
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   148
     * Priority queue represented as a balanced binary heap: the two
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   149
     * children of queue[n] are queue[2*n+1] and queue[2*(n+1)].  The
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   150
     * priority queue is ordered by comparator, or by the elements'
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   151
     * natural ordering, if comparator is null: For each node n in the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   152
     * heap and each descendant d of n, n <= d.  The element with the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   153
     * lowest value is in queue[0], assuming the queue is nonempty.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   154
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   155
    private transient Object[] queue;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   156
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   157
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   158
     * The number of elements in the priority queue.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   159
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   160
    private transient int size;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   161
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   162
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   163
     * The comparator, or null if priority queue uses elements'
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   164
     * natural ordering.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   165
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   166
    private transient Comparator<? super E> comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   167
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   168
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   169
     * Lock used for all public operations.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   170
     */
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   171
    private final ReentrantLock lock = new ReentrantLock();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   172
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   173
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   174
     * Condition for blocking when empty.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   175
     */
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52427
diff changeset
   176
    @SuppressWarnings("serial") // Classes implementing Condition may be serializable.
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   177
    private final Condition notEmpty = lock.newCondition();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   178
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   179
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   180
     * Spinlock for allocation, acquired via CAS.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   181
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   182
    private transient volatile int allocationSpinLock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   183
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   184
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   185
     * A plain PriorityQueue used only for serialization,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   186
     * to maintain compatibility with previous versions
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   187
     * of this class. Non-null only during serialization/deserialization.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   188
     */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   189
    private PriorityQueue<E> q;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   190
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   191
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   192
     * Creates a {@code PriorityBlockingQueue} with the default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * initial capacity (11) that orders its elements according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * their {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public PriorityBlockingQueue() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   197
        this(DEFAULT_INITIAL_CAPACITY, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
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
     * Creates a {@code PriorityBlockingQueue} with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * initial capacity that orders its elements according to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param initialCapacity the initial capacity for this priority queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   206
     * @throws IllegalArgumentException if {@code initialCapacity} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public PriorityBlockingQueue(int initialCapacity) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   210
        this(initialCapacity, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   214
     * Creates a {@code PriorityBlockingQueue} with the specified initial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * capacity that orders its elements according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param initialCapacity the initial capacity for this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param  comparator the comparator that will be used to order this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *         priority queue.  If {@code null}, the {@linkplain Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *         natural ordering} of the elements will be used.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   222
     * @throws IllegalArgumentException if {@code initialCapacity} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public PriorityBlockingQueue(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                 Comparator<? super E> comparator) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   227
        if (initialCapacity < 1)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   228
            throw new IllegalArgumentException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   229
        this.comparator = comparator;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   230
        this.queue = new Object[Math.max(1, initialCapacity)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   234
     * Creates a {@code PriorityBlockingQueue} containing the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * in the specified collection.  If the specified collection is a
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   236
     * {@link SortedSet} or a {@link PriorityQueue}, this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * priority queue will be ordered according to the same ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Otherwise, this priority queue will be ordered according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * {@linkplain Comparable natural ordering} of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param  c the collection whose elements are to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *         into this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws ClassCastException if elements of the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         cannot be compared to one another according to the priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public PriorityBlockingQueue(Collection<? extends E> c) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   250
        boolean heapify = true; // true if not known to be in heap order
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   251
        boolean screen = true;  // true if must screen for nulls
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   252
        if (c instanceof SortedSet<?>) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   253
            SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   254
            this.comparator = (Comparator<? super E>) ss.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   255
            heapify = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   256
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   257
        else if (c instanceof PriorityBlockingQueue<?>) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   258
            PriorityBlockingQueue<? extends E> pq =
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   259
                (PriorityBlockingQueue<? extends E>) c;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   260
            this.comparator = (Comparator<? super E>) pq.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   261
            screen = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   262
            if (pq.getClass() == PriorityBlockingQueue.class) // exact match
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   263
                heapify = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   264
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   265
        Object[] es = c.toArray();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   266
        int n = es.length;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   267
        // If c.toArray incorrectly doesn't return Object[], copy it.
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   268
        if (es.getClass() != Object[].class)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   269
            es = Arrays.copyOf(es, n, Object[].class);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   270
        if (screen && (n == 1 || this.comparator != null)) {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   271
            for (Object e : es)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   272
                if (e == null)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   273
                    throw new NullPointerException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   274
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   275
        this.queue = ensureNonEmpty(es);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   276
        this.size = n;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   277
        if (heapify)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   278
            heapify();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   279
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   280
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   281
    /** Ensures that queue[0] exists, helping peek() and poll(). */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   282
    private static Object[] ensureNonEmpty(Object[] es) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   283
        return (es.length > 0) ? es : new Object[1];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   284
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   285
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   286
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   287
     * Tries to grow array to accommodate at least one more element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   288
     * (but normally expand by about 50%), giving up (allowing retry)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   289
     * on contention (which we expect to be rare). Call only while
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   290
     * holding lock.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   291
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   292
     * @param array the heap array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   293
     * @param oldCap the length of the array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   294
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   295
    private void tryGrow(Object[] array, int oldCap) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   296
        lock.unlock(); // must release and then re-acquire main lock
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   297
        Object[] newArray = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   298
        if (allocationSpinLock == 0 &&
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
   299
            ALLOCATIONSPINLOCK.compareAndSet(this, 0, 1)) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   300
            try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   301
                int newCap = oldCap + ((oldCap < 64) ?
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   302
                                       (oldCap + 2) : // grow faster if small
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   303
                                       (oldCap >> 1));
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   304
                if (newCap - MAX_ARRAY_SIZE > 0) {    // possible overflow
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   305
                    int minCap = oldCap + 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   306
                    if (minCap < 0 || minCap > MAX_ARRAY_SIZE)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   307
                        throw new OutOfMemoryError();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   308
                    newCap = MAX_ARRAY_SIZE;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   309
                }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   310
                if (newCap > oldCap && queue == array)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   311
                    newArray = new Object[newCap];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   312
            } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   313
                allocationSpinLock = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   314
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   315
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   316
        if (newArray == null) // back off if another thread is allocating
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   317
            Thread.yield();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   318
        lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   319
        if (newArray != null && queue == array) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   320
            queue = newArray;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   321
            System.arraycopy(array, 0, newArray, 0, oldCap);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   322
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   323
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   324
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   325
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   326
     * Mechanics for poll().  Call only while holding lock.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   327
     */
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   328
    private E dequeue() {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   329
        // assert lock.isHeldByCurrentThread();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   330
        final Object[] es;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   331
        final E result;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   332
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   333
        if ((result = (E) ((es = queue)[0])) != null) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   334
            final int n;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   335
            final E x = (E) es[(n = --size)];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   336
            es[n] = null;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   337
            if (n > 0) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   338
                final Comparator<? super E> cmp;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   339
                if ((cmp = comparator) == null)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   340
                    siftDownComparable(0, x, es, n);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   341
                else
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   342
                    siftDownUsingComparator(0, x, es, n, cmp);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   343
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   344
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   345
        return result;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   346
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   347
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   348
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   349
     * Inserts item x at position k, maintaining heap invariant by
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   350
     * promoting x up the tree until it is greater than or equal to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   351
     * its parent, or is the root.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   352
     *
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   353
     * To simplify and speed up coercions and comparisons, the
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   354
     * Comparable and Comparator versions are separated into different
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   355
     * methods that are otherwise identical. (Similarly for siftDown.)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   356
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   357
     * @param k the position to fill
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   358
     * @param x the item to insert
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   359
     * @param es the heap array
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   360
     */
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   361
    private static <T> void siftUpComparable(int k, T x, Object[] es) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   362
        Comparable<? super T> key = (Comparable<? super T>) x;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   363
        while (k > 0) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   364
            int parent = (k - 1) >>> 1;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   365
            Object e = es[parent];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   366
            if (key.compareTo((T) e) >= 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   367
                break;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   368
            es[k] = e;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   369
            k = parent;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   370
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   371
        es[k] = key;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   372
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   373
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   374
    private static <T> void siftUpUsingComparator(
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   375
        int k, T x, Object[] es, Comparator<? super T> cmp) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   376
        while (k > 0) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   377
            int parent = (k - 1) >>> 1;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   378
            Object e = es[parent];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   379
            if (cmp.compare(x, (T) e) >= 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   380
                break;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   381
            es[k] = e;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   382
            k = parent;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   383
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   384
        es[k] = x;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   385
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   386
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   387
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   388
     * Inserts item x at position k, maintaining heap invariant by
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   389
     * demoting x down the tree repeatedly until it is less than or
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   390
     * equal to its children or is a leaf.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   391
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   392
     * @param k the position to fill
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   393
     * @param x the item to insert
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   394
     * @param es the heap array
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   395
     * @param n heap size
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   396
     */
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   397
    private static <T> void siftDownComparable(int k, T x, Object[] es, int n) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   398
        // assert n > 0;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   399
        Comparable<? super T> key = (Comparable<? super T>)x;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   400
        int half = n >>> 1;           // loop while a non-leaf
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   401
        while (k < half) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   402
            int child = (k << 1) + 1; // assume left child is least
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   403
            Object c = es[child];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   404
            int right = child + 1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   405
            if (right < n &&
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   406
                ((Comparable<? super T>) c).compareTo((T) es[right]) > 0)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   407
                c = es[child = right];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   408
            if (key.compareTo((T) c) <= 0)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   409
                break;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   410
            es[k] = c;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   411
            k = child;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   412
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   413
        es[k] = key;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   414
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   415
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   416
    private static <T> void siftDownUsingComparator(
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   417
        int k, T x, Object[] es, int n, Comparator<? super T> cmp) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   418
        // assert n > 0;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   419
        int half = n >>> 1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   420
        while (k < half) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   421
            int child = (k << 1) + 1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   422
            Object c = es[child];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   423
            int right = child + 1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   424
            if (right < n && cmp.compare((T) c, (T) es[right]) > 0)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   425
                c = es[child = right];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   426
            if (cmp.compare(x, (T) c) <= 0)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   427
                break;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   428
            es[k] = c;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   429
            k = child;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   430
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   431
        es[k] = x;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   432
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   433
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   434
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   435
     * Establishes the heap invariant (described above) in the entire tree,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   436
     * assuming nothing about the order of the elements prior to the call.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   437
     * This classic algorithm due to Floyd (1964) is known to be O(size).
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   438
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   439
    private void heapify() {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   440
        final Object[] es = queue;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   441
        int n = size, i = (n >>> 1) - 1;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   442
        final Comparator<? super E> cmp;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   443
        if ((cmp = comparator) == null)
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   444
            for (; i >= 0; i--)
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   445
                siftDownComparable(i, (E) es[i], es, n);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   446
        else
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   447
            for (; i >= 0; i--)
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   448
                siftDownUsingComparator(i, (E) es[i], es, n, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Inserts the specified element into this priority queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   455
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Inserts the specified element into this priority queue.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   467
     * As the queue is unbounded, this method will never return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   470
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public boolean offer(E e) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   477
        if (e == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   478
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        lock.lock();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   481
        int n, cap;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   482
        Object[] es;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   483
        while ((n = size) >= (cap = (es = queue).length))
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   484
            tryGrow(es, cap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   486
            final Comparator<? super E> cmp;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   487
            if ((cmp = comparator) == null)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   488
                siftUpComparable(n, e, es);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   489
            else
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   490
                siftUpUsingComparator(n, e, es, cmp);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   491
            size = n + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   496
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   500
     * Inserts the specified element into this priority queue.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   501
     * As the queue is unbounded, this method will never block.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public void put(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   514
     * Inserts the specified element into this priority queue.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   515
     * As the queue is unbounded, this method will never block or
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   516
     * return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param timeout This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param unit This parameter is ignored as the method never blocks
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   521
     * @return {@code true} (as specified by
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   522
     *  {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public boolean offer(E e, long timeout, TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   536
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        lock.lockInterruptibly();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   545
        E result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   547
            while ( (result = dequeue()) == null)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   548
                notEmpty.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   552
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        lock.lockInterruptibly();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   559
        E result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   561
            while ( (result = dequeue()) == null && nanos > 0)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   562
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   566
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   573
            return (E) queue[0];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   574
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   575
            lock.unlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   576
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   577
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   578
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   579
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   580
     * Returns the comparator used to order the elements in this queue,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   581
     * or {@code null} if this queue uses the {@linkplain Comparable
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   582
     * natural ordering} of its elements.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   583
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   584
     * @return the comparator used to order the elements in this queue,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   585
     *         or {@code null} if this queue uses the natural
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   586
     *         ordering of its elements
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   587
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   588
    public Comparator<? super E> comparator() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   589
        return comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   590
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   591
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   592
    public int size() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   593
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   594
        lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   595
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   596
            return size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   603
     * Always returns {@code Integer.MAX_VALUE} because
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   604
     * a {@code PriorityBlockingQueue} is not capacity constrained.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   605
     * @return {@code Integer.MAX_VALUE} always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   607
    public int remainingCapacity() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   608
        return Integer.MAX_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   611
    private int indexOf(Object o) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   612
        if (o != null) {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   613
            final Object[] es = queue;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   614
            for (int i = 0, n = size; i < n; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   615
                if (o.equals(es[i]))
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   616
                    return i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   618
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   622
     * Removes the ith element from queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   624
    private void removeAt(int i) {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   625
        final Object[] es = queue;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   626
        final int n = size - 1;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   627
        if (n == i) // removed last element
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   628
            es[i] = null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   629
        else {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   630
            E moved = (E) es[n];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   631
            es[n] = null;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   632
            final Comparator<? super E> cmp;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   633
            if ((cmp = comparator) == null)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   634
                siftDownComparable(i, moved, es, n);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   635
            else
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   636
                siftDownUsingComparator(i, moved, es, n, cmp);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   637
            if (es[i] == moved) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   638
                if (cmp == null)
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   639
                    siftUpComparable(i, moved, es);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   640
                else
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   641
                    siftUpUsingComparator(i, moved, es, cmp);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   642
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   643
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   644
        size = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Removes a single instance of the specified element from this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * if it is present.  More formally, removes an element {@code e} such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * that {@code o.equals(e)}, if this queue contains one or more such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * elements.  Returns {@code true} if and only if this queue contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * the specified element (or equivalently, if this queue changed as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param o element to be removed from this queue, if present
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   656
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   662
            int i = indexOf(o);
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   663
            if (i == -1)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   664
                return false;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   665
            removeAt(i);
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   666
            return true;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   667
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   668
            lock.unlock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   669
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   670
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   671
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   672
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   673
     * Identity-based version for use in Itr.remove.
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   674
     *
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   675
     * @param o element to be removed from this queue, if present
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   676
     */
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   677
    void removeEq(Object o) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   678
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   679
        lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   680
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   681
            final Object[] es = queue;
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   682
            for (int i = 0, n = size; i < n; i++) {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   683
                if (o == es[i]) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   684
                    removeAt(i);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   685
                    break;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   686
                }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   687
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Returns {@code true} if this queue contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * More formally, returns {@code true} if and only if this queue contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * at least one element {@code e} such that {@code o.equals(e)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @param o object to be checked for containment in this queue
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   699
     * @return {@code true} if this queue contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   705
            return indexOf(o) != -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public String toString() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   712
        return Helpers.collectionToString(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public int drainTo(Collection<? super E> c) {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   722
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public int drainTo(Collection<? super E> c, int maxElements) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   732
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   740
            int n = Math.min(size, maxElements);
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   741
            for (int i = 0; i < n; i++) {
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   742
                c.add((E) queue[0]); // In this order, in case add() throws.
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   743
                dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   759
            final Object[] es = queue;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   760
            for (int i = 0, n = size; i < n; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   761
                es[i] = null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   762
            size = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   769
     * Returns an array containing all of the elements in this queue.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   770
     * The returned array elements are in no particular order.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   771
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   772
     * <p>The returned array will be "safe" in that no references to it are
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   773
     * maintained by this queue.  (In other words, this method must allocate
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   774
     * a new array).  The caller is thus free to modify the returned array.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   775
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   776
     * <p>This method acts as bridge between array-based and collection-based
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   777
     * APIs.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   778
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   779
     * @return an array containing all of the elements in this queue
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   780
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   781
    public Object[] toArray() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   782
        final ReentrantLock lock = this.lock;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   783
        lock.lock();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   784
        try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   785
            return Arrays.copyOf(queue, size);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   786
        } finally {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   787
            lock.unlock();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   788
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   789
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   790
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   791
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Returns an array containing all of the elements in this queue; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * runtime type of the returned array is that of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * If the queue fits in the specified array, it is returned therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Otherwise, a new array is allocated with the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * the array immediately following the end of the queue is set to
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   802
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   809
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * The following code can be used to dump the queue into a newly
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   811
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   813
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   815
     * Note that {@code toArray(new Object[0])} is identical in function to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   816
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   831
            int n = size;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   832
            if (a.length < n)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   833
                // Make a new array of a's runtime type, but my contents:
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   834
                return (T[]) Arrays.copyOf(queue, size, a.getClass());
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   835
            System.arraycopy(queue, 0, a, 0, n);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   836
            if (a.length > n)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   837
                a[n] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   838
            return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Returns an iterator over the elements in this queue. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * iterator does not return the elements in any particular order.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   847
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   848
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   849
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @return an iterator over the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return new Itr(toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Snapshot iterator that works off copy of underlying q array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   860
    final class Itr implements Iterator<E> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        final Object[] array; // Array of all elements
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   862
        int cursor;           // index of next element to return
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   863
        int lastRet = -1;     // index of last element, or -1 if no such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        Itr(Object[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return cursor < array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (cursor >= array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                throw new NoSuchElementException();
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   876
            return (E)array[lastRet = cursor++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                throw new IllegalStateException();
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   882
            removeEq(array[lastRet]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   885
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   886
        public void forEachRemaining(Consumer<? super E> action) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   887
            Objects.requireNonNull(action);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   888
            final Object[] es = array;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   889
            int i;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   890
            if ((i = cursor) < es.length) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   891
                lastRet = -1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   892
                cursor = es.length;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   893
                for (; i < es.length; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   894
                    action.accept((E) es[i]);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   895
                lastRet = es.length - 1;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   896
            }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   897
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   901
     * Saves this queue to a stream (that is, serializes it).
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   902
     *
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   903
     * For compatibility with previous version of this class, elements
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   904
     * are first copied to a java.util.PriorityQueue, which is then
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   905
     * serialized.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   906
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   907
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   908
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   914
            // avoid zero capacity argument
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   915
            q = new PriorityQueue<E>(Math.max(size, 1), comparator);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   916
            q.addAll(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        } finally {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   919
            q = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   924
    /**
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   925
     * 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
   926
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   927
     * @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
   928
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   929
     * @throws java.io.IOException if an I/O error occurs
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   930
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   931
    private void readObject(java.io.ObjectInputStream s)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   932
        throws java.io.IOException, ClassNotFoundException {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   933
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   934
            s.defaultReadObject();
49792
cfdce76e0449 8189981: Improve queuing portability
smarks
parents: 49565
diff changeset
   935
            int sz = q.size();
cfdce76e0449 8189981: Improve queuing portability
smarks
parents: 49565
diff changeset
   936
            SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, sz);
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   937
            this.queue = new Object[Math.max(1, sz)];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   938
            comparator = q.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   939
            addAll(q);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   940
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   941
            q = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   942
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   943
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   944
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   945
    /**
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   946
     * Immutable snapshot spliterator that binds to elements "late".
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   947
     */
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   948
    final class PBQSpliterator implements Spliterator<E> {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   949
        Object[] array;        // null until late-bound-initialized
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   950
        int index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   951
        int fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   952
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   953
        PBQSpliterator() {}
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   954
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   955
        PBQSpliterator(Object[] array, int index, int fence) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   956
            this.array = array;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   957
            this.index = index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   958
            this.fence = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   959
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   960
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   961
        private int getFence() {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   962
            if (array == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   963
                fence = (array = toArray()).length;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   964
            return fence;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   965
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   966
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   967
        public PBQSpliterator trySplit() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   968
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   969
            return (lo >= mid) ? null :
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   970
                new PBQSpliterator(array, lo, index = mid);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   971
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   972
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   973
        public void forEachRemaining(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   974
            Objects.requireNonNull(action);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   975
            final int hi = getFence(), lo = index;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   976
            final Object[] es = array;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   977
            index = hi;                 // ensure exhaustion
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   978
            for (int i = lo; i < hi; i++)
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
   979
                action.accept((E) es[i]);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   980
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   981
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   982
        public boolean tryAdvance(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   983
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   984
            if (getFence() > index && index >= 0) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   985
                action.accept((E) array[index++]);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   986
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   987
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   988
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   989
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   990
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   991
        public long estimateSize() { return getFence() - index; }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   992
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   993
        public int characteristics() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   994
            return (Spliterator.NONNULL |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   995
                    Spliterator.SIZED |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   996
                    Spliterator.SUBSIZED);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   997
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   998
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   999
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1000
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1001
     * Returns a {@link Spliterator} over the elements in this queue.
42226
f55cb692058f 8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents: 39725
diff changeset
  1002
     * The spliterator does not traverse elements in any particular order
f55cb692058f 8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents: 39725
diff changeset
  1003
     * (the {@link Spliterator#ORDERED ORDERED} characteristic is not reported).
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1004
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1005
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1006
     * <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
  1007
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1008
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1009
     * {@link Spliterator#NONNULL}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1010
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1011
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1012
     * The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1013
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1014
     * @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
  1015
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1016
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1017
    public Spliterator<E> spliterator() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1018
        return new PBQSpliterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1019
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1020
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1021
    /**
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1022
     * @throws NullPointerException {@inheritDoc}
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1023
     */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1024
    public boolean removeIf(Predicate<? super E> filter) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1025
        Objects.requireNonNull(filter);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1026
        return bulkRemove(filter);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1027
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1028
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1029
    /**
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1030
     * @throws NullPointerException {@inheritDoc}
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1031
     */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1032
    public boolean removeAll(Collection<?> c) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1033
        Objects.requireNonNull(c);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1034
        return bulkRemove(e -> c.contains(e));
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1035
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1036
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1037
    /**
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1038
     * @throws NullPointerException {@inheritDoc}
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1039
     */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1040
    public boolean retainAll(Collection<?> c) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1041
        Objects.requireNonNull(c);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1042
        return bulkRemove(e -> !c.contains(e));
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1043
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1044
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1045
    // A tiny bit set implementation
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1046
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1047
    private static long[] nBits(int n) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1048
        return new long[((n - 1) >> 6) + 1];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1049
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1050
    private static void setBit(long[] bits, int i) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1051
        bits[i >> 6] |= 1L << i;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1052
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1053
    private static boolean isClear(long[] bits, int i) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1054
        return (bits[i >> 6] & (1L << i)) == 0;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1055
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1056
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1057
    /** Implementation of bulk remove methods. */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1058
    private boolean bulkRemove(Predicate<? super E> filter) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1059
        final ReentrantLock lock = this.lock;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1060
        lock.lock();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1061
        try {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1062
            final Object[] es = queue;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1063
            final int end = size;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1064
            int i;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1065
            // Optimize for initial run of survivors
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1066
            for (i = 0; i < end && !filter.test((E) es[i]); i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1067
                ;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1068
            if (i >= end)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1069
                return false;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1070
            // Tolerate predicates that reentrantly access the
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1071
            // collection for read, so traverse once to find elements
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1072
            // to delete, a second pass to physically expunge.
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1073
            final int beg = i;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1074
            final long[] deathRow = nBits(end - beg);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1075
            deathRow[0] = 1L;   // set bit 0
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1076
            for (i = beg + 1; i < end; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1077
                if (filter.test((E) es[i]))
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1078
                    setBit(deathRow, i - beg);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1079
            int w = beg;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1080
            for (i = beg; i < end; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1081
                if (isClear(deathRow, i - beg))
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1082
                    es[w++] = es[i];
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1083
            for (i = size = w; i < end; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1084
                es[i] = null;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1085
            heapify();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1086
            return true;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1087
        } finally {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1088
            lock.unlock();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1089
        }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1090
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1091
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1092
    /**
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1093
     * @throws NullPointerException {@inheritDoc}
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1094
     */
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1095
    public void forEach(Consumer<? super E> action) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1096
        Objects.requireNonNull(action);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1097
        final ReentrantLock lock = this.lock;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1098
        lock.lock();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1099
        try {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1100
            final Object[] es = queue;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1101
            for (int i = 0, n = size; i < n; i++)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1102
                action.accept((E) es[i]);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1103
        } finally {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1104
            lock.unlock();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1105
        }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1106
    }
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49792
diff changeset
  1107
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1108
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1109
    private static final VarHandle ALLOCATIONSPINLOCK;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  1110
    static {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1111
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1112
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1113
            ALLOCATIONSPINLOCK = l.findVarHandle(PriorityBlockingQueue.class,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1114
                                                 "allocationSpinLock",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1115
                                                 int.class);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1116
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 49433
diff changeset
  1117
            throw new ExceptionInInitializerError(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1118
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1119
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
}