author | dl |
Sat, 22 Jul 2017 09:18:50 -0700 | |
changeset 45937 | 646816090183 |
parent 44743 | f0bbd698c486 |
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:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
37 |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
38 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
39 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
40 |
import java.util.AbstractQueue; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
41 |
import java.util.Collection; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
42 |
import java.util.Iterator; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
43 |
import java.util.NoSuchElementException; |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
44 |
import java.util.Objects; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.PriorityQueue; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
46 |
import java.util.concurrent.locks.Condition; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
47 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 48 |
|
49 |
/** |
|
50 |
* An unbounded {@linkplain BlockingQueue blocking queue} of |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
51 |
* {@code Delayed} elements, in which an element can only be taken |
2 | 52 |
* when its delay has expired. The <em>head</em> of the queue is that |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
53 |
* {@code Delayed} element whose delay expired furthest in the |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
54 |
* past. If no delay has expired there is no head and {@code poll} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
55 |
* will return {@code null}. Expiration occurs when an element's |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
56 |
* {@code getDelay(TimeUnit.NANOSECONDS)} method returns a value less |
2 | 57 |
* than or equal to zero. Even though unexpired elements cannot be |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
58 |
* removed using {@code take} or {@code poll}, they are otherwise |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
59 |
* treated as normal elements. For example, the {@code size} method |
2 | 60 |
* returns the count of both expired and unexpired elements. |
61 |
* This queue does not permit null elements. |
|
62 |
* |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
63 |
* <p>This class and its iterator implement all of the <em>optional</em> |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
64 |
* methods of the {@link Collection} and {@link Iterator} interfaces. |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
65 |
* The Iterator provided in method {@link #iterator()} is <em>not</em> |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
66 |
* guaranteed to traverse the elements of the DelayQueue in any |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
67 |
* particular order. |
2 | 68 |
* |
69 |
* <p>This class is a member of the |
|
44743
f0bbd698c486
8177789: fix collections framework links to point to java.util package doc
smarks
parents:
43522
diff
changeset
|
70 |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
2 | 71 |
* Java Collections Framework</a>. |
72 |
* |
|
73 |
* @since 1.5 |
|
74 |
* @author Doug Lea |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
75 |
* @param <E> the type of elements held in this queue |
2 | 76 |
*/ |
77 |
public class DelayQueue<E extends Delayed> extends AbstractQueue<E> |
|
78 |
implements BlockingQueue<E> { |
|
79 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
80 |
private final transient ReentrantLock lock = new ReentrantLock(); |
2 | 81 |
private final PriorityQueue<E> q = new PriorityQueue<E>(); |
82 |
||
83 |
/** |
|
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
84 |
* Thread designated to wait for the element at the head of |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
85 |
* the queue. This variant of the Leader-Follower pattern |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
86 |
* (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
87 |
* minimize unnecessary timed waiting. When a thread becomes |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
88 |
* the leader, it waits only for the next delay to elapse, but |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
89 |
* other threads await indefinitely. The leader thread must |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
90 |
* signal some other thread before returning from take() or |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
91 |
* poll(...), unless some other thread becomes leader in the |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
92 |
* interim. Whenever the head of the queue is replaced with |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
93 |
* an element with an earlier expiration time, the leader |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
94 |
* field is invalidated by being reset to null, and some |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
95 |
* waiting thread, but not necessarily the current leader, is |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
96 |
* signalled. So waiting threads must be prepared to acquire |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
97 |
* and lose leadership while waiting. |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
98 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
99 |
private Thread leader; |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
100 |
|
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
101 |
/** |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
102 |
* Condition signalled when a newer element becomes available |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
103 |
* at the head of the queue or a new thread may need to |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
104 |
* become leader. |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
105 |
*/ |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
106 |
private final Condition available = lock.newCondition(); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
107 |
|
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
108 |
/** |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
109 |
* Creates a new {@code DelayQueue} that is initially empty. |
2 | 110 |
*/ |
111 |
public DelayQueue() {} |
|
112 |
||
113 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
114 |
* Creates a {@code DelayQueue} initially containing the elements of the |
2 | 115 |
* given collection of {@link Delayed} instances. |
116 |
* |
|
117 |
* @param c the collection of elements to initially contain |
|
118 |
* @throws NullPointerException if the specified collection or any |
|
119 |
* of its elements are null |
|
120 |
*/ |
|
121 |
public DelayQueue(Collection<? extends E> c) { |
|
122 |
this.addAll(c); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Inserts the specified element into this delay queue. |
|
127 |
* |
|
128 |
* @param e the element to add |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
129 |
* @return {@code true} (as specified by {@link Collection#add}) |
2 | 130 |
* @throws NullPointerException if the specified element is null |
131 |
*/ |
|
132 |
public boolean add(E e) { |
|
133 |
return offer(e); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Inserts the specified element into this delay queue. |
|
138 |
* |
|
139 |
* @param e the element to add |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
140 |
* @return {@code true} |
2 | 141 |
* @throws NullPointerException if the specified element is null |
142 |
*/ |
|
143 |
public boolean offer(E e) { |
|
144 |
final ReentrantLock lock = this.lock; |
|
145 |
lock.lock(); |
|
146 |
try { |
|
147 |
q.offer(e); |
|
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
148 |
if (q.peek() == e) { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
149 |
leader = null; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
150 |
available.signal(); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
151 |
} |
2 | 152 |
return true; |
153 |
} finally { |
|
154 |
lock.unlock(); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Inserts the specified element into this delay queue. As the queue is |
|
160 |
* unbounded this method will never block. |
|
161 |
* |
|
162 |
* @param e the element to add |
|
163 |
* @throws NullPointerException {@inheritDoc} |
|
164 |
*/ |
|
165 |
public void put(E e) { |
|
166 |
offer(e); |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Inserts the specified element into this delay queue. As the queue is |
|
171 |
* unbounded this method will never block. |
|
172 |
* |
|
173 |
* @param e the element to add |
|
174 |
* @param timeout This parameter is ignored as the method never blocks |
|
175 |
* @param unit This parameter is ignored as the method never blocks |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
176 |
* @return {@code true} |
2 | 177 |
* @throws NullPointerException {@inheritDoc} |
178 |
*/ |
|
179 |
public boolean offer(E e, long timeout, TimeUnit unit) { |
|
180 |
return offer(e); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
184 |
* Retrieves and removes the head of this queue, or returns {@code null} |
2 | 185 |
* if this queue has no elements with an expired delay. |
186 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
187 |
* @return the head of this queue, or {@code null} if this |
2 | 188 |
* queue has no elements with an expired delay |
189 |
*/ |
|
190 |
public E poll() { |
|
191 |
final ReentrantLock lock = this.lock; |
|
192 |
lock.lock(); |
|
193 |
try { |
|
194 |
E first = q.peek(); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
195 |
return (first == null || first.getDelay(NANOSECONDS) > 0) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
196 |
? null |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
197 |
: q.poll(); |
2 | 198 |
} finally { |
199 |
lock.unlock(); |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Retrieves and removes the head of this queue, waiting if necessary |
|
205 |
* until an element with an expired delay is available on this queue. |
|
206 |
* |
|
207 |
* @return the head of this queue |
|
208 |
* @throws InterruptedException {@inheritDoc} |
|
209 |
*/ |
|
210 |
public E take() throws InterruptedException { |
|
211 |
final ReentrantLock lock = this.lock; |
|
212 |
lock.lockInterruptibly(); |
|
213 |
try { |
|
214 |
for (;;) { |
|
215 |
E first = q.peek(); |
|
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
216 |
if (first == null) |
2 | 217 |
available.await(); |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
218 |
else { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
219 |
long delay = first.getDelay(NANOSECONDS); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
220 |
if (delay <= 0L) |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
221 |
return q.poll(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
222 |
first = null; // don't retain ref while waiting |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
223 |
if (leader != null) |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
224 |
available.await(); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
225 |
else { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
226 |
Thread thisThread = Thread.currentThread(); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
227 |
leader = thisThread; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
228 |
try { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
229 |
available.awaitNanos(delay); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
230 |
} finally { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
231 |
if (leader == thisThread) |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
232 |
leader = null; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
233 |
} |
2 | 234 |
} |
235 |
} |
|
236 |
} |
|
237 |
} finally { |
|
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
238 |
if (leader == null && q.peek() != null) |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
239 |
available.signal(); |
2 | 240 |
lock.unlock(); |
241 |
} |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Retrieves and removes the head of this queue, waiting if necessary |
|
246 |
* until an element with an expired delay is available on this queue, |
|
247 |
* or the specified wait time expires. |
|
248 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
249 |
* @return the head of this queue, or {@code null} if the |
2 | 250 |
* specified waiting time elapses before an element with |
251 |
* an expired delay becomes available |
|
252 |
* @throws InterruptedException {@inheritDoc} |
|
253 |
*/ |
|
254 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
|
255 |
long nanos = unit.toNanos(timeout); |
|
256 |
final ReentrantLock lock = this.lock; |
|
257 |
lock.lockInterruptibly(); |
|
258 |
try { |
|
259 |
for (;;) { |
|
260 |
E first = q.peek(); |
|
261 |
if (first == null) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
262 |
if (nanos <= 0L) |
2 | 263 |
return null; |
264 |
else |
|
265 |
nanos = available.awaitNanos(nanos); |
|
266 |
} else { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
267 |
long delay = first.getDelay(NANOSECONDS); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
268 |
if (delay <= 0L) |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
269 |
return q.poll(); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
270 |
if (nanos <= 0L) |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
271 |
return null; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
272 |
first = null; // don't retain ref while waiting |
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
273 |
if (nanos < delay || leader != null) |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
274 |
nanos = available.awaitNanos(nanos); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
275 |
else { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
276 |
Thread thisThread = Thread.currentThread(); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
277 |
leader = thisThread; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
278 |
try { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
279 |
long timeLeft = available.awaitNanos(delay); |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
280 |
nanos -= delay - timeLeft; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
281 |
} finally { |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
282 |
if (leader == thisThread) |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
283 |
leader = null; |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
284 |
} |
2 | 285 |
} |
286 |
} |
|
287 |
} |
|
288 |
} finally { |
|
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
289 |
if (leader == null && q.peek() != null) |
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
290 |
available.signal(); |
2 | 291 |
lock.unlock(); |
292 |
} |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Retrieves, but does not remove, the head of this queue, or |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
297 |
* returns {@code null} if this queue is empty. Unlike |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
298 |
* {@code poll}, if no expired elements are available in the queue, |
2 | 299 |
* this method returns the element that will expire next, |
300 |
* if one exists. |
|
301 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
302 |
* @return the head of this queue, or {@code null} if this |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
303 |
* queue is empty |
2 | 304 |
*/ |
305 |
public E peek() { |
|
306 |
final ReentrantLock lock = this.lock; |
|
307 |
lock.lock(); |
|
308 |
try { |
|
309 |
return q.peek(); |
|
310 |
} finally { |
|
311 |
lock.unlock(); |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
public int size() { |
|
316 |
final ReentrantLock lock = this.lock; |
|
317 |
lock.lock(); |
|
318 |
try { |
|
319 |
return q.size(); |
|
320 |
} finally { |
|
321 |
lock.unlock(); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
327 |
* @throws ClassCastException {@inheritDoc} |
|
328 |
* @throws NullPointerException {@inheritDoc} |
|
329 |
* @throws IllegalArgumentException {@inheritDoc} |
|
330 |
*/ |
|
331 |
public int drainTo(Collection<? super E> c) { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
332 |
return drainTo(c, Integer.MAX_VALUE); |
2 | 333 |
} |
334 |
||
335 |
/** |
|
336 |
* @throws UnsupportedOperationException {@inheritDoc} |
|
337 |
* @throws ClassCastException {@inheritDoc} |
|
338 |
* @throws NullPointerException {@inheritDoc} |
|
339 |
* @throws IllegalArgumentException {@inheritDoc} |
|
340 |
*/ |
|
341 |
public int drainTo(Collection<? super E> c, int maxElements) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
342 |
Objects.requireNonNull(c); |
2 | 343 |
if (c == this) |
344 |
throw new IllegalArgumentException(); |
|
345 |
if (maxElements <= 0) |
|
346 |
return 0; |
|
347 |
final ReentrantLock lock = this.lock; |
|
348 |
lock.lock(); |
|
349 |
try { |
|
350 |
int n = 0; |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
351 |
for (E first; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
352 |
n < maxElements |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
353 |
&& (first = q.peek()) != null |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
354 |
&& first.getDelay(NANOSECONDS) <= 0;) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44743
diff
changeset
|
355 |
c.add(first); // In this order, in case add() throws. |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
356 |
q.poll(); |
2 | 357 |
++n; |
358 |
} |
|
359 |
return n; |
|
360 |
} finally { |
|
361 |
lock.unlock(); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Atomically removes all of the elements from this delay queue. |
|
367 |
* The queue will be empty after this call returns. |
|
368 |
* Elements with an unexpired delay are not waited for; they are |
|
369 |
* simply discarded from the queue. |
|
370 |
*/ |
|
371 |
public void clear() { |
|
372 |
final ReentrantLock lock = this.lock; |
|
373 |
lock.lock(); |
|
374 |
try { |
|
375 |
q.clear(); |
|
376 |
} finally { |
|
377 |
lock.unlock(); |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
382 |
* Always returns {@code Integer.MAX_VALUE} because |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
383 |
* a {@code DelayQueue} is not capacity constrained. |
2 | 384 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
385 |
* @return {@code Integer.MAX_VALUE} |
2 | 386 |
*/ |
387 |
public int remainingCapacity() { |
|
388 |
return Integer.MAX_VALUE; |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* Returns an array containing all of the elements in this queue. |
|
393 |
* The returned array elements are in no particular order. |
|
394 |
* |
|
395 |
* <p>The returned array will be "safe" in that no references to it are |
|
396 |
* maintained by this queue. (In other words, this method must allocate |
|
397 |
* a new array). The caller is thus free to modify the returned array. |
|
398 |
* |
|
399 |
* <p>This method acts as bridge between array-based and collection-based |
|
400 |
* APIs. |
|
401 |
* |
|
402 |
* @return an array containing all of the elements in this queue |
|
403 |
*/ |
|
404 |
public Object[] toArray() { |
|
405 |
final ReentrantLock lock = this.lock; |
|
406 |
lock.lock(); |
|
407 |
try { |
|
408 |
return q.toArray(); |
|
409 |
} finally { |
|
410 |
lock.unlock(); |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Returns an array containing all of the elements in this queue; the |
|
416 |
* runtime type of the returned array is that of the specified array. |
|
417 |
* The returned array elements are in no particular order. |
|
418 |
* If the queue fits in the specified array, it is returned therein. |
|
419 |
* Otherwise, a new array is allocated with the runtime type of the |
|
420 |
* specified array and the size of this queue. |
|
421 |
* |
|
422 |
* <p>If this queue fits in the specified array with room to spare |
|
423 |
* (i.e., the array has more elements than this queue), the element in |
|
424 |
* the array immediately following the end of the queue is set to |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
425 |
* {@code null}. |
2 | 426 |
* |
427 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
428 |
* array-based and collection-based APIs. Further, this method allows |
|
429 |
* precise control over the runtime type of the output array, and may, |
|
430 |
* under certain circumstances, be used to save allocation costs. |
|
431 |
* |
|
432 |
* <p>The following code can be used to dump a delay queue into a newly |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
433 |
* allocated array of {@code Delayed}: |
2 | 434 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
435 |
* <pre> {@code Delayed[] a = q.toArray(new Delayed[0]);}</pre> |
2 | 436 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
437 |
* Note that {@code toArray(new Object[0])} is identical in function to |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
438 |
* {@code toArray()}. |
2 | 439 |
* |
440 |
* @param a the array into which the elements of the queue are to |
|
441 |
* be stored, if it is big enough; otherwise, a new array of the |
|
442 |
* same runtime type is allocated for this purpose |
|
443 |
* @return an array containing all of the elements in this queue |
|
444 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
445 |
* is not a supertype of the runtime type of every element in |
|
446 |
* this queue |
|
447 |
* @throws NullPointerException if the specified array is null |
|
448 |
*/ |
|
449 |
public <T> T[] toArray(T[] a) { |
|
450 |
final ReentrantLock lock = this.lock; |
|
451 |
lock.lock(); |
|
452 |
try { |
|
453 |
return q.toArray(a); |
|
454 |
} finally { |
|
455 |
lock.unlock(); |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* Removes a single instance of the specified element from this |
|
461 |
* queue, if it is present, whether or not it has expired. |
|
462 |
*/ |
|
463 |
public boolean remove(Object o) { |
|
464 |
final ReentrantLock lock = this.lock; |
|
465 |
lock.lock(); |
|
466 |
try { |
|
467 |
return q.remove(o); |
|
468 |
} finally { |
|
469 |
lock.unlock(); |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
474 |
* Identity-based version for use in Itr.remove. |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
475 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
476 |
void removeEQ(Object o) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
477 |
final ReentrantLock lock = this.lock; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
478 |
lock.lock(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
479 |
try { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
480 |
for (Iterator<E> it = q.iterator(); it.hasNext(); ) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
481 |
if (o == it.next()) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
482 |
it.remove(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
483 |
break; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
484 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
485 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
486 |
} finally { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
487 |
lock.unlock(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
488 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
489 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
490 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
491 |
/** |
2 | 492 |
* Returns an iterator over all the elements (both expired and |
493 |
* unexpired) in this queue. The iterator does not return the |
|
7518 | 494 |
* elements in any particular order. |
495 |
* |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
496 |
* <p>The returned iterator is |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
497 |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
2 | 498 |
* |
499 |
* @return an iterator over the elements in this queue |
|
500 |
*/ |
|
501 |
public Iterator<E> iterator() { |
|
502 |
return new Itr(toArray()); |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* Snapshot iterator that works off copy of underlying q array. |
|
507 |
*/ |
|
508 |
private class Itr implements Iterator<E> { |
|
509 |
final Object[] array; // Array of all elements |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
510 |
int cursor; // index of next element to return |
2 | 511 |
int lastRet; // index of last element, or -1 if no such |
512 |
||
513 |
Itr(Object[] array) { |
|
514 |
lastRet = -1; |
|
515 |
this.array = array; |
|
516 |
} |
|
517 |
||
518 |
public boolean hasNext() { |
|
519 |
return cursor < array.length; |
|
520 |
} |
|
521 |
||
60
429af02854d2
6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
2
diff
changeset
|
522 |
@SuppressWarnings("unchecked") |
2 | 523 |
public E next() { |
524 |
if (cursor >= array.length) |
|
525 |
throw new NoSuchElementException(); |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
526 |
return (E)array[lastRet = cursor++]; |
2 | 527 |
} |
528 |
||
529 |
public void remove() { |
|
530 |
if (lastRet < 0) |
|
531 |
throw new IllegalStateException(); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
11279
diff
changeset
|
532 |
removeEQ(array[lastRet]); |
2 | 533 |
lastRet = -1; |
534 |
} |
|
535 |
} |
|
536 |
||
537 |
} |