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