author | dl |
Fri, 26 Oct 2012 21:34:24 +0100 | |
changeset 14325 | 622c473a21aa |
parent 11279 | d9dab5ec5044 |
child 18790 | d25399d849bc |
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:
7976
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
11279 | 37 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
38 |
import java.util.concurrent.atomic.AtomicLong; |
|
39 |
import java.util.concurrent.locks.Condition; |
|
40 |
import java.util.concurrent.locks.ReentrantLock; |
|
2 | 41 |
import java.util.*; |
42 |
||
43 |
/** |
|
44 |
* A {@link ThreadPoolExecutor} that can additionally schedule |
|
45 |
* commands to run after a given delay, or to execute |
|
46 |
* periodically. This class is preferable to {@link java.util.Timer} |
|
47 |
* when multiple worker threads are needed, or when the additional |
|
48 |
* flexibility or capabilities of {@link ThreadPoolExecutor} (which |
|
49 |
* this class extends) are required. |
|
50 |
* |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
51 |
* <p>Delayed tasks execute no sooner than they are enabled, but |
2 | 52 |
* without any real-time guarantees about when, after they are |
53 |
* enabled, they will commence. Tasks scheduled for exactly the same |
|
54 |
* execution time are enabled in first-in-first-out (FIFO) order of |
|
55 |
* submission. |
|
56 |
* |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
57 |
* <p>When a submitted task is cancelled before it is run, execution |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
58 |
* is suppressed. By default, such a cancelled task is not |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
59 |
* automatically removed from the work queue until its delay |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
60 |
* elapses. While this enables further inspection and monitoring, it |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
61 |
* may also cause unbounded retention of cancelled tasks. To avoid |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
62 |
* this, set {@link #setRemoveOnCancelPolicy} to {@code true}, which |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
63 |
* causes tasks to be immediately removed from the work queue at |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
64 |
* time of cancellation. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
65 |
* |
4110 | 66 |
* <p>Successive executions of a task scheduled via |
7518 | 67 |
* {@code scheduleAtFixedRate} or |
68 |
* {@code scheduleWithFixedDelay} do not overlap. While different |
|
4110 | 69 |
* executions may be performed by different threads, the effects of |
70 |
* prior executions <a |
|
71 |
* href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
72 |
* those of subsequent ones. |
|
73 |
* |
|
2 | 74 |
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
75 |
* of the inherited tuning methods are not useful for it. In |
|
76 |
* particular, because it acts as a fixed-sized pool using |
|
77 |
* {@code corePoolSize} threads and an unbounded queue, adjustments |
|
78 |
* to {@code maximumPoolSize} have no useful effect. Additionally, it |
|
79 |
* is almost never a good idea to set {@code corePoolSize} to zero or |
|
80 |
* use {@code allowCoreThreadTimeOut} because this may leave the pool |
|
81 |
* without threads to handle tasks once they become eligible to run. |
|
82 |
* |
|
83 |
* <p><b>Extension notes:</b> This class overrides the |
|
84 |
* {@link ThreadPoolExecutor#execute execute} and |
|
85 |
* {@link AbstractExecutorService#submit(Runnable) submit} |
|
86 |
* methods to generate internal {@link ScheduledFuture} objects to |
|
87 |
* control per-task delays and scheduling. To preserve |
|
88 |
* functionality, any further overrides of these methods in |
|
89 |
* subclasses must invoke superclass versions, which effectively |
|
90 |
* disables additional task customization. However, this class |
|
91 |
* provides alternative protected extension method |
|
92 |
* {@code decorateTask} (one version each for {@code Runnable} and |
|
93 |
* {@code Callable}) that can be used to customize the concrete task |
|
94 |
* types used to execute commands entered via {@code execute}, |
|
95 |
* {@code submit}, {@code schedule}, {@code scheduleAtFixedRate}, |
|
96 |
* and {@code scheduleWithFixedDelay}. By default, a |
|
97 |
* {@code ScheduledThreadPoolExecutor} uses a task type extending |
|
98 |
* {@link FutureTask}. However, this may be modified or replaced using |
|
99 |
* subclasses of the form: |
|
100 |
* |
|
101 |
* <pre> {@code |
|
102 |
* public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor { |
|
103 |
* |
|
104 |
* static class CustomTask<V> implements RunnableScheduledFuture<V> { ... } |
|
105 |
* |
|
106 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
|
107 |
* Runnable r, RunnableScheduledFuture<V> task) { |
|
108 |
* return new CustomTask<V>(r, task); |
|
109 |
* } |
|
110 |
* |
|
111 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
|
112 |
* Callable<V> c, RunnableScheduledFuture<V> task) { |
|
113 |
* return new CustomTask<V>(c, task); |
|
114 |
* } |
|
115 |
* // ... add constructors, etc. |
|
116 |
* }}</pre> |
|
117 |
* |
|
118 |
* @since 1.5 |
|
119 |
* @author Doug Lea |
|
120 |
*/ |
|
121 |
public class ScheduledThreadPoolExecutor |
|
122 |
extends ThreadPoolExecutor |
|
123 |
implements ScheduledExecutorService { |
|
124 |
||
125 |
/* |
|
126 |
* This class specializes ThreadPoolExecutor implementation by |
|
127 |
* |
|
128 |
* 1. Using a custom task type, ScheduledFutureTask for |
|
129 |
* tasks, even those that don't require scheduling (i.e., |
|
130 |
* those submitted using ExecutorService execute, not |
|
131 |
* ScheduledExecutorService methods) which are treated as |
|
132 |
* delayed tasks with a delay of zero. |
|
133 |
* |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
134 |
* 2. Using a custom queue (DelayedWorkQueue), a variant of |
2 | 135 |
* unbounded DelayQueue. The lack of capacity constraint and |
136 |
* the fact that corePoolSize and maximumPoolSize are |
|
137 |
* effectively identical simplifies some execution mechanics |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
138 |
* (see delayedExecute) compared to ThreadPoolExecutor. |
2 | 139 |
* |
140 |
* 3. Supporting optional run-after-shutdown parameters, which |
|
141 |
* leads to overrides of shutdown methods to remove and cancel |
|
142 |
* tasks that should NOT be run after shutdown, as well as |
|
143 |
* different recheck logic when task (re)submission overlaps |
|
144 |
* with a shutdown. |
|
145 |
* |
|
146 |
* 4. Task decoration methods to allow interception and |
|
147 |
* instrumentation, which are needed because subclasses cannot |
|
148 |
* otherwise override submit methods to get this effect. These |
|
149 |
* don't have any impact on pool control logic though. |
|
150 |
*/ |
|
151 |
||
152 |
/** |
|
153 |
* False if should cancel/suppress periodic tasks on shutdown. |
|
154 |
*/ |
|
155 |
private volatile boolean continueExistingPeriodicTasksAfterShutdown; |
|
156 |
||
157 |
/** |
|
158 |
* False if should cancel non-periodic tasks on shutdown. |
|
159 |
*/ |
|
160 |
private volatile boolean executeExistingDelayedTasksAfterShutdown = true; |
|
161 |
||
162 |
/** |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
163 |
* True if ScheduledFutureTask.cancel should remove from queue |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
164 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
165 |
private volatile boolean removeOnCancel = false; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
166 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
167 |
/** |
2 | 168 |
* Sequence number to break scheduling ties, and in turn to |
169 |
* guarantee FIFO order among tied entries. |
|
170 |
*/ |
|
11279 | 171 |
private static final AtomicLong sequencer = new AtomicLong(); |
2 | 172 |
|
173 |
/** |
|
174 |
* Returns current nanosecond time. |
|
175 |
*/ |
|
176 |
final long now() { |
|
177 |
return System.nanoTime(); |
|
178 |
} |
|
179 |
||
180 |
private class ScheduledFutureTask<V> |
|
181 |
extends FutureTask<V> implements RunnableScheduledFuture<V> { |
|
182 |
||
183 |
/** Sequence number to break ties FIFO */ |
|
184 |
private final long sequenceNumber; |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
185 |
|
2 | 186 |
/** The time the task is enabled to execute in nanoTime units */ |
187 |
private long time; |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
188 |
|
2 | 189 |
/** |
190 |
* Period in nanoseconds for repeating tasks. A positive |
|
191 |
* value indicates fixed-rate execution. A negative value |
|
192 |
* indicates fixed-delay execution. A value of 0 indicates a |
|
193 |
* non-repeating task. |
|
194 |
*/ |
|
195 |
private final long period; |
|
196 |
||
197 |
/** The actual task to be re-enqueued by reExecutePeriodic */ |
|
198 |
RunnableScheduledFuture<V> outerTask = this; |
|
199 |
||
200 |
/** |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
201 |
* Index into delay queue, to support faster cancellation. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
202 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
203 |
int heapIndex; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
204 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
205 |
/** |
2 | 206 |
* Creates a one-shot action with given nanoTime-based trigger time. |
207 |
*/ |
|
208 |
ScheduledFutureTask(Runnable r, V result, long ns) { |
|
209 |
super(r, result); |
|
210 |
this.time = ns; |
|
211 |
this.period = 0; |
|
212 |
this.sequenceNumber = sequencer.getAndIncrement(); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Creates a periodic action with given nano time and period. |
|
217 |
*/ |
|
218 |
ScheduledFutureTask(Runnable r, V result, long ns, long period) { |
|
219 |
super(r, result); |
|
220 |
this.time = ns; |
|
221 |
this.period = period; |
|
222 |
this.sequenceNumber = sequencer.getAndIncrement(); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
226 |
* Creates a one-shot action with given nanoTime-based trigger time. |
2 | 227 |
*/ |
228 |
ScheduledFutureTask(Callable<V> callable, long ns) { |
|
229 |
super(callable); |
|
230 |
this.time = ns; |
|
231 |
this.period = 0; |
|
232 |
this.sequenceNumber = sequencer.getAndIncrement(); |
|
233 |
} |
|
234 |
||
235 |
public long getDelay(TimeUnit unit) { |
|
11279 | 236 |
return unit.convert(time - now(), NANOSECONDS); |
2 | 237 |
} |
238 |
||
239 |
public int compareTo(Delayed other) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
240 |
if (other == this) // compare zero if same object |
2 | 241 |
return 0; |
242 |
if (other instanceof ScheduledFutureTask) { |
|
243 |
ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other; |
|
244 |
long diff = time - x.time; |
|
245 |
if (diff < 0) |
|
246 |
return -1; |
|
247 |
else if (diff > 0) |
|
248 |
return 1; |
|
249 |
else if (sequenceNumber < x.sequenceNumber) |
|
250 |
return -1; |
|
251 |
else |
|
252 |
return 1; |
|
253 |
} |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
254 |
long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
255 |
return (diff < 0) ? -1 : (diff > 0) ? 1 : 0; |
2 | 256 |
} |
257 |
||
258 |
/** |
|
259 |
* Returns true if this is a periodic (not a one-shot) action. |
|
260 |
* |
|
261 |
* @return true if periodic |
|
262 |
*/ |
|
263 |
public boolean isPeriodic() { |
|
264 |
return period != 0; |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Sets the next time to run for a periodic task. |
|
269 |
*/ |
|
270 |
private void setNextRunTime() { |
|
271 |
long p = period; |
|
272 |
if (p > 0) |
|
273 |
time += p; |
|
274 |
else |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
275 |
time = triggerTime(-p); |
2 | 276 |
} |
277 |
||
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
278 |
public boolean cancel(boolean mayInterruptIfRunning) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
279 |
boolean cancelled = super.cancel(mayInterruptIfRunning); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
280 |
if (cancelled && removeOnCancel && heapIndex >= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
281 |
remove(this); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
282 |
return cancelled; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
283 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
284 |
|
2 | 285 |
/** |
286 |
* Overrides FutureTask version so as to reset/requeue if periodic. |
|
287 |
*/ |
|
288 |
public void run() { |
|
289 |
boolean periodic = isPeriodic(); |
|
290 |
if (!canRunInCurrentRunState(periodic)) |
|
291 |
cancel(false); |
|
292 |
else if (!periodic) |
|
293 |
ScheduledFutureTask.super.run(); |
|
294 |
else if (ScheduledFutureTask.super.runAndReset()) { |
|
295 |
setNextRunTime(); |
|
296 |
reExecutePeriodic(outerTask); |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Returns true if can run a task given current run state |
|
303 |
* and run-after-shutdown parameters. |
|
304 |
* |
|
305 |
* @param periodic true if this task periodic, false if delayed |
|
306 |
*/ |
|
307 |
boolean canRunInCurrentRunState(boolean periodic) { |
|
308 |
return isRunningOrShutdown(periodic ? |
|
309 |
continueExistingPeriodicTasksAfterShutdown : |
|
310 |
executeExistingDelayedTasksAfterShutdown); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Main execution method for delayed or periodic tasks. If pool |
|
315 |
* is shut down, rejects the task. Otherwise adds task to queue |
|
316 |
* and starts a thread, if necessary, to run it. (We cannot |
|
317 |
* prestart the thread to run the task because the task (probably) |
|
318 |
* shouldn't be run yet,) If the pool is shut down while the task |
|
319 |
* is being added, cancel and remove it if required by state and |
|
320 |
* run-after-shutdown parameters. |
|
321 |
* |
|
322 |
* @param task the task |
|
323 |
*/ |
|
324 |
private void delayedExecute(RunnableScheduledFuture<?> task) { |
|
325 |
if (isShutdown()) |
|
326 |
reject(task); |
|
327 |
else { |
|
328 |
super.getQueue().add(task); |
|
329 |
if (isShutdown() && |
|
330 |
!canRunInCurrentRunState(task.isPeriodic()) && |
|
331 |
remove(task)) |
|
332 |
task.cancel(false); |
|
333 |
else |
|
10609
0caee7f44e7b
7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents:
9242
diff
changeset
|
334 |
ensurePrestart(); |
2 | 335 |
} |
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Requeues a periodic task unless current run state precludes it. |
|
340 |
* Same idea as delayedExecute except drops task rather than rejecting. |
|
341 |
* |
|
342 |
* @param task the task |
|
343 |
*/ |
|
344 |
void reExecutePeriodic(RunnableScheduledFuture<?> task) { |
|
345 |
if (canRunInCurrentRunState(true)) { |
|
346 |
super.getQueue().add(task); |
|
347 |
if (!canRunInCurrentRunState(true) && remove(task)) |
|
348 |
task.cancel(false); |
|
349 |
else |
|
10609
0caee7f44e7b
7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents:
9242
diff
changeset
|
350 |
ensurePrestart(); |
2 | 351 |
} |
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Cancels and clears the queue of all tasks that should not be run |
|
356 |
* due to shutdown policy. Invoked within super.shutdown. |
|
357 |
*/ |
|
358 |
@Override void onShutdown() { |
|
359 |
BlockingQueue<Runnable> q = super.getQueue(); |
|
360 |
boolean keepDelayed = |
|
361 |
getExecuteExistingDelayedTasksAfterShutdownPolicy(); |
|
362 |
boolean keepPeriodic = |
|
363 |
getContinueExistingPeriodicTasksAfterShutdownPolicy(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
364 |
if (!keepDelayed && !keepPeriodic) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
365 |
for (Object e : q.toArray()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
366 |
if (e instanceof RunnableScheduledFuture<?>) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
367 |
((RunnableScheduledFuture<?>) e).cancel(false); |
2 | 368 |
q.clear(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
369 |
} |
2 | 370 |
else { |
371 |
// Traverse snapshot to avoid iterator exceptions |
|
372 |
for (Object e : q.toArray()) { |
|
373 |
if (e instanceof RunnableScheduledFuture) { |
|
374 |
RunnableScheduledFuture<?> t = |
|
375 |
(RunnableScheduledFuture<?>)e; |
|
376 |
if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) || |
|
377 |
t.isCancelled()) { // also remove if already cancelled |
|
378 |
if (q.remove(t)) |
|
379 |
t.cancel(false); |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
tryTerminate(); |
|
385 |
} |
|
386 |
||
387 |
/** |
|
388 |
* Modifies or replaces the task used to execute a runnable. |
|
389 |
* This method can be used to override the concrete |
|
390 |
* class used for managing internal tasks. |
|
391 |
* The default implementation simply returns the given task. |
|
392 |
* |
|
393 |
* @param runnable the submitted Runnable |
|
394 |
* @param task the task created to execute the runnable |
|
395 |
* @return a task that can execute the runnable |
|
396 |
* @since 1.6 |
|
397 |
*/ |
|
398 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
399 |
Runnable runnable, RunnableScheduledFuture<V> task) { |
|
400 |
return task; |
|
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* Modifies or replaces the task used to execute a callable. |
|
405 |
* This method can be used to override the concrete |
|
406 |
* class used for managing internal tasks. |
|
407 |
* The default implementation simply returns the given task. |
|
408 |
* |
|
409 |
* @param callable the submitted Callable |
|
410 |
* @param task the task created to execute the callable |
|
411 |
* @return a task that can execute the callable |
|
412 |
* @since 1.6 |
|
413 |
*/ |
|
414 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
415 |
Callable<V> callable, RunnableScheduledFuture<V> task) { |
|
416 |
return task; |
|
417 |
} |
|
418 |
||
419 |
/** |
|
420 |
* Creates a new {@code ScheduledThreadPoolExecutor} with the |
|
421 |
* given core pool size. |
|
422 |
* |
|
423 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
424 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
425 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
426 |
*/ |
|
427 |
public ScheduledThreadPoolExecutor(int corePoolSize) { |
|
11279 | 428 |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
2 | 429 |
new DelayedWorkQueue()); |
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Creates a new {@code ScheduledThreadPoolExecutor} with the |
|
434 |
* given initial parameters. |
|
435 |
* |
|
436 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
437 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
438 |
* @param threadFactory the factory to use when the executor |
|
439 |
* creates a new thread |
|
440 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
441 |
* @throws NullPointerException if {@code threadFactory} is null |
|
442 |
*/ |
|
443 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 444 |
ThreadFactory threadFactory) { |
11279 | 445 |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
2 | 446 |
new DelayedWorkQueue(), threadFactory); |
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Creates a new ScheduledThreadPoolExecutor with the given |
|
451 |
* initial parameters. |
|
452 |
* |
|
453 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
454 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
455 |
* @param handler the handler to use when execution is blocked |
|
456 |
* because the thread bounds and queue capacities are reached |
|
457 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
458 |
* @throws NullPointerException if {@code handler} is null |
|
459 |
*/ |
|
460 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 461 |
RejectedExecutionHandler handler) { |
11279 | 462 |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
2 | 463 |
new DelayedWorkQueue(), handler); |
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Creates a new ScheduledThreadPoolExecutor with the given |
|
468 |
* initial parameters. |
|
469 |
* |
|
470 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
471 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
472 |
* @param threadFactory the factory to use when the executor |
|
473 |
* creates a new thread |
|
474 |
* @param handler the handler to use when execution is blocked |
|
475 |
* because the thread bounds and queue capacities are reached |
|
476 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
477 |
* @throws NullPointerException if {@code threadFactory} or |
|
478 |
* {@code handler} is null |
|
479 |
*/ |
|
480 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 481 |
ThreadFactory threadFactory, |
482 |
RejectedExecutionHandler handler) { |
|
11279 | 483 |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
2 | 484 |
new DelayedWorkQueue(), threadFactory, handler); |
485 |
} |
|
486 |
||
487 |
/** |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
488 |
* Returns the trigger time of a delayed action. |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
489 |
*/ |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
490 |
private long triggerTime(long delay, TimeUnit unit) { |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
491 |
return triggerTime(unit.toNanos((delay < 0) ? 0 : delay)); |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
492 |
} |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
493 |
|
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
494 |
/** |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
495 |
* Returns the trigger time of a delayed action. |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
496 |
*/ |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
497 |
long triggerTime(long delay) { |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
498 |
return now() + |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
499 |
((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay)); |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
500 |
} |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
501 |
|
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
502 |
/** |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
503 |
* Constrains the values of all delays in the queue to be within |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
504 |
* Long.MAX_VALUE of each other, to avoid overflow in compareTo. |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
505 |
* This may occur if a task is eligible to be dequeued, but has |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
506 |
* not yet been, while some other task is added with a delay of |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
507 |
* Long.MAX_VALUE. |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
508 |
*/ |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
509 |
private long overflowFree(long delay) { |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
510 |
Delayed head = (Delayed) super.getQueue().peek(); |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
511 |
if (head != null) { |
11279 | 512 |
long headDelay = head.getDelay(NANOSECONDS); |
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
513 |
if (headDelay < 0 && (delay - headDelay < 0)) |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
514 |
delay = Long.MAX_VALUE + headDelay; |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
515 |
} |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
516 |
return delay; |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
517 |
} |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
518 |
|
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
519 |
/** |
2 | 520 |
* @throws RejectedExecutionException {@inheritDoc} |
521 |
* @throws NullPointerException {@inheritDoc} |
|
522 |
*/ |
|
523 |
public ScheduledFuture<?> schedule(Runnable command, |
|
524 |
long delay, |
|
525 |
TimeUnit unit) { |
|
526 |
if (command == null || unit == null) |
|
527 |
throw new NullPointerException(); |
|
528 |
RunnableScheduledFuture<?> t = decorateTask(command, |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
529 |
new ScheduledFutureTask<Void>(command, null, |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
530 |
triggerTime(delay, unit))); |
2 | 531 |
delayedExecute(t); |
532 |
return t; |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* @throws RejectedExecutionException {@inheritDoc} |
|
537 |
* @throws NullPointerException {@inheritDoc} |
|
538 |
*/ |
|
539 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, |
|
540 |
long delay, |
|
541 |
TimeUnit unit) { |
|
542 |
if (callable == null || unit == null) |
|
543 |
throw new NullPointerException(); |
|
544 |
RunnableScheduledFuture<V> t = decorateTask(callable, |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
545 |
new ScheduledFutureTask<V>(callable, |
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
546 |
triggerTime(delay, unit))); |
2 | 547 |
delayedExecute(t); |
548 |
return t; |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* @throws RejectedExecutionException {@inheritDoc} |
|
553 |
* @throws NullPointerException {@inheritDoc} |
|
554 |
* @throws IllegalArgumentException {@inheritDoc} |
|
555 |
*/ |
|
556 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, |
|
557 |
long initialDelay, |
|
558 |
long period, |
|
559 |
TimeUnit unit) { |
|
560 |
if (command == null || unit == null) |
|
561 |
throw new NullPointerException(); |
|
562 |
if (period <= 0) |
|
563 |
throw new IllegalArgumentException(); |
|
564 |
ScheduledFutureTask<Void> sft = |
|
565 |
new ScheduledFutureTask<Void>(command, |
|
566 |
null, |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
567 |
triggerTime(initialDelay, unit), |
2 | 568 |
unit.toNanos(period)); |
569 |
RunnableScheduledFuture<Void> t = decorateTask(command, sft); |
|
570 |
sft.outerTask = t; |
|
571 |
delayedExecute(t); |
|
572 |
return t; |
|
573 |
} |
|
574 |
||
575 |
/** |
|
576 |
* @throws RejectedExecutionException {@inheritDoc} |
|
577 |
* @throws NullPointerException {@inheritDoc} |
|
578 |
* @throws IllegalArgumentException {@inheritDoc} |
|
579 |
*/ |
|
580 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, |
|
581 |
long initialDelay, |
|
582 |
long delay, |
|
583 |
TimeUnit unit) { |
|
584 |
if (command == null || unit == null) |
|
585 |
throw new NullPointerException(); |
|
586 |
if (delay <= 0) |
|
587 |
throw new IllegalArgumentException(); |
|
588 |
ScheduledFutureTask<Void> sft = |
|
589 |
new ScheduledFutureTask<Void>(command, |
|
590 |
null, |
|
1007
72a8a27e1d69
6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents:
61
diff
changeset
|
591 |
triggerTime(initialDelay, unit), |
2 | 592 |
unit.toNanos(-delay)); |
593 |
RunnableScheduledFuture<Void> t = decorateTask(command, sft); |
|
594 |
sft.outerTask = t; |
|
595 |
delayedExecute(t); |
|
596 |
return t; |
|
597 |
} |
|
598 |
||
599 |
/** |
|
600 |
* Executes {@code command} with zero required delay. |
|
601 |
* This has effect equivalent to |
|
602 |
* {@link #schedule(Runnable,long,TimeUnit) schedule(command, 0, anyUnit)}. |
|
603 |
* Note that inspections of the queue and of the list returned by |
|
604 |
* {@code shutdownNow} will access the zero-delayed |
|
605 |
* {@link ScheduledFuture}, not the {@code command} itself. |
|
606 |
* |
|
607 |
* <p>A consequence of the use of {@code ScheduledFuture} objects is |
|
608 |
* that {@link ThreadPoolExecutor#afterExecute afterExecute} is always |
|
609 |
* called with a null second {@code Throwable} argument, even if the |
|
610 |
* {@code command} terminated abruptly. Instead, the {@code Throwable} |
|
611 |
* thrown by such a task can be obtained via {@link Future#get}. |
|
612 |
* |
|
613 |
* @throws RejectedExecutionException at discretion of |
|
614 |
* {@code RejectedExecutionHandler}, if the task |
|
615 |
* cannot be accepted for execution because the |
|
616 |
* executor has been shut down |
|
617 |
* @throws NullPointerException {@inheritDoc} |
|
618 |
*/ |
|
619 |
public void execute(Runnable command) { |
|
11279 | 620 |
schedule(command, 0, NANOSECONDS); |
2 | 621 |
} |
622 |
||
623 |
// Override AbstractExecutorService methods |
|
624 |
||
625 |
/** |
|
626 |
* @throws RejectedExecutionException {@inheritDoc} |
|
627 |
* @throws NullPointerException {@inheritDoc} |
|
628 |
*/ |
|
629 |
public Future<?> submit(Runnable task) { |
|
11279 | 630 |
return schedule(task, 0, NANOSECONDS); |
2 | 631 |
} |
632 |
||
633 |
/** |
|
634 |
* @throws RejectedExecutionException {@inheritDoc} |
|
635 |
* @throws NullPointerException {@inheritDoc} |
|
636 |
*/ |
|
637 |
public <T> Future<T> submit(Runnable task, T result) { |
|
11279 | 638 |
return schedule(Executors.callable(task, result), 0, NANOSECONDS); |
2 | 639 |
} |
640 |
||
641 |
/** |
|
642 |
* @throws RejectedExecutionException {@inheritDoc} |
|
643 |
* @throws NullPointerException {@inheritDoc} |
|
644 |
*/ |
|
645 |
public <T> Future<T> submit(Callable<T> task) { |
|
11279 | 646 |
return schedule(task, 0, NANOSECONDS); |
2 | 647 |
} |
648 |
||
649 |
/** |
|
650 |
* Sets the policy on whether to continue executing existing |
|
651 |
* periodic tasks even when this executor has been {@code shutdown}. |
|
652 |
* In this case, these tasks will only terminate upon |
|
653 |
* {@code shutdownNow} or after setting the policy to |
|
654 |
* {@code false} when already shutdown. |
|
655 |
* This value is by default {@code false}. |
|
656 |
* |
|
657 |
* @param value if {@code true}, continue after shutdown, else don't. |
|
658 |
* @see #getContinueExistingPeriodicTasksAfterShutdownPolicy |
|
659 |
*/ |
|
660 |
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) { |
|
661 |
continueExistingPeriodicTasksAfterShutdown = value; |
|
662 |
if (!value && isShutdown()) |
|
663 |
onShutdown(); |
|
664 |
} |
|
665 |
||
666 |
/** |
|
667 |
* Gets the policy on whether to continue executing existing |
|
668 |
* periodic tasks even when this executor has been {@code shutdown}. |
|
669 |
* In this case, these tasks will only terminate upon |
|
670 |
* {@code shutdownNow} or after setting the policy to |
|
671 |
* {@code false} when already shutdown. |
|
672 |
* This value is by default {@code false}. |
|
673 |
* |
|
674 |
* @return {@code true} if will continue after shutdown |
|
675 |
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy |
|
676 |
*/ |
|
677 |
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() { |
|
678 |
return continueExistingPeriodicTasksAfterShutdown; |
|
679 |
} |
|
680 |
||
681 |
/** |
|
682 |
* Sets the policy on whether to execute existing delayed |
|
683 |
* tasks even when this executor has been {@code shutdown}. |
|
684 |
* In this case, these tasks will only terminate upon |
|
685 |
* {@code shutdownNow}, or after setting the policy to |
|
686 |
* {@code false} when already shutdown. |
|
687 |
* This value is by default {@code true}. |
|
688 |
* |
|
689 |
* @param value if {@code true}, execute after shutdown, else don't. |
|
690 |
* @see #getExecuteExistingDelayedTasksAfterShutdownPolicy |
|
691 |
*/ |
|
692 |
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) { |
|
693 |
executeExistingDelayedTasksAfterShutdown = value; |
|
694 |
if (!value && isShutdown()) |
|
695 |
onShutdown(); |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* Gets the policy on whether to execute existing delayed |
|
700 |
* tasks even when this executor has been {@code shutdown}. |
|
701 |
* In this case, these tasks will only terminate upon |
|
702 |
* {@code shutdownNow}, or after setting the policy to |
|
703 |
* {@code false} when already shutdown. |
|
704 |
* This value is by default {@code true}. |
|
705 |
* |
|
706 |
* @return {@code true} if will execute after shutdown |
|
707 |
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy |
|
708 |
*/ |
|
709 |
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() { |
|
710 |
return executeExistingDelayedTasksAfterShutdown; |
|
711 |
} |
|
712 |
||
713 |
/** |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
714 |
* Sets the policy on whether cancelled tasks should be immediately |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
715 |
* removed from the work queue at time of cancellation. This value is |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
716 |
* by default {@code false}. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
717 |
* |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
718 |
* @param value if {@code true}, remove on cancellation, else don't |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
719 |
* @see #getRemoveOnCancelPolicy |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
720 |
* @since 1.7 |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
721 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
722 |
public void setRemoveOnCancelPolicy(boolean value) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
723 |
removeOnCancel = value; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
724 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
725 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
726 |
/** |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
727 |
* Gets the policy on whether cancelled tasks should be immediately |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
728 |
* removed from the work queue at time of cancellation. This value is |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
729 |
* by default {@code false}. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
730 |
* |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
731 |
* @return {@code true} if cancelled tasks are immediately removed |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
732 |
* from the queue |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
733 |
* @see #setRemoveOnCancelPolicy |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
734 |
* @since 1.7 |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
735 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
736 |
public boolean getRemoveOnCancelPolicy() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
737 |
return removeOnCancel; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
738 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
739 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
740 |
/** |
2 | 741 |
* Initiates an orderly shutdown in which previously submitted |
61
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
742 |
* tasks are executed, but no new tasks will be accepted. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
743 |
* Invocation has no additional effect if already shut down. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
744 |
* |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
745 |
* <p>This method does not wait for previously submitted tasks to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
746 |
* complete execution. Use {@link #awaitTermination awaitTermination} |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
747 |
* to do that. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
748 |
* |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
749 |
* <p>If the {@code ExecuteExistingDelayedTasksAfterShutdownPolicy} |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
750 |
* has been set {@code false}, existing delayed tasks whose delays |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
751 |
* have not yet elapsed are cancelled. And unless the {@code |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
752 |
* ContinueExistingPeriodicTasksAfterShutdownPolicy} has been set |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
753 |
* {@code true}, future executions of existing periodic tasks will |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
754 |
* be cancelled. |
2 | 755 |
* |
756 |
* @throws SecurityException {@inheritDoc} |
|
757 |
*/ |
|
758 |
public void shutdown() { |
|
759 |
super.shutdown(); |
|
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* Attempts to stop all actively executing tasks, halts the |
|
764 |
* processing of waiting tasks, and returns a list of the tasks |
|
765 |
* that were awaiting execution. |
|
766 |
* |
|
61
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
767 |
* <p>This method does not wait for actively executing tasks to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
768 |
* terminate. Use {@link #awaitTermination awaitTermination} to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
769 |
* do that. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
59
diff
changeset
|
770 |
* |
2 | 771 |
* <p>There are no guarantees beyond best-effort attempts to stop |
772 |
* processing actively executing tasks. This implementation |
|
773 |
* cancels tasks via {@link Thread#interrupt}, so any task that |
|
774 |
* fails to respond to interrupts may never terminate. |
|
775 |
* |
|
776 |
* @return list of tasks that never commenced execution. |
|
777 |
* Each element of this list is a {@link ScheduledFuture}, |
|
778 |
* including those tasks submitted using {@code execute}, |
|
779 |
* which are for scheduling purposes used as the basis of a |
|
780 |
* zero-delay {@code ScheduledFuture}. |
|
781 |
* @throws SecurityException {@inheritDoc} |
|
782 |
*/ |
|
783 |
public List<Runnable> shutdownNow() { |
|
784 |
return super.shutdownNow(); |
|
785 |
} |
|
786 |
||
787 |
/** |
|
788 |
* Returns the task queue used by this executor. Each element of |
|
789 |
* this queue is a {@link ScheduledFuture}, including those |
|
790 |
* tasks submitted using {@code execute} which are for scheduling |
|
791 |
* purposes used as the basis of a zero-delay |
|
792 |
* {@code ScheduledFuture}. Iteration over this queue is |
|
793 |
* <em>not</em> guaranteed to traverse tasks in the order in |
|
794 |
* which they will execute. |
|
795 |
* |
|
796 |
* @return the task queue |
|
797 |
*/ |
|
798 |
public BlockingQueue<Runnable> getQueue() { |
|
799 |
return super.getQueue(); |
|
800 |
} |
|
801 |
||
802 |
/** |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
803 |
* Specialized delay queue. To mesh with TPE declarations, this |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
804 |
* class must be declared as a BlockingQueue<Runnable> even though |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
805 |
* it can only hold RunnableScheduledFutures. |
2 | 806 |
*/ |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
807 |
static class DelayedWorkQueue extends AbstractQueue<Runnable> |
2 | 808 |
implements BlockingQueue<Runnable> { |
809 |
||
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
810 |
/* |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
811 |
* A DelayedWorkQueue is based on a heap-based data structure |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
812 |
* like those in DelayQueue and PriorityQueue, except that |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
813 |
* every ScheduledFutureTask also records its index into the |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
814 |
* heap array. This eliminates the need to find a task upon |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
815 |
* cancellation, greatly speeding up removal (down from O(n) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
816 |
* to O(log n)), and reducing garbage retention that would |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
817 |
* otherwise occur by waiting for the element to rise to top |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
818 |
* before clearing. But because the queue may also hold |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
819 |
* RunnableScheduledFutures that are not ScheduledFutureTasks, |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
820 |
* we are not guaranteed to have such indices available, in |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
821 |
* which case we fall back to linear search. (We expect that |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
822 |
* most tasks will not be decorated, and that the faster cases |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
823 |
* will be much more common.) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
824 |
* |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
825 |
* All heap operations must record index changes -- mainly |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
826 |
* within siftUp and siftDown. Upon removal, a task's |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
827 |
* heapIndex is set to -1. Note that ScheduledFutureTasks can |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
828 |
* appear at most once in the queue (this need not be true for |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
829 |
* other kinds of tasks or work queues), so are uniquely |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
830 |
* identified by heapIndex. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
831 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
832 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
833 |
private static final int INITIAL_CAPACITY = 16; |
11279 | 834 |
private RunnableScheduledFuture<?>[] queue = |
835 |
new RunnableScheduledFuture<?>[INITIAL_CAPACITY]; |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
836 |
private final ReentrantLock lock = new ReentrantLock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
837 |
private int size = 0; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
838 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
839 |
/** |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
840 |
* Thread designated to wait for the task at the head of the |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
841 |
* queue. This variant of the Leader-Follower pattern |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
842 |
* (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
843 |
* minimize unnecessary timed waiting. When a thread becomes |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
844 |
* the leader, it waits only for the next delay to elapse, but |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
845 |
* other threads await indefinitely. The leader thread must |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
846 |
* signal some other thread before returning from take() or |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
847 |
* poll(...), unless some other thread becomes leader in the |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
848 |
* interim. Whenever the head of the queue is replaced with a |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
849 |
* task with an earlier expiration time, the leader field is |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
850 |
* invalidated by being reset to null, and some waiting |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
851 |
* thread, but not necessarily the current leader, is |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
852 |
* signalled. So waiting threads must be prepared to acquire |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
853 |
* and lose leadership while waiting. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
854 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
855 |
private Thread leader = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
856 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
857 |
/** |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
858 |
* Condition signalled when a newer task becomes available at the |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
859 |
* head of the queue or a new thread may need to become leader. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
860 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
861 |
private final Condition available = lock.newCondition(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
862 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
863 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
864 |
* Sets f's heapIndex if it is a ScheduledFutureTask. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
865 |
*/ |
11279 | 866 |
private void setIndex(RunnableScheduledFuture<?> f, int idx) { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
867 |
if (f instanceof ScheduledFutureTask) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
868 |
((ScheduledFutureTask)f).heapIndex = idx; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
869 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
870 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
871 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
872 |
* Sifts element added at bottom up to its heap-ordered spot. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
873 |
* Call only when holding lock. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
874 |
*/ |
11279 | 875 |
private void siftUp(int k, RunnableScheduledFuture<?> key) { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
876 |
while (k > 0) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
877 |
int parent = (k - 1) >>> 1; |
11279 | 878 |
RunnableScheduledFuture<?> e = queue[parent]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
879 |
if (key.compareTo(e) >= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
880 |
break; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
881 |
queue[k] = e; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
882 |
setIndex(e, k); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
883 |
k = parent; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
884 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
885 |
queue[k] = key; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
886 |
setIndex(key, k); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
887 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
888 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
889 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
890 |
* Sifts element added at top down to its heap-ordered spot. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
891 |
* Call only when holding lock. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
892 |
*/ |
11279 | 893 |
private void siftDown(int k, RunnableScheduledFuture<?> key) { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
894 |
int half = size >>> 1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
895 |
while (k < half) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
896 |
int child = (k << 1) + 1; |
11279 | 897 |
RunnableScheduledFuture<?> c = queue[child]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
898 |
int right = child + 1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
899 |
if (right < size && c.compareTo(queue[right]) > 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
900 |
c = queue[child = right]; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
901 |
if (key.compareTo(c) <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
902 |
break; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
903 |
queue[k] = c; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
904 |
setIndex(c, k); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
905 |
k = child; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
906 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
907 |
queue[k] = key; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
908 |
setIndex(key, k); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
909 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
910 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
911 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
912 |
* Resizes the heap array. Call only when holding lock. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
913 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
914 |
private void grow() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
915 |
int oldCapacity = queue.length; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
916 |
int newCapacity = oldCapacity + (oldCapacity >> 1); // grow 50% |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
917 |
if (newCapacity < 0) // overflow |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
918 |
newCapacity = Integer.MAX_VALUE; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
919 |
queue = Arrays.copyOf(queue, newCapacity); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
920 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
921 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
922 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
923 |
* Finds index of given object, or -1 if absent. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
924 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
925 |
private int indexOf(Object x) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
926 |
if (x != null) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
927 |
if (x instanceof ScheduledFutureTask) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
928 |
int i = ((ScheduledFutureTask) x).heapIndex; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
929 |
// Sanity check; x could conceivably be a |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
930 |
// ScheduledFutureTask from some other pool. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
931 |
if (i >= 0 && i < size && queue[i] == x) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
932 |
return i; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
933 |
} else { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
934 |
for (int i = 0; i < size; i++) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
935 |
if (x.equals(queue[i])) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
936 |
return i; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
937 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
938 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
939 |
return -1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
940 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
941 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
942 |
public boolean contains(Object x) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
943 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
944 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
945 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
946 |
return indexOf(x) != -1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
947 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
948 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
949 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
950 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
951 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
952 |
public boolean remove(Object x) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
953 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
954 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
955 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
956 |
int i = indexOf(x); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
957 |
if (i < 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
958 |
return false; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
959 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
960 |
setIndex(queue[i], -1); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
961 |
int s = --size; |
11279 | 962 |
RunnableScheduledFuture<?> replacement = queue[s]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
963 |
queue[s] = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
964 |
if (s != i) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
965 |
siftDown(i, replacement); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
966 |
if (queue[i] == replacement) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
967 |
siftUp(i, replacement); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
968 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
969 |
return true; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
970 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
971 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
972 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
973 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
974 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
975 |
public int size() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
976 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
977 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
978 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
979 |
return size; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
980 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
981 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
982 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
983 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
984 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
985 |
public boolean isEmpty() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
986 |
return size() == 0; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
987 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
988 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
989 |
public int remainingCapacity() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
990 |
return Integer.MAX_VALUE; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
991 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
992 |
|
11279 | 993 |
public RunnableScheduledFuture<?> peek() { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
994 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
995 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
996 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
997 |
return queue[0]; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
998 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
999 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1000 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1001 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1002 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1003 |
public boolean offer(Runnable x) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1004 |
if (x == null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1005 |
throw new NullPointerException(); |
11279 | 1006 |
RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1007 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1008 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1009 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1010 |
int i = size; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1011 |
if (i >= queue.length) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1012 |
grow(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1013 |
size = i + 1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1014 |
if (i == 0) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1015 |
queue[0] = e; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1016 |
setIndex(e, 0); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1017 |
} else { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1018 |
siftUp(i, e); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1019 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1020 |
if (queue[0] == e) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1021 |
leader = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1022 |
available.signal(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1023 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1024 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1025 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1026 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1027 |
return true; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1028 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1029 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1030 |
public void put(Runnable e) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1031 |
offer(e); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1032 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1033 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1034 |
public boolean add(Runnable e) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1035 |
return offer(e); |
2 | 1036 |
} |
1037 |
||
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1038 |
public boolean offer(Runnable e, long timeout, TimeUnit unit) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1039 |
return offer(e); |
2 | 1040 |
} |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1041 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1042 |
/** |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1043 |
* Performs common bookkeeping for poll and take: Replaces |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1044 |
* first element with last and sifts it down. Call only when |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1045 |
* holding lock. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1046 |
* @param f the task to remove and return |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1047 |
*/ |
11279 | 1048 |
private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1049 |
int s = --size; |
11279 | 1050 |
RunnableScheduledFuture<?> x = queue[s]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1051 |
queue[s] = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1052 |
if (s != 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1053 |
siftDown(0, x); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1054 |
setIndex(f, -1); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1055 |
return f; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1056 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1057 |
|
11279 | 1058 |
public RunnableScheduledFuture<?> poll() { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1059 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1060 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1061 |
try { |
11279 | 1062 |
RunnableScheduledFuture<?> first = queue[0]; |
1063 |
if (first == null || first.getDelay(NANOSECONDS) > 0) |
|
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1064 |
return null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1065 |
else |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1066 |
return finishPoll(first); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1067 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1068 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1069 |
} |
2 | 1070 |
} |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1071 |
|
11279 | 1072 |
public RunnableScheduledFuture<?> take() throws InterruptedException { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1073 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1074 |
lock.lockInterruptibly(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1075 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1076 |
for (;;) { |
11279 | 1077 |
RunnableScheduledFuture<?> first = queue[0]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1078 |
if (first == null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1079 |
available.await(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1080 |
else { |
11279 | 1081 |
long delay = first.getDelay(NANOSECONDS); |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1082 |
if (delay <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1083 |
return finishPoll(first); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1084 |
else if (leader != null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1085 |
available.await(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1086 |
else { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1087 |
Thread thisThread = Thread.currentThread(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1088 |
leader = thisThread; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1089 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1090 |
available.awaitNanos(delay); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1091 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1092 |
if (leader == thisThread) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1093 |
leader = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1094 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1095 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1096 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1097 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1098 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1099 |
if (leader == null && queue[0] != null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1100 |
available.signal(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1101 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1102 |
} |
2 | 1103 |
} |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1104 |
|
11279 | 1105 |
public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit) |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1106 |
throws InterruptedException { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1107 |
long nanos = unit.toNanos(timeout); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1108 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1109 |
lock.lockInterruptibly(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1110 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1111 |
for (;;) { |
11279 | 1112 |
RunnableScheduledFuture<?> first = queue[0]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1113 |
if (first == null) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1114 |
if (nanos <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1115 |
return null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1116 |
else |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1117 |
nanos = available.awaitNanos(nanos); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1118 |
} else { |
11279 | 1119 |
long delay = first.getDelay(NANOSECONDS); |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1120 |
if (delay <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1121 |
return finishPoll(first); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1122 |
if (nanos <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1123 |
return null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1124 |
if (nanos < delay || leader != null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1125 |
nanos = available.awaitNanos(nanos); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1126 |
else { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1127 |
Thread thisThread = Thread.currentThread(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1128 |
leader = thisThread; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1129 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1130 |
long timeLeft = available.awaitNanos(delay); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1131 |
nanos -= delay - timeLeft; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1132 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1133 |
if (leader == thisThread) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1134 |
leader = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1135 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1136 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1137 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1138 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1139 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1140 |
if (leader == null && queue[0] != null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1141 |
available.signal(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1142 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1143 |
} |
2 | 1144 |
} |
1145 |
||
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1146 |
public void clear() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1147 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1148 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1149 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1150 |
for (int i = 0; i < size; i++) { |
11279 | 1151 |
RunnableScheduledFuture<?> t = queue[i]; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1152 |
if (t != null) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1153 |
queue[i] = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1154 |
setIndex(t, -1); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1155 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1156 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1157 |
size = 0; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1158 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1159 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1160 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1161 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1162 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1163 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1164 |
* Returns first element only if it is expired. |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1165 |
* Used only by drainTo. Call only when holding lock. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1166 |
*/ |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1167 |
private RunnableScheduledFuture<?> peekExpired() { |
11279 | 1168 |
// assert lock.isHeldByCurrentThread(); |
1169 |
RunnableScheduledFuture<?> first = queue[0]; |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1170 |
return (first == null || first.getDelay(NANOSECONDS) > 0) ? |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1171 |
null : first; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1172 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1173 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1174 |
public int drainTo(Collection<? super Runnable> c) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1175 |
if (c == null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1176 |
throw new NullPointerException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1177 |
if (c == this) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1178 |
throw new IllegalArgumentException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1179 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1180 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1181 |
try { |
11279 | 1182 |
RunnableScheduledFuture<?> first; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1183 |
int n = 0; |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1184 |
while ((first = peekExpired()) != null) { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1185 |
c.add(first); // In this order, in case add() throws. |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1186 |
finishPoll(first); |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1187 |
++n; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1188 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1189 |
return n; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1190 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1191 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1192 |
} |
2 | 1193 |
} |
1194 |
||
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1195 |
public int drainTo(Collection<? super Runnable> c, int maxElements) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1196 |
if (c == null) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1197 |
throw new NullPointerException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1198 |
if (c == this) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1199 |
throw new IllegalArgumentException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1200 |
if (maxElements <= 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1201 |
return 0; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1202 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1203 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1204 |
try { |
11279 | 1205 |
RunnableScheduledFuture<?> first; |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1206 |
int n = 0; |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1207 |
while (n < maxElements && (first = peekExpired()) != null) { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1208 |
c.add(first); // In this order, in case add() throws. |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1209 |
finishPoll(first); |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1210 |
++n; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1211 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1212 |
return n; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1213 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1214 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1215 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1216 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1217 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1218 |
public Object[] toArray() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1219 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1220 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1221 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1222 |
return Arrays.copyOf(queue, size, Object[].class); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1223 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1224 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1225 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1226 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1227 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1228 |
@SuppressWarnings("unchecked") |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1229 |
public <T> T[] toArray(T[] a) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1230 |
final ReentrantLock lock = this.lock; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1231 |
lock.lock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1232 |
try { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1233 |
if (a.length < size) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1234 |
return (T[]) Arrays.copyOf(queue, size, a.getClass()); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1235 |
System.arraycopy(queue, 0, a, 0, size); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1236 |
if (a.length > size) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1237 |
a[size] = null; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1238 |
return a; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1239 |
} finally { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1240 |
lock.unlock(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1241 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1242 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1243 |
|
2 | 1244 |
public Iterator<Runnable> iterator() { |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1245 |
return new Itr(Arrays.copyOf(queue, size)); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1246 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1247 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1248 |
/** |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1249 |
* Snapshot iterator that works off copy of underlying q array. |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1250 |
*/ |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1251 |
private class Itr implements Iterator<Runnable> { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1252 |
final RunnableScheduledFuture[] array; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1253 |
int cursor = 0; // index of next element to return |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1254 |
int lastRet = -1; // index of last element, or -1 if no such |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1255 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1256 |
Itr(RunnableScheduledFuture[] array) { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1257 |
this.array = array; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1258 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1259 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1260 |
public boolean hasNext() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1261 |
return cursor < array.length; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1262 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1263 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1264 |
public Runnable next() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1265 |
if (cursor >= array.length) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1266 |
throw new NoSuchElementException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1267 |
lastRet = cursor; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1268 |
return array[cursor++]; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1269 |
} |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1270 |
|
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1271 |
public void remove() { |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1272 |
if (lastRet < 0) |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1273 |
throw new IllegalStateException(); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1274 |
DelayedWorkQueue.this.remove(array[lastRet]); |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1275 |
lastRet = -1; |
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1276 |
} |
2 | 1277 |
} |
1278 |
} |
|
1279 |
} |