author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 42322 | c3474fef4fe4 |
child 43521 | 60e247b8d9a4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
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 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
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; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
46 |
import java.util.PriorityQueue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
47 |
import java.util.Queue; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
48 |
import java.util.SortedSet; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
49 |
import java.util.Spliterator; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
50 |
import java.util.concurrent.locks.Condition; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
51 |
import java.util.concurrent.locks.ReentrantLock; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
52 |
import java.util.function.Consumer; |
2 | 53 |
|
54 |
/** |
|
55 |
* An unbounded {@linkplain BlockingQueue blocking queue} that uses |
|
56 |
* the same ordering rules as class {@link PriorityQueue} and supplies |
|
57 |
* blocking retrieval operations. While this queue is logically |
|
58 |
* 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
|
59 |
* (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
|
60 |
* {@code null} elements. A priority queue relying on {@linkplain |
2 | 61 |
* Comparable natural ordering} also does not permit insertion of |
62 |
* 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
|
63 |
* {@code ClassCastException}). |
2 | 64 |
* |
65 |
* <p>This class and its iterator implement all of the |
|
66 |
* <em>optional</em> methods of the {@link Collection} and {@link |
|
67 |
* Iterator} interfaces. The Iterator provided in method {@link |
|
42226
f55cb692058f
8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents:
39725
diff
changeset
|
68 |
* #iterator()} and the Spliterator provided in method {@link #spliterator()} |
f55cb692058f
8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents:
39725
diff
changeset
|
69 |
* are <em>not</em> guaranteed to traverse the elements of |
2 | 70 |
* the PriorityBlockingQueue in any particular order. If you need |
71 |
* ordered traversal, consider using |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
72 |
* {@code Arrays.sort(pq.toArray())}. Also, method {@code drainTo} |
2 | 73 |
* can be used to <em>remove</em> some or all elements in priority |
74 |
* order and place them in another collection. |
|
75 |
* |
|
76 |
* <p>Operations on this class make no guarantees about the ordering |
|
77 |
* of elements with equal priority. If you need to enforce an |
|
78 |
* ordering, you can define custom classes or comparators that use a |
|
79 |
* secondary key to break ties in primary priority values. For |
|
80 |
* example, here is a class that applies first-in-first-out |
|
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 | 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 | 88 |
* final long seqNum; |
89 |
* final E entry; |
|
90 |
* public FIFOEntry(E entry) { |
|
91 |
* seqNum = seq.getAndIncrement(); |
|
92 |
* this.entry = entry; |
|
93 |
* } |
|
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 | 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 | 99 |
* return res; |
100 |
* } |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
101 |
* }}</pre> |
2 | 102 |
* |
103 |
* <p>This class is a member of the |
|
104 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
105 |
* Java Collections Framework</a>. |
|
106 |
* |
|
107 |
* @since 1.5 |
|
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 | 110 |
*/ |
11279 | 111 |
@SuppressWarnings("unchecked") |
2 | 112 |
public class PriorityBlockingQueue<E> extends AbstractQueue<E> |
113 |
implements BlockingQueue<E>, java.io.Serializable { |
|
114 |
private static final long serialVersionUID = 5595510919245408276L; |
|
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 | 144 |
|
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 | 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 | 190 |
* initial capacity (11) that orders its elements according to |
191 |
* their {@linkplain Comparable natural ordering}. |
|
192 |
*/ |
|
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 | 195 |
} |
196 |
||
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 | 199 |
* initial capacity that orders its elements according to their |
200 |
* {@linkplain Comparable natural ordering}. |
|
201 |
* |
|
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 | 204 |
* than 1 |
205 |
*/ |
|
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 | 208 |
} |
209 |
||
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 | 212 |
* capacity that orders its elements according to the specified |
213 |
* comparator. |
|
214 |
* |
|
215 |
* @param initialCapacity the initial capacity for this priority queue |
|
216 |
* @param comparator the comparator that will be used to order this |
|
217 |
* priority queue. If {@code null}, the {@linkplain Comparable |
|
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 | 220 |
* than 1 |
221 |
*/ |
|
222 |
public PriorityBlockingQueue(int initialCapacity, |
|
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 | 230 |
} |
231 |
||
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 | 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 | 236 |
* priority queue will be ordered according to the same ordering. |
237 |
* Otherwise, this priority queue will be ordered according to the |
|
238 |
* {@linkplain Comparable natural ordering} of its elements. |
|
239 |
* |
|
240 |
* @param c the collection whose elements are to be placed |
|
241 |
* into this priority queue |
|
242 |
* @throws ClassCastException if elements of the specified collection |
|
243 |
* cannot be compared to one another according to the priority |
|
244 |
* queue's ordering |
|
245 |
* @throws NullPointerException if the specified collection or any |
|
246 |
* of its elements are null |
|
247 |
*/ |
|
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)) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
272 |
for (int i = 0; i < n; ++i) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
273 |
if (a[i] == null) |
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; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
440 |
int n = size; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
441 |
int half = (n >>> 1) - 1; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
442 |
Comparator<? super E> cmp = comparator; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
443 |
if (cmp == null) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
444 |
for (int i = half; i >= 0; i--) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
445 |
siftDownComparable(i, (E) array[i], array, n); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
446 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
447 |
else { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
448 |
for (int i = half; i >= 0; i--) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
449 |
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
|
450 |
} |
2 | 451 |
} |
452 |
||
453 |
/** |
|
454 |
* Inserts the specified element into this priority queue. |
|
455 |
* |
|
456 |
* @param e the element to add |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
457 |
* @return {@code true} (as specified by {@link Collection#add}) |
2 | 458 |
* @throws ClassCastException if the specified element cannot be compared |
459 |
* with elements currently in the priority queue according to the |
|
460 |
* priority queue's ordering |
|
461 |
* @throws NullPointerException if the specified element is null |
|
462 |
*/ |
|
463 |
public boolean add(E e) { |
|
464 |
return offer(e); |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* 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
|
469 |
* As the queue is unbounded, this method will never return {@code false}. |
2 | 470 |
* |
471 |
* @param e the element to add |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
472 |
* @return {@code true} (as specified by {@link Queue#offer}) |
2 | 473 |
* @throws ClassCastException if the specified element cannot be compared |
474 |
* with elements currently in the priority queue according to the |
|
475 |
* priority queue's ordering |
|
476 |
* @throws NullPointerException if the specified element is null |
|
477 |
*/ |
|
478 |
public boolean offer(E e) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
479 |
if (e == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
480 |
throw new NullPointerException(); |
2 | 481 |
final ReentrantLock lock = this.lock; |
482 |
lock.lock(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
483 |
int n, cap; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
484 |
Object[] array; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
485 |
while ((n = size) >= (cap = (array = queue).length)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
486 |
tryGrow(array, cap); |
2 | 487 |
try { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
488 |
Comparator<? super E> cmp = comparator; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
489 |
if (cmp == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
490 |
siftUpComparable(n, e, array); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
491 |
else |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
492 |
siftUpUsingComparator(n, e, array, cmp); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
493 |
size = n + 1; |
2 | 494 |
notEmpty.signal(); |
495 |
} finally { |
|
496 |
lock.unlock(); |
|
497 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
498 |
return true; |
2 | 499 |
} |
500 |
||
501 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
502 |
* 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
|
503 |
* As the queue is unbounded, this method will never block. |
2 | 504 |
* |
505 |
* @param e the element to add |
|
506 |
* @throws ClassCastException if the specified element cannot be compared |
|
507 |
* with elements currently in the priority queue according to the |
|
508 |
* priority queue's ordering |
|
509 |
* @throws NullPointerException if the specified element is null |
|
510 |
*/ |
|
511 |
public void put(E e) { |
|
512 |
offer(e); // never need to block |
|
513 |
} |
|
514 |
||
515 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
516 |
* 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
|
517 |
* 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
|
518 |
* return {@code false}. |
2 | 519 |
* |
520 |
* @param e the element to add |
|
521 |
* @param timeout This parameter is ignored as the method never blocks |
|
522 |
* @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
|
523 |
* @return {@code true} (as specified by |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
524 |
* {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer}) |
2 | 525 |
* @throws ClassCastException if the specified element cannot be compared |
526 |
* with elements currently in the priority queue according to the |
|
527 |
* priority queue's ordering |
|
528 |
* @throws NullPointerException if the specified element is null |
|
529 |
*/ |
|
530 |
public boolean offer(E e, long timeout, TimeUnit unit) { |
|
531 |
return offer(e); // never need to block |
|
532 |
} |
|
533 |
||
534 |
public E poll() { |
|
535 |
final ReentrantLock lock = this.lock; |
|
536 |
lock.lock(); |
|
537 |
try { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
538 |
return dequeue(); |
2 | 539 |
} finally { |
540 |
lock.unlock(); |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
public E take() throws InterruptedException { |
|
545 |
final ReentrantLock lock = this.lock; |
|
546 |
lock.lockInterruptibly(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
547 |
E result; |
2 | 548 |
try { |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
549 |
while ( (result = dequeue()) == null) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
550 |
notEmpty.await(); |
2 | 551 |
} finally { |
552 |
lock.unlock(); |
|
553 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
554 |
return result; |
2 | 555 |
} |
556 |
||
557 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
558 |
long nanos = unit.toNanos(timeout); |
|
559 |
final ReentrantLock lock = this.lock; |
|
560 |
lock.lockInterruptibly(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
561 |
E result; |
2 | 562 |
try { |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
563 |
while ( (result = dequeue()) == null && nanos > 0) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
564 |
nanos = notEmpty.awaitNanos(nanos); |
2 | 565 |
} finally { |
566 |
lock.unlock(); |
|
567 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
568 |
return result; |
2 | 569 |
} |
570 |
||
571 |
public E peek() { |
|
572 |
final ReentrantLock lock = this.lock; |
|
573 |
lock.lock(); |
|
574 |
try { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
575 |
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
|
576 |
} finally { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
577 |
lock.unlock(); |
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 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
582 |
* 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
|
583 |
* 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
|
584 |
* natural ordering} of its elements. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
585 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
586 |
* @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
|
587 |
* 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
|
588 |
* ordering of its elements |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
589 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
590 |
public Comparator<? super E> comparator() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
591 |
return comparator; |
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 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
594 |
public int size() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
595 |
final ReentrantLock lock = this.lock; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
596 |
lock.lock(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
597 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
598 |
return size; |
2 | 599 |
} finally { |
600 |
lock.unlock(); |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
605 |
* Always returns {@code Integer.MAX_VALUE} because |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
606 |
* a {@code PriorityBlockingQueue} is not capacity constrained. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
607 |
* @return {@code Integer.MAX_VALUE} always |
2 | 608 |
*/ |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
609 |
public int remainingCapacity() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
610 |
return Integer.MAX_VALUE; |
2 | 611 |
} |
612 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
613 |
private int indexOf(Object o) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
614 |
if (o != null) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
615 |
Object[] array = queue; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
616 |
int n = size; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
617 |
for (int i = 0; i < n; i++) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
618 |
if (o.equals(array[i])) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
619 |
return i; |
2 | 620 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
621 |
return -1; |
2 | 622 |
} |
623 |
||
624 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
625 |
* Removes the ith element from queue. |
2 | 626 |
*/ |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
627 |
private void removeAt(int i) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
628 |
Object[] array = queue; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
629 |
int n = size - 1; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
630 |
if (n == i) // removed last element |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
631 |
array[i] = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
632 |
else { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
633 |
E moved = (E) array[n]; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
634 |
array[n] = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
635 |
Comparator<? super E> cmp = comparator; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
636 |
if (cmp == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
637 |
siftDownComparable(i, moved, array, n); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
638 |
else |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
639 |
siftDownUsingComparator(i, moved, array, n, cmp); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
640 |
if (array[i] == moved) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
641 |
if (cmp == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
642 |
siftUpComparable(i, moved, array); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
643 |
else |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
644 |
siftUpUsingComparator(i, moved, array, cmp); |
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 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
647 |
size = n; |
2 | 648 |
} |
649 |
||
650 |
/** |
|
651 |
* Removes a single instance of the specified element from this queue, |
|
652 |
* if it is present. More formally, removes an element {@code e} such |
|
653 |
* that {@code o.equals(e)}, if this queue contains one or more such |
|
654 |
* elements. Returns {@code true} if and only if this queue contained |
|
655 |
* the specified element (or equivalently, if this queue changed as a |
|
656 |
* result of the call). |
|
657 |
* |
|
658 |
* @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
|
659 |
* @return {@code true} if this queue changed as a result of the call |
2 | 660 |
*/ |
661 |
public boolean remove(Object o) { |
|
662 |
final ReentrantLock lock = this.lock; |
|
663 |
lock.lock(); |
|
664 |
try { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
665 |
int i = indexOf(o); |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
666 |
if (i == -1) |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
667 |
return false; |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
668 |
removeAt(i); |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
669 |
return true; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
670 |
} finally { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
671 |
lock.unlock(); |
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 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
675 |
/** |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
676 |
* 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
|
677 |
*/ |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
678 |
void removeEQ(Object o) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
679 |
final ReentrantLock lock = this.lock; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
680 |
lock.lock(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
681 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
682 |
Object[] array = queue; |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
683 |
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
|
684 |
if (o == array[i]) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
685 |
removeAt(i); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
686 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
687 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
688 |
} |
2 | 689 |
} finally { |
690 |
lock.unlock(); |
|
691 |
} |
|
692 |
} |
|
693 |
||
694 |
/** |
|
695 |
* Returns {@code true} if this queue contains the specified element. |
|
696 |
* More formally, returns {@code true} if and only if this queue contains |
|
697 |
* at least one element {@code e} such that {@code o.equals(e)}. |
|
698 |
* |
|
699 |
* @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
|
700 |
* @return {@code true} if this queue contains the specified element |
2 | 701 |
*/ |
702 |
public boolean contains(Object o) { |
|
703 |
final ReentrantLock lock = this.lock; |
|
704 |
lock.lock(); |
|
705 |
try { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
706 |
return indexOf(o) != -1; |
2 | 707 |
} finally { |
708 |
lock.unlock(); |
|
709 |
} |
|
710 |
} |
|
711 |
||
712 |
public String toString() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
713 |
return Helpers.collectionToString(this); |
2 | 714 |
} |
715 |
||
716 |
/** |
|
717 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
718 |
* @throws ClassCastException {@inheritDoc} |
|
719 |
* @throws NullPointerException {@inheritDoc} |
|
720 |
* @throws IllegalArgumentException {@inheritDoc} |
|
721 |
*/ |
|
722 |
public int drainTo(Collection<? super E> c) { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
723 |
return drainTo(c, Integer.MAX_VALUE); |
2 | 724 |
} |
725 |
||
726 |
/** |
|
727 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
728 |
* @throws ClassCastException {@inheritDoc} |
|
729 |
* @throws NullPointerException {@inheritDoc} |
|
730 |
* @throws IllegalArgumentException {@inheritDoc} |
|
731 |
*/ |
|
732 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
733 |
if (c == null) |
|
734 |
throw new NullPointerException(); |
|
735 |
if (c == this) |
|
736 |
throw new IllegalArgumentException(); |
|
737 |
if (maxElements <= 0) |
|
738 |
return 0; |
|
739 |
final ReentrantLock lock = this.lock; |
|
740 |
lock.lock(); |
|
741 |
try { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
742 |
int n = Math.min(size, maxElements); |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
743 |
for (int i = 0; i < n; i++) { |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
744 |
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
|
745 |
dequeue(); |
2 | 746 |
} |
747 |
return n; |
|
748 |
} finally { |
|
749 |
lock.unlock(); |
|
750 |
} |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* Atomically removes all of the elements from this queue. |
|
755 |
* The queue will be empty after this call returns. |
|
756 |
*/ |
|
757 |
public void clear() { |
|
758 |
final ReentrantLock lock = this.lock; |
|
759 |
lock.lock(); |
|
760 |
try { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
761 |
Object[] array = queue; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
762 |
int n = size; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
763 |
size = 0; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
764 |
for (int i = 0; i < n; i++) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
765 |
array[i] = null; |
2 | 766 |
} finally { |
767 |
lock.unlock(); |
|
768 |
} |
|
769 |
} |
|
770 |
||
771 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
772 |
* 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
|
773 |
* The returned array elements are in no particular order. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
774 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
775 |
* <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
|
776 |
* 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
|
777 |
* 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
|
778 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
779 |
* <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
|
780 |
* APIs. |
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 |
* @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
|
783 |
*/ |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
784 |
public Object[] toArray() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
785 |
final ReentrantLock lock = this.lock; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
786 |
lock.lock(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
787 |
try { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
788 |
return Arrays.copyOf(queue, size); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
789 |
} finally { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
790 |
lock.unlock(); |
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 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
793 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
794 |
/** |
2 | 795 |
* Returns an array containing all of the elements in this queue; the |
796 |
* runtime type of the returned array is that of the specified array. |
|
797 |
* The returned array elements are in no particular order. |
|
798 |
* If the queue fits in the specified array, it is returned therein. |
|
799 |
* Otherwise, a new array is allocated with the runtime type of the |
|
800 |
* specified array and the size of this queue. |
|
801 |
* |
|
802 |
* <p>If this queue fits in the specified array with room to spare |
|
803 |
* (i.e., the array has more elements than this queue), the element in |
|
804 |
* 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
|
805 |
* {@code null}. |
2 | 806 |
* |
807 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
808 |
* array-based and collection-based APIs. Further, this method allows |
|
809 |
* precise control over the runtime type of the output array, and may, |
|
810 |
* under certain circumstances, be used to save allocation costs. |
|
811 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
812 |
* <p>Suppose {@code x} is a queue known to contain only strings. |
2 | 813 |
* 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
|
814 |
* allocated array of {@code String}: |
2 | 815 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
816 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
2 | 817 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
818 |
* 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
|
819 |
* {@code toArray()}. |
2 | 820 |
* |
821 |
* @param a the array into which the elements of the queue are to |
|
822 |
* be stored, if it is big enough; otherwise, a new array of the |
|
823 |
* same runtime type is allocated for this purpose |
|
824 |
* @return an array containing all of the elements in this queue |
|
825 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
826 |
* is not a supertype of the runtime type of every element in |
|
827 |
* this queue |
|
828 |
* @throws NullPointerException if the specified array is null |
|
829 |
*/ |
|
830 |
public <T> T[] toArray(T[] a) { |
|
831 |
final ReentrantLock lock = this.lock; |
|
832 |
lock.lock(); |
|
833 |
try { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
834 |
int n = size; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
835 |
if (a.length < n) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
836 |
// 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
|
837 |
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
|
838 |
System.arraycopy(queue, 0, a, 0, n); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
839 |
if (a.length > n) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
840 |
a[n] = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
841 |
return a; |
2 | 842 |
} finally { |
843 |
lock.unlock(); |
|
844 |
} |
|
845 |
} |
|
846 |
||
847 |
/** |
|
848 |
* Returns an iterator over the elements in this queue. The |
|
849 |
* 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
|
850 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
851 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
852 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
2 | 853 |
* |
854 |
* @return an iterator over the elements in this queue |
|
855 |
*/ |
|
856 |
public Iterator<E> iterator() { |
|
857 |
return new Itr(toArray()); |
|
858 |
} |
|
859 |
||
860 |
/** |
|
861 |
* Snapshot iterator that works off copy of underlying q array. |
|
862 |
*/ |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
863 |
final class Itr implements Iterator<E> { |
2 | 864 |
final Object[] array; // Array of all elements |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
865 |
int cursor; // index of next element to return |
2 | 866 |
int lastRet; // index of last element, or -1 if no such |
867 |
||
868 |
Itr(Object[] array) { |
|
869 |
lastRet = -1; |
|
870 |
this.array = array; |
|
871 |
} |
|
872 |
||
873 |
public boolean hasNext() { |
|
874 |
return cursor < array.length; |
|
875 |
} |
|
876 |
||
877 |
public E next() { |
|
878 |
if (cursor >= array.length) |
|
879 |
throw new NoSuchElementException(); |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
880 |
return (E)array[lastRet = cursor++]; |
2 | 881 |
} |
882 |
||
883 |
public void remove() { |
|
884 |
if (lastRet < 0) |
|
885 |
throw new IllegalStateException(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
886 |
removeEQ(array[lastRet]); |
2 | 887 |
lastRet = -1; |
888 |
} |
|
889 |
} |
|
890 |
||
891 |
/** |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
892 |
* 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
|
893 |
* |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
894 |
* For compatibility with previous version of this class, elements |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
895 |
* 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
|
896 |
* serialized. |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
897 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
898 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
899 |
* @throws java.io.IOException if an I/O error occurs |
2 | 900 |
*/ |
901 |
private void writeObject(java.io.ObjectOutputStream s) |
|
902 |
throws java.io.IOException { |
|
903 |
lock.lock(); |
|
904 |
try { |
|
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
905 |
// avoid zero capacity argument |
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
906 |
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
|
907 |
q.addAll(this); |
2 | 908 |
s.defaultWriteObject(); |
909 |
} finally { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
910 |
q = null; |
2 | 911 |
lock.unlock(); |
912 |
} |
|
913 |
} |
|
914 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
915 |
/** |
13150
17f7fa4a0313
7161229: PriorityBlockingQueue keeps hard reference to last removed element
dholmes
parents:
11279
diff
changeset
|
916 |
* 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
|
917 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
918 |
* @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
|
919 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
920 |
* @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
|
921 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
922 |
private void readObject(java.io.ObjectInputStream s) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
923 |
throws java.io.IOException, ClassNotFoundException { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
924 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
925 |
s.defaultReadObject(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
926 |
this.queue = new Object[q.size()]; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
927 |
comparator = q.comparator(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
928 |
addAll(q); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
929 |
} finally { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
930 |
q = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
931 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
932 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
933 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
42226
diff
changeset
|
934 |
/** |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
42226
diff
changeset
|
935 |
* Immutable snapshot spliterator that binds to elements "late". |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
42226
diff
changeset
|
936 |
*/ |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
937 |
final class PBQSpliterator implements Spliterator<E> { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
938 |
Object[] array; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
939 |
int index; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
940 |
int fence; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
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 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
948 |
final int getFence() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
949 |
int hi; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
950 |
if ((hi = fence) < 0) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
951 |
hi = fence = (array = toArray()).length; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
952 |
return hi; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
953 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
954 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
955 |
public PBQSpliterator trySplit() { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
956 |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
957 |
return (lo >= mid) ? null : |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
958 |
new PBQSpliterator(array, lo, index = mid); |
18767
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 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
961 |
@SuppressWarnings("unchecked") |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
962 |
public void forEachRemaining(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
963 |
Object[] a; int i, hi; // hoist accesses and checks from loop |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
964 |
if (action == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
965 |
throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
966 |
if ((a = array) == null) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
967 |
fence = (a = toArray()).length; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
968 |
if ((hi = fence) <= a.length && |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
969 |
(i = index) >= 0 && i < (index = hi)) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
970 |
do { action.accept((E)a[i]); } while (++i < hi); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
971 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
972 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
973 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
974 |
public boolean tryAdvance(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
975 |
if (action == null) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
976 |
throw new NullPointerException(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
977 |
if (getFence() > index && index >= 0) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
978 |
@SuppressWarnings("unchecked") E e = (E) array[index++]; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
979 |
action.accept(e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
980 |
return true; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
981 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
982 |
return false; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
983 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
984 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
985 |
public long estimateSize() { return getFence() - index; } |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
986 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
987 |
public int characteristics() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
988 |
return Spliterator.NONNULL | Spliterator.SIZED | Spliterator.SUBSIZED; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
989 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
990 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
991 |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
992 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
993 |
* Returns a {@link Spliterator} over the elements in this queue. |
42226
f55cb692058f
8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents:
39725
diff
changeset
|
994 |
* The spliterator does not traverse elements in any particular order |
f55cb692058f
8132964: Spliterator documentation on Priority(Blocking)Queue
psandoz
parents:
39725
diff
changeset
|
995 |
* (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
|
996 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
997 |
* <p>The returned spliterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
998 |
* <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
|
999 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1000 |
* <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
|
1001 |
* {@link Spliterator#NONNULL}. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1002 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1003 |
* @implNote |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1004 |
* 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
|
1005 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1006 |
* @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
|
1007 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1008 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
1009 |
public Spliterator<E> spliterator() { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
1010 |
return new PBQSpliterator(null, 0, -1); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
1011 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
13150
diff
changeset
|
1012 |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1013 |
// VarHandle mechanics |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1014 |
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
|
1015 |
static { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1016 |
try { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1017 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1018 |
ALLOCATIONSPINLOCK = l.findVarHandle(PriorityBlockingQueue.class, |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1019 |
"allocationSpinLock", |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1020 |
int.class); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
1021 |
} catch (ReflectiveOperationException e) { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1022 |
throw new Error(e); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1023 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
1024 |
} |
2 | 1025 |
} |