src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java
author dl
Tue, 10 Apr 2018 11:37:18 -0700
changeset 49565 b5705ade8c8d
parent 49433 b6671a111395
child 49792 cfdce76e0449
permissions -rw-r--r--
8197531: Miscellaneous changes imported from jsr166 CVS 2018-04 Reviewed-by: martin, psandoz
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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * An unbounded {@linkplain BlockingQueue blocking queue} that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the same ordering rules as class {@link PriorityQueue} and supplies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * blocking retrieval operations.  While this queue is logically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * 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
    60
 * (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
    61
 * {@code null} elements.  A priority queue relying on {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Comparable natural ordering} also does not permit insertion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * 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
    64
 * {@code ClassCastException}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    66
 * <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
    67
 * methods of the {@link Collection} and {@link Iterator} interfaces.
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    68
 * The Iterator provided in method {@link #iterator()} and the
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    69
 * Spliterator provided in method {@link #spliterator()} are <em>not</em>
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    70
 * guaranteed to traverse the elements of the PriorityBlockingQueue in
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    71
 * any particular order. If you need ordered traversal, consider using
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    72
 * {@code Arrays.sort(pq.toArray())}.  Also, method {@code drainTo} can
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
    73
 * 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
    74
 * place them in another collection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>Operations on this class make no guarantees about the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * of elements with equal priority. If you need to enforce an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * ordering, you can define custom classes or comparators that use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * secondary key to break ties in primary priority values.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * example, here is a class that applies first-in-first-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * 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
    82
 * {@code new FIFOEntry(anEntry)} instead of a plain entry object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    84
 * <pre> {@code
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    85
 * class FIFOEntry<E extends Comparable<? super E>>
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    86
 *     implements Comparable<FIFOEntry<E>> {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    87
 *   static final AtomicLong seq = new AtomicLong(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   final long seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   final E entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   public FIFOEntry(E entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     seqNum = seq.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   public E getEntry() { return entry; }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    95
 *   public int compareTo(FIFOEntry<E> other) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     int res = entry.compareTo(other.entry);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    97
 *     if (res == 0 && other.entry != this.entry)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    98
 *       res = (seqNum < other.seqNum ? -1 : 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   101
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <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
   104
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author Doug Lea
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   109
 * @param <E> the type of elements held in this queue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   111
@SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public class PriorityBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final long serialVersionUID = 5595510919245408276L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   116
    /*
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   117
     * 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
   118
     * 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
   119
     * 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
   120
     * 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
   121
     * concurrently with allocation.  This avoids repeated
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   122
     * postponement of waiting consumers and consequent element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   123
     * 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
   124
     * makes it impossible to simply wrap delegated
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   125
     * 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
   126
     * 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
   127
     * interoperability, a plain PriorityQueue is still used during
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   128
     * 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
   129
     * transiently doubling overhead.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   130
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   131
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
     * Default array capacity.
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
    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
   136
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   137
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   138
     * The maximum size of array to allocate.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   139
     * 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
   140
     * 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
   141
     * OutOfMemoryError: Requested array size exceeds VM limit
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   142
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   143
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   146
     * 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
   147
     * 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
   148
     * 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
   149
     * 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
   150
     * 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
   151
     * 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
   152
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   153
    private transient Object[] queue;
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
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   156
     * 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
   157
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   158
    private transient int size;
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
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   161
     * 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
   162
     * natural ordering.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   163
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   164
    private transient Comparator<? super E> comparator;
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
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   167
     * Lock used for all public operations.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   168
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   169
    private final ReentrantLock lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   170
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   171
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   172
     * Condition for blocking when empty.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   173
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   174
    private final Condition notEmpty;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   175
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   176
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   177
     * Spinlock for allocation, acquired via CAS.
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
    private transient volatile int allocationSpinLock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   180
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
     * A plain PriorityQueue used only for serialization,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   183
     * to maintain compatibility with previous versions
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   184
     * 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
   185
     */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9242
diff changeset
   186
    private PriorityQueue<E> q;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   187
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   188
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   189
     * Creates a {@code PriorityBlockingQueue} with the default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * initial capacity (11) that orders its elements according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * their {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public PriorityBlockingQueue() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   194
        this(DEFAULT_INITIAL_CAPACITY, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   198
     * Creates a {@code PriorityBlockingQueue} with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * initial capacity that orders its elements according to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @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
   203
     * @throws IllegalArgumentException if {@code initialCapacity} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public PriorityBlockingQueue(int initialCapacity) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   207
        this(initialCapacity, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   211
     * Creates a {@code PriorityBlockingQueue} with the specified initial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * capacity that orders its elements according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param initialCapacity the initial capacity for this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param  comparator the comparator that will be used to order this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *         priority queue.  If {@code null}, the {@linkplain Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *         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
   219
     * @throws IllegalArgumentException if {@code initialCapacity} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public PriorityBlockingQueue(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                 Comparator<? super E> comparator) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   224
        if (initialCapacity < 1)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   225
            throw new IllegalArgumentException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   226
        this.lock = new ReentrantLock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   227
        this.notEmpty = lock.newCondition();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   228
        this.comparator = comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   229
        this.queue = new Object[initialCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   233
     * Creates a {@code PriorityBlockingQueue} containing the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * 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
   235
     * {@link SortedSet} or a {@link PriorityQueue}, this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * priority queue will be ordered according to the same ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Otherwise, this priority queue will be ordered according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * {@linkplain Comparable natural ordering} of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param  c the collection whose elements are to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         into this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws ClassCastException if elements of the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         cannot be compared to one another according to the priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public PriorityBlockingQueue(Collection<? extends E> c) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   249
        this.lock = new ReentrantLock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   250
        this.notEmpty = lock.newCondition();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   251
        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
   252
        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
   253
        if (c instanceof SortedSet<?>) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   254
            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
   255
            this.comparator = (Comparator<? super E>) ss.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   256
            heapify = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   257
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   258
        else if (c instanceof PriorityBlockingQueue<?>) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   259
            PriorityBlockingQueue<? extends E> pq =
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   260
                (PriorityBlockingQueue<? extends E>) c;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   261
            this.comparator = (Comparator<? super E>) pq.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   262
            screen = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   263
            if (pq.getClass() == PriorityBlockingQueue.class) // exact match
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   264
                heapify = false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   265
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   266
        Object[] a = c.toArray();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   267
        int n = a.length;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   268
        // If c.toArray incorrectly doesn't return Object[], copy it.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   269
        if (a.getClass() != Object[].class)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   270
            a = Arrays.copyOf(a, n, Object[].class);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   271
        if (screen && (n == 1 || this.comparator != null)) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
   272
            for (Object elt : a)
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
   273
                if (elt == null)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   274
                    throw new NullPointerException();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   275
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   276
        this.queue = a;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   277
        this.size = n;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   278
        if (heapify)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   279
            heapify();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   280
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   281
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   282
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   283
     * 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
   284
     * (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
   285
     * 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
   286
     * holding lock.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   287
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   288
     * @param array the heap array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   289
     * @param oldCap the length of the array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   290
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   291
    private void tryGrow(Object[] array, int oldCap) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   292
        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
   293
        Object[] newArray = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   294
        if (allocationSpinLock == 0 &&
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
   295
            ALLOCATIONSPINLOCK.compareAndSet(this, 0, 1)) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   296
            try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   297
                int newCap = oldCap + ((oldCap < 64) ?
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   298
                                       (oldCap + 2) : // grow faster if small
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   299
                                       (oldCap >> 1));
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   300
                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
   301
                    int minCap = oldCap + 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   302
                    if (minCap < 0 || minCap > MAX_ARRAY_SIZE)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   303
                        throw new OutOfMemoryError();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   304
                    newCap = MAX_ARRAY_SIZE;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   305
                }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   306
                if (newCap > oldCap && queue == array)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   307
                    newArray = new Object[newCap];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   308
            } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   309
                allocationSpinLock = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   310
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   311
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   312
        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
   313
            Thread.yield();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   314
        lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   315
        if (newArray != null && queue == array) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   316
            queue = newArray;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   317
            System.arraycopy(array, 0, newArray, 0, oldCap);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   318
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   319
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   320
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   321
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   322
     * 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
   323
     */
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   324
    private E dequeue() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   325
        int n = size - 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   326
        if (n < 0)
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   327
            return null;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   328
        else {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   329
            Object[] array = queue;
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   330
            E result = (E) array[0];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   331
            E x = (E) array[n];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   332
            array[n] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   333
            Comparator<? super E> cmp = comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   334
            if (cmp == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   335
                siftDownComparable(0, x, array, n);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   336
            else
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   337
                siftDownUsingComparator(0, x, array, n, cmp);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   338
            size = n;
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   339
            return result;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   340
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   341
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   342
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   343
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   344
     * 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
   345
     * 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
   346
     * its parent, or is the root.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   347
     *
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   348
     * 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
   349
     * 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
   350
     * 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
   351
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   352
     * @param k the position to fill
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   353
     * @param x the item to insert
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   354
     * @param array the heap array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   355
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   356
    private static <T> void siftUpComparable(int k, T x, Object[] array) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   357
        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
   358
        while (k > 0) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   359
            int parent = (k - 1) >>> 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   360
            Object e = array[parent];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   361
            if (key.compareTo((T) e) >= 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   362
                break;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   363
            array[k] = e;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   364
            k = parent;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   365
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   366
        array[k] = key;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   367
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   368
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   369
    private static <T> void siftUpUsingComparator(int k, T x, Object[] array,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   370
                                       Comparator<? super T> cmp) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   371
        while (k > 0) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   372
            int parent = (k - 1) >>> 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   373
            Object e = array[parent];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   374
            if (cmp.compare(x, (T) e) >= 0)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   375
                break;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   376
            array[k] = e;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   377
            k = parent;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   378
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   379
        array[k] = x;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   380
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   381
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   382
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   383
     * 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
   384
     * 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
   385
     * 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
   386
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   387
     * @param k the position to fill
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   388
     * @param x the item to insert
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   389
     * @param array the heap array
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   390
     * @param n heap size
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
    private static <T> void siftDownComparable(int k, T x, Object[] array,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   393
                                               int n) {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   394
        if (n > 0) {
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   395
            Comparable<? super T> key = (Comparable<? super T>)x;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   396
            int half = n >>> 1;           // loop while a non-leaf
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   397
            while (k < half) {
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   398
                int child = (k << 1) + 1; // assume left child is least
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   399
                Object c = array[child];
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   400
                int right = child + 1;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   401
                if (right < n &&
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   402
                    ((Comparable<? super T>) c).compareTo((T) array[right]) > 0)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   403
                    c = array[child = right];
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   404
                if (key.compareTo((T) c) <= 0)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   405
                    break;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   406
                array[k] = c;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   407
                k = child;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   408
            }
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   409
            array[k] = key;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   410
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   411
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   412
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   413
    private static <T> void siftDownUsingComparator(int k, T x, Object[] array,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   414
                                                    int n,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   415
                                                    Comparator<? super T> cmp) {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   416
        if (n > 0) {
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   417
            int half = n >>> 1;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   418
            while (k < half) {
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   419
                int child = (k << 1) + 1;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   420
                Object c = array[child];
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   421
                int right = child + 1;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   422
                if (right < n && cmp.compare((T) c, (T) array[right]) > 0)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   423
                    c = array[child = right];
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   424
                if (cmp.compare(x, (T) c) <= 0)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   425
                    break;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   426
                array[k] = c;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   427
                k = child;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   428
            }
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   429
            array[k] = x;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   430
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   431
    }
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
     * 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
   435
     * 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
   436
     * 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
   437
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   438
    private void heapify() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   439
        Object[] array = queue;
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   440
        int n = size, i = (n >>> 1) - 1;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   441
        Comparator<? super E> cmp = comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   442
        if (cmp == null) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   443
            for (; i >= 0; i--)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   444
                siftDownComparable(i, (E) array[i], array, n);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   445
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   446
        else {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   447
            for (; i >= 0; i--)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   448
                siftDownUsingComparator(i, (E) array[i], array, n, cmp);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   449
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Inserts the specified element into this priority queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   456
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * 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
   468
     * As the queue is unbounded, this method will never return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param e the element to add
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   471
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public boolean offer(E e) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   478
        if (e == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   479
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        lock.lock();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   482
        int n, cap;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   483
        Object[] array;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   484
        while ((n = size) >= (cap = (array = queue).length))
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   485
            tryGrow(array, cap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   487
            Comparator<? super E> cmp = comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   488
            if (cmp == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   489
                siftUpComparable(n, e, array);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   490
            else
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   491
                siftUpUsingComparator(n, e, array, cmp);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   492
            size = n + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   497
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   501
     * 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
   502
     * As the queue is unbounded, this method will never block.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public void put(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   515
     * 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
   516
     * 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
   517
     * return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param timeout This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @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
   522
     * @return {@code true} (as specified by
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   523
     *  {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public boolean offer(E e, long timeout, TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   537
            return dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        lock.lockInterruptibly();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   546
        E result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   548
            while ( (result = dequeue()) == null)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   549
                notEmpty.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   553
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        lock.lockInterruptibly();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   560
        E result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   562
            while ( (result = dequeue()) == null && nanos > 0)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   563
                nanos = notEmpty.awaitNanos(nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   567
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   574
            return (size == 0) ? null : (E) queue[0];
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   575
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   576
            lock.unlock();
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
    /**
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   581
     * 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
   582
     * 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
   583
     * natural ordering} of its elements.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   584
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   585
     * @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
   586
     *         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
   587
     *         ordering of its elements
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   588
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   589
    public Comparator<? super E> comparator() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   590
        return comparator;
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
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   593
    public int size() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   594
        final ReentrantLock lock = this.lock;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   595
        lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   596
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   597
            return size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   604
     * Always returns {@code Integer.MAX_VALUE} because
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   605
     * a {@code PriorityBlockingQueue} is not capacity constrained.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   606
     * @return {@code Integer.MAX_VALUE} always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   608
    public int remainingCapacity() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   609
        return Integer.MAX_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   612
    private int indexOf(Object o) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   613
        if (o != null) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   614
            Object[] array = queue;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   615
            int n = size;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   616
            for (int i = 0; i < n; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   617
                if (o.equals(array[i]))
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   618
                    return i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   620
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
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
     * Removes the ith element from queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   626
    private void removeAt(int i) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   627
        Object[] array = queue;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   628
        int n = size - 1;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   629
        if (n == i) // removed last element
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   630
            array[i] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   631
        else {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   632
            E moved = (E) array[n];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   633
            array[n] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   634
            Comparator<? super E> cmp = comparator;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   635
            if (cmp == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   636
                siftDownComparable(i, moved, array, n);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   637
            else
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   638
                siftDownUsingComparator(i, moved, array, n, cmp);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   639
            if (array[i] == moved) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   640
                if (cmp == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   641
                    siftUpComparable(i, moved, array);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   642
                else
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   643
                    siftUpUsingComparator(i, moved, array, cmp);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   644
            }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   645
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   646
        size = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Removes a single instance of the specified element from this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * if it is present.  More formally, removes an element {@code e} such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * that {@code o.equals(e)}, if this queue contains one or more such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * elements.  Returns {@code true} if and only if this queue contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * the specified element (or equivalently, if this queue changed as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @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
   658
     * @return {@code true} if this queue changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   664
            int i = indexOf(o);
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   665
            if (i == -1)
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   666
                return false;
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   667
            removeAt(i);
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   668
            return true;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   669
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   670
            lock.unlock();
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
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   673
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   674
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   675
     * Identity-based version for use in Itr.remove.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   676
     */
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
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 {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   681
            Object[] array = 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++) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   683
                if (o == array[i]) {
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 {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   759
            Object[] array = queue;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   760
            int n = size;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   761
            size = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   762
            for (int i = 0; i < n; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   763
                array[i] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   770
     * 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
   771
     * The returned array elements are in no particular order.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   772
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   773
     * <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
   774
     * 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
   775
     * 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
   776
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   777
     * <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
   778
     * APIs.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   779
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   780
     * @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
   781
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   782
    public Object[] toArray() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   783
        final ReentrantLock lock = this.lock;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   784
        lock.lock();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   785
        try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   786
            return Arrays.copyOf(queue, size);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   787
        } finally {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   788
            lock.unlock();
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
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   792
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Returns an array containing all of the elements in this queue; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * runtime type of the returned array is that of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * If the queue fits in the specified array, it is returned therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Otherwise, a new array is allocated with the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * 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
   803
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   810
     * <p>Suppose {@code x} is a queue known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * 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
   812
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   814
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   816
     * 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
   817
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        try {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   832
            int n = size;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   833
            if (a.length < n)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   834
                // 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
   835
                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
   836
            System.arraycopy(queue, 0, a, 0, n);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   837
            if (a.length > n)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   838
                a[n] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   839
            return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Returns an iterator over the elements in this queue. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * 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
   848
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   849
     * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   850
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @return an iterator over the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        return new Itr(toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Snapshot iterator that works off copy of underlying q array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   861
    final class Itr implements Iterator<E> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        final Object[] array; // Array of all elements
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   863
        int cursor;           // index of next element to return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int lastRet;          // index of last element, or -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        Itr(Object[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            return cursor < array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if (cursor >= array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                throw new NoSuchElementException();
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   878
            return (E)array[lastRet = cursor++];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                throw new IllegalStateException();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   884
            removeEQ(array[lastRet]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   890
     * 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
   891
     *
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   892
     * For compatibility with previous version of this class, elements
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   893
     * 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
   894
     * serialized.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   895
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   896
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   897
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        try {
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   903
            // avoid zero capacity argument
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   904
            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
   905
            q.addAll(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        } finally {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   908
            q = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   913
    /**
13150
17f7fa4a0313 7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents: 11279
diff changeset
   914
     * 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
   915
     * @param s the stream
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   916
     * @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
   917
     *         could not be found
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   918
     * @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
   919
     */
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   920
    private void readObject(java.io.ObjectInputStream s)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   921
        throws java.io.IOException, ClassNotFoundException {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   922
        try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   923
            s.defaultReadObject();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   924
            this.queue = new Object[q.size()];
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   925
            comparator = q.comparator();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   926
            addAll(q);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   927
        } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   928
            q = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   929
        }
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
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   932
    /**
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   933
     * Immutable snapshot spliterator that binds to elements "late".
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 42226
diff changeset
   934
     */
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   935
    final class PBQSpliterator implements Spliterator<E> {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   936
        Object[] array;        // null until late-bound-initialized
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   937
        int index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   938
        int fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   939
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   940
        PBQSpliterator() {}
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   941
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   942
        PBQSpliterator(Object[] array, int index, int fence) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   943
            this.array = array;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   944
            this.index = index;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   945
            this.fence = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   946
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   947
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   948
        private int getFence() {
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   949
            if (array == null)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   950
                fence = (array = toArray()).length;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   951
            return fence;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   952
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   953
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   954
        public PBQSpliterator trySplit() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   955
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   956
            return (lo >= mid) ? null :
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   957
                new PBQSpliterator(array, lo, index = mid);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   958
        }
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
        public void forEachRemaining(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   961
            Objects.requireNonNull(action);
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   962
            final int hi = getFence(), lo = index;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   963
            final Object[] a = array;
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   964
            index = hi;                 // ensure exhaustion
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   965
            for (int i = lo; i < hi; i++)
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   966
                action.accept((E) a[i]);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   967
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   968
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   969
        public boolean tryAdvance(Consumer<? super E> action) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   970
            Objects.requireNonNull(action);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   971
            if (getFence() > index && index >= 0) {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   972
                action.accept((E) array[index++]);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   973
                return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   974
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   975
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   976
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   977
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   978
        public long estimateSize() { return getFence() - index; }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   979
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   980
        public int characteristics() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   981
            return (Spliterator.NONNULL |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   982
                    Spliterator.SIZED |
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
   983
                    Spliterator.SUBSIZED);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   984
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   985
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
   986
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   987
    /**
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   988
     * Returns a {@link Spliterator} over the elements in this queue.
42226
f55cb692058f 8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents: 39725
diff changeset
   989
     * The spliterator does not traverse elements in any particular order
f55cb692058f 8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents: 39725
diff changeset
   990
     * (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
   991
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   992
     * <p>The returned spliterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   993
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   994
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   995
     * <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
   996
     * {@link Spliterator#NONNULL}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   997
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   998
     * @implNote
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
   999
     * 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
  1000
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1001
     * @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
  1002
     * @since 1.8
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 18767
diff changeset
  1003
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1004
    public Spliterator<E> spliterator() {
43521
60e247b8d9a4 8169748: LinkedTransferQueue bulk remove is O(n^2)
dl
parents: 42927
diff changeset
  1005
        return new PBQSpliterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1006
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 13150
diff changeset
  1007
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1008
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1009
    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
  1010
    static {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1011
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1012
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1013
            ALLOCATIONSPINLOCK = l.findVarHandle(PriorityBlockingQueue.class,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1014
                                                 "allocationSpinLock",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 33674
diff changeset
  1015
                                                 int.class);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1016
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 49433
diff changeset
  1017
            throw new ExceptionInInitializerError(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1018
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1019
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
}