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