1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.11 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.28 |
import java.util.concurrent.*; // for javadoc (till 6280605 is fixed) |
9 |
dl |
1.1 |
import java.util.concurrent.atomic.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
|
12 |
|
|
/** |
13 |
dl |
1.7 |
* A {@link ThreadPoolExecutor} that can additionally schedule |
14 |
|
|
* commands to run after a given delay, or to execute |
15 |
|
|
* periodically. This class is preferable to {@link java.util.Timer} |
16 |
|
|
* when multiple worker threads are needed, or when the additional |
17 |
|
|
* flexibility or capabilities of {@link ThreadPoolExecutor} (which |
18 |
|
|
* this class extends) are required. |
19 |
dl |
1.1 |
* |
20 |
|
|
* <p> Delayed tasks execute no sooner than they are enabled, but |
21 |
dl |
1.18 |
* without any real-time guarantees about when, after they are |
22 |
|
|
* enabled, they will commence. Tasks scheduled for exactly the same |
23 |
|
|
* execution time are enabled in first-in-first-out (FIFO) order of |
24 |
|
|
* submission. |
25 |
dl |
1.1 |
* |
26 |
|
|
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
27 |
dl |
1.8 |
* of the inherited tuning methods are not useful for it. In |
28 |
|
|
* particular, because it acts as a fixed-sized pool using |
29 |
|
|
* <tt>corePoolSize</tt> threads and an unbounded queue, adjustments |
30 |
|
|
* to <tt>maximumPoolSize</tt> have no useful effect. |
31 |
dl |
1.1 |
* |
32 |
dl |
1.23 |
* <p>This class supports protected extension method |
33 |
|
|
* <tt>decorateTask</tt> (one version each for <tt>Runnable</tt> and |
34 |
|
|
* <tt>Callable</tt>) that can be used to customize the concrete task |
35 |
jsr166 |
1.28 |
* types used to execute commands. By default, |
36 |
dl |
1.23 |
* a <tt>ScheduledThreadPoolExecutor</tt> uses |
37 |
|
|
* a task type extending {@link FutureTask}. However, this may |
38 |
|
|
* be modified or replaced using subclasses of the form: |
39 |
|
|
* <pre> |
40 |
|
|
* public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor { |
41 |
|
|
* |
42 |
jsr166 |
1.29 |
* static class CustomTask<V> implements RunnableScheduledFuture<V> { ... } |
43 |
dl |
1.23 |
* |
44 |
jsr166 |
1.29 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
45 |
|
|
* Runnable r, RunnableScheduledFuture<V> task) { |
46 |
|
|
* return new CustomTask<V>(r, task); |
47 |
|
|
* } |
48 |
dl |
1.23 |
* |
49 |
jsr166 |
1.29 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
50 |
|
|
* Callable<V> c, RunnableScheduledFuture<V> task) { |
51 |
|
|
* return new CustomTask<V>(c, task); |
52 |
|
|
* } |
53 |
|
|
* // ... add constructors, etc. |
54 |
dl |
1.23 |
* } |
55 |
|
|
* </pre> |
56 |
dl |
1.1 |
* @since 1.5 |
57 |
|
|
* @author Doug Lea |
58 |
|
|
*/ |
59 |
jsr166 |
1.21 |
public class ScheduledThreadPoolExecutor |
60 |
|
|
extends ThreadPoolExecutor |
61 |
tim |
1.3 |
implements ScheduledExecutorService { |
62 |
dl |
1.1 |
|
63 |
|
|
/** |
64 |
|
|
* False if should cancel/suppress periodic tasks on shutdown. |
65 |
|
|
*/ |
66 |
|
|
private volatile boolean continueExistingPeriodicTasksAfterShutdown; |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* False if should cancel non-periodic tasks on shutdown. |
70 |
|
|
*/ |
71 |
|
|
private volatile boolean executeExistingDelayedTasksAfterShutdown = true; |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Sequence number to break scheduling ties, and in turn to |
75 |
|
|
* guarantee FIFO order among tied entries. |
76 |
|
|
*/ |
77 |
|
|
private static final AtomicLong sequencer = new AtomicLong(0); |
78 |
dl |
1.14 |
|
79 |
|
|
/** Base of nanosecond timings, to avoid wrapping */ |
80 |
|
|
private static final long NANO_ORIGIN = System.nanoTime(); |
81 |
|
|
|
82 |
|
|
/** |
83 |
dl |
1.18 |
* Returns nanosecond time offset by origin |
84 |
dl |
1.14 |
*/ |
85 |
dl |
1.17 |
final long now() { |
86 |
peierls |
1.22 |
return System.nanoTime() - NANO_ORIGIN; |
87 |
dl |
1.14 |
} |
88 |
|
|
|
89 |
jsr166 |
1.21 |
private class ScheduledFutureTask<V> |
90 |
peierls |
1.22 |
extends FutureTask<V> implements RunnableScheduledFuture<V> { |
91 |
jsr166 |
1.21 |
|
92 |
dl |
1.1 |
/** Sequence number to break ties FIFO */ |
93 |
|
|
private final long sequenceNumber; |
94 |
|
|
/** The time the task is enabled to execute in nanoTime units */ |
95 |
|
|
private long time; |
96 |
dl |
1.16 |
/** |
97 |
|
|
* Period in nanoseconds for repeating tasks. A positive |
98 |
|
|
* value indicates fixed-rate execution. A negative value |
99 |
|
|
* indicates fixed-delay execution. A value of 0 indicates a |
100 |
|
|
* non-repeating task. |
101 |
|
|
*/ |
102 |
dl |
1.1 |
private final long period; |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Creates a one-shot action with given nanoTime-based trigger time |
106 |
|
|
*/ |
107 |
|
|
ScheduledFutureTask(Runnable r, V result, long ns) { |
108 |
|
|
super(r, result); |
109 |
|
|
this.time = ns; |
110 |
|
|
this.period = 0; |
111 |
|
|
this.sequenceNumber = sequencer.getAndIncrement(); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* Creates a periodic action with given nano time and period |
116 |
|
|
*/ |
117 |
dl |
1.16 |
ScheduledFutureTask(Runnable r, V result, long ns, long period) { |
118 |
dl |
1.1 |
super(r, result); |
119 |
|
|
this.time = ns; |
120 |
|
|
this.period = period; |
121 |
|
|
this.sequenceNumber = sequencer.getAndIncrement(); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* Creates a one-shot action with given nanoTime-based trigger |
126 |
|
|
*/ |
127 |
|
|
ScheduledFutureTask(Callable<V> callable, long ns) { |
128 |
|
|
super(callable); |
129 |
|
|
this.time = ns; |
130 |
|
|
this.period = 0; |
131 |
|
|
this.sequenceNumber = sequencer.getAndIncrement(); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
public long getDelay(TimeUnit unit) { |
135 |
dl |
1.14 |
long d = unit.convert(time - now(), TimeUnit.NANOSECONDS); |
136 |
dl |
1.1 |
return d; |
137 |
|
|
} |
138 |
|
|
|
139 |
dl |
1.20 |
public int compareTo(Delayed other) { |
140 |
dl |
1.1 |
if (other == this) // compare zero ONLY if same object |
141 |
|
|
return 0; |
142 |
|
|
ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other; |
143 |
|
|
long diff = time - x.time; |
144 |
|
|
if (diff < 0) |
145 |
|
|
return -1; |
146 |
|
|
else if (diff > 0) |
147 |
|
|
return 1; |
148 |
|
|
else if (sequenceNumber < x.sequenceNumber) |
149 |
|
|
return -1; |
150 |
|
|
else |
151 |
|
|
return 1; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
/** |
155 |
dl |
1.18 |
* Returns true if this is a periodic (not a one-shot) action. |
156 |
dl |
1.1 |
* @return true if periodic |
157 |
|
|
*/ |
158 |
dl |
1.23 |
public boolean isPeriodic() { |
159 |
dl |
1.16 |
return period != 0; |
160 |
dl |
1.1 |
} |
161 |
|
|
|
162 |
|
|
/** |
163 |
dl |
1.14 |
* Run a periodic task |
164 |
dl |
1.13 |
*/ |
165 |
|
|
private void runPeriodic() { |
166 |
|
|
boolean ok = ScheduledFutureTask.super.runAndReset(); |
167 |
|
|
boolean down = isShutdown(); |
168 |
|
|
// Reschedule if not cancelled and not shutdown or policy allows |
169 |
|
|
if (ok && (!down || |
170 |
jsr166 |
1.21 |
(getContinueExistingPeriodicTasksAfterShutdownPolicy() && |
171 |
dl |
1.13 |
!isTerminating()))) { |
172 |
dl |
1.16 |
long p = period; |
173 |
|
|
if (p > 0) |
174 |
|
|
time += p; |
175 |
|
|
else |
176 |
|
|
time = now() - p; |
177 |
dl |
1.13 |
ScheduledThreadPoolExecutor.super.getQueue().add(this); |
178 |
|
|
} |
179 |
|
|
// This might have been the final executed delayed |
180 |
|
|
// task. Wake up threads to check. |
181 |
jsr166 |
1.21 |
else if (down) |
182 |
dl |
1.13 |
interruptIdleWorkers(); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
dl |
1.5 |
* Overrides FutureTask version so as to reset/requeue if periodic. |
187 |
jsr166 |
1.21 |
*/ |
188 |
dl |
1.1 |
public void run() { |
189 |
dl |
1.13 |
if (isPeriodic()) |
190 |
|
|
runPeriodic(); |
191 |
jsr166 |
1.21 |
else |
192 |
dl |
1.5 |
ScheduledFutureTask.super.run(); |
193 |
dl |
1.1 |
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
dl |
1.13 |
* Specialized variant of ThreadPoolExecutor.execute for delayed tasks. |
198 |
|
|
*/ |
199 |
|
|
private void delayedExecute(Runnable command) { |
200 |
|
|
if (isShutdown()) { |
201 |
|
|
reject(command); |
202 |
|
|
return; |
203 |
dl |
1.1 |
} |
204 |
dl |
1.13 |
// Prestart a thread if necessary. We cannot prestart it |
205 |
|
|
// running the task because the task (probably) shouldn't be |
206 |
|
|
// run yet, so thread will just idle until delay elapses. |
207 |
|
|
if (getPoolSize() < getCorePoolSize()) |
208 |
|
|
prestartCoreThread(); |
209 |
jsr166 |
1.21 |
|
210 |
dl |
1.13 |
super.getQueue().add(command); |
211 |
|
|
} |
212 |
dl |
1.1 |
|
213 |
dl |
1.13 |
/** |
214 |
jsr166 |
1.21 |
* Cancels and clears the queue of all tasks that should not be run |
215 |
dl |
1.13 |
* due to shutdown policy. |
216 |
|
|
*/ |
217 |
|
|
private void cancelUnwantedTasks() { |
218 |
|
|
boolean keepDelayed = getExecuteExistingDelayedTasksAfterShutdownPolicy(); |
219 |
|
|
boolean keepPeriodic = getContinueExistingPeriodicTasksAfterShutdownPolicy(); |
220 |
jsr166 |
1.21 |
if (!keepDelayed && !keepPeriodic) |
221 |
dl |
1.13 |
super.getQueue().clear(); |
222 |
|
|
else if (keepDelayed || keepPeriodic) { |
223 |
|
|
Object[] entries = super.getQueue().toArray(); |
224 |
|
|
for (int i = 0; i < entries.length; ++i) { |
225 |
|
|
Object e = entries[i]; |
226 |
dl |
1.23 |
if (e instanceof RunnableScheduledFuture) { |
227 |
|
|
RunnableScheduledFuture<?> t = (RunnableScheduledFuture<?>)e; |
228 |
dl |
1.13 |
if (t.isPeriodic()? !keepPeriodic : !keepDelayed) |
229 |
|
|
t.cancel(false); |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
entries = null; |
233 |
|
|
purge(); |
234 |
dl |
1.1 |
} |
235 |
|
|
} |
236 |
|
|
|
237 |
dl |
1.19 |
public boolean remove(Runnable task) { |
238 |
dl |
1.23 |
if (!(task instanceof RunnableScheduledFuture)) |
239 |
peierls |
1.22 |
return false; |
240 |
|
|
return getQueue().remove(task); |
241 |
|
|
} |
242 |
|
|
|
243 |
dl |
1.23 |
/** |
244 |
|
|
* Modify or replace the task used to execute a runnable. |
245 |
jsr166 |
1.28 |
* This method can be used to override the concrete |
246 |
dl |
1.23 |
* class used for managing internal tasks. |
247 |
|
|
* The default implementation simply returns the given |
248 |
|
|
* task. |
249 |
jsr166 |
1.28 |
* |
250 |
dl |
1.23 |
* @param runnable the submitted Runnable |
251 |
|
|
* @param task the task created to execute the runnable |
252 |
|
|
* @return a task that can execute the runnable |
253 |
|
|
* @since 1.6 |
254 |
|
|
*/ |
255 |
peierls |
1.22 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
256 |
dl |
1.23 |
Runnable runnable, RunnableScheduledFuture<V> task) { |
257 |
|
|
return task; |
258 |
peierls |
1.22 |
} |
259 |
|
|
|
260 |
dl |
1.23 |
/** |
261 |
|
|
* Modify or replace the task used to execute a callable. |
262 |
jsr166 |
1.28 |
* This method can be used to override the concrete |
263 |
dl |
1.23 |
* class used for managing internal tasks. |
264 |
|
|
* The default implementation simply returns the given |
265 |
|
|
* task. |
266 |
jsr166 |
1.28 |
* |
267 |
dl |
1.23 |
* @param callable the submitted Callable |
268 |
|
|
* @param task the task created to execute the callable |
269 |
|
|
* @return a task that can execute the callable |
270 |
|
|
* @since 1.6 |
271 |
|
|
*/ |
272 |
peierls |
1.22 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
273 |
dl |
1.23 |
Callable<V> callable, RunnableScheduledFuture<V> task) { |
274 |
|
|
return task; |
275 |
dl |
1.19 |
} |
276 |
|
|
|
277 |
dl |
1.1 |
/** |
278 |
dl |
1.13 |
* Creates a new ScheduledThreadPoolExecutor with the given core |
279 |
|
|
* pool size. |
280 |
jsr166 |
1.21 |
* |
281 |
dl |
1.1 |
* @param corePoolSize the number of threads to keep in the pool, |
282 |
|
|
* even if they are idle. |
283 |
jsr166 |
1.26 |
* @throws IllegalArgumentException if <tt>corePoolSize <= 0</tt> |
284 |
dl |
1.1 |
*/ |
285 |
|
|
public ScheduledThreadPoolExecutor(int corePoolSize) { |
286 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
287 |
|
|
new DelayedWorkQueue()); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
/** |
291 |
dl |
1.13 |
* Creates a new ScheduledThreadPoolExecutor with the given |
292 |
|
|
* initial parameters. |
293 |
jsr166 |
1.21 |
* |
294 |
dl |
1.1 |
* @param corePoolSize the number of threads to keep in the pool, |
295 |
|
|
* even if they are idle. |
296 |
|
|
* @param threadFactory the factory to use when the executor |
297 |
jsr166 |
1.21 |
* creates a new thread. |
298 |
jsr166 |
1.26 |
* @throws IllegalArgumentException if <tt>corePoolSize <= 0</tt> |
299 |
dl |
1.1 |
* @throws NullPointerException if threadFactory is null |
300 |
|
|
*/ |
301 |
|
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
302 |
|
|
ThreadFactory threadFactory) { |
303 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
304 |
|
|
new DelayedWorkQueue(), threadFactory); |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
dl |
1.13 |
* Creates a new ScheduledThreadPoolExecutor with the given |
309 |
|
|
* initial parameters. |
310 |
jsr166 |
1.21 |
* |
311 |
dl |
1.1 |
* @param corePoolSize the number of threads to keep in the pool, |
312 |
|
|
* even if they are idle. |
313 |
|
|
* @param handler the handler to use when execution is blocked |
314 |
|
|
* because the thread bounds and queue capacities are reached. |
315 |
jsr166 |
1.26 |
* @throws IllegalArgumentException if <tt>corePoolSize <= 0</tt> |
316 |
dl |
1.1 |
* @throws NullPointerException if handler is null |
317 |
|
|
*/ |
318 |
|
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
319 |
|
|
RejectedExecutionHandler handler) { |
320 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
321 |
|
|
new DelayedWorkQueue(), handler); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
/** |
325 |
dl |
1.13 |
* Creates a new ScheduledThreadPoolExecutor with the given |
326 |
|
|
* initial parameters. |
327 |
jsr166 |
1.21 |
* |
328 |
dl |
1.1 |
* @param corePoolSize the number of threads to keep in the pool, |
329 |
|
|
* even if they are idle. |
330 |
|
|
* @param threadFactory the factory to use when the executor |
331 |
jsr166 |
1.21 |
* creates a new thread. |
332 |
dl |
1.1 |
* @param handler the handler to use when execution is blocked |
333 |
|
|
* because the thread bounds and queue capacities are reached. |
334 |
jsr166 |
1.26 |
* @throws IllegalArgumentException if <tt>corePoolSize <= 0</tt> |
335 |
dl |
1.1 |
* @throws NullPointerException if threadFactory or handler is null |
336 |
|
|
*/ |
337 |
|
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
338 |
|
|
ThreadFactory threadFactory, |
339 |
|
|
RejectedExecutionHandler handler) { |
340 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
341 |
|
|
new DelayedWorkQueue(), threadFactory, handler); |
342 |
|
|
} |
343 |
|
|
|
344 |
jsr166 |
1.21 |
public ScheduledFuture<?> schedule(Runnable command, |
345 |
|
|
long delay, |
346 |
dl |
1.13 |
TimeUnit unit) { |
347 |
dl |
1.9 |
if (command == null || unit == null) |
348 |
dl |
1.1 |
throw new NullPointerException(); |
349 |
dl |
1.14 |
long triggerTime = now() + unit.toNanos(delay); |
350 |
peierls |
1.22 |
RunnableScheduledFuture<?> t = decorateTask(command, |
351 |
|
|
new ScheduledFutureTask<Boolean>(command, null, triggerTime)); |
352 |
dl |
1.1 |
delayedExecute(t); |
353 |
|
|
return t; |
354 |
|
|
} |
355 |
|
|
|
356 |
jsr166 |
1.21 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, |
357 |
|
|
long delay, |
358 |
dl |
1.13 |
TimeUnit unit) { |
359 |
dl |
1.9 |
if (callable == null || unit == null) |
360 |
dl |
1.1 |
throw new NullPointerException(); |
361 |
dl |
1.16 |
if (delay < 0) delay = 0; |
362 |
dl |
1.14 |
long triggerTime = now() + unit.toNanos(delay); |
363 |
peierls |
1.22 |
RunnableScheduledFuture<V> t = decorateTask(callable, |
364 |
|
|
new ScheduledFutureTask<V>(callable, triggerTime)); |
365 |
dl |
1.1 |
delayedExecute(t); |
366 |
|
|
return t; |
367 |
|
|
} |
368 |
|
|
|
369 |
jsr166 |
1.21 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, |
370 |
|
|
long initialDelay, |
371 |
|
|
long period, |
372 |
dl |
1.13 |
TimeUnit unit) { |
373 |
dl |
1.9 |
if (command == null || unit == null) |
374 |
dl |
1.1 |
throw new NullPointerException(); |
375 |
|
|
if (period <= 0) |
376 |
|
|
throw new IllegalArgumentException(); |
377 |
dl |
1.16 |
if (initialDelay < 0) initialDelay = 0; |
378 |
dl |
1.14 |
long triggerTime = now() + unit.toNanos(initialDelay); |
379 |
peierls |
1.22 |
RunnableScheduledFuture<?> t = decorateTask(command, |
380 |
jsr166 |
1.21 |
new ScheduledFutureTask<Object>(command, |
381 |
dl |
1.13 |
null, |
382 |
|
|
triggerTime, |
383 |
peierls |
1.22 |
unit.toNanos(period))); |
384 |
dl |
1.1 |
delayedExecute(t); |
385 |
|
|
return t; |
386 |
|
|
} |
387 |
jsr166 |
1.21 |
|
388 |
|
|
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, |
389 |
|
|
long initialDelay, |
390 |
|
|
long delay, |
391 |
dl |
1.13 |
TimeUnit unit) { |
392 |
dl |
1.9 |
if (command == null || unit == null) |
393 |
dl |
1.1 |
throw new NullPointerException(); |
394 |
|
|
if (delay <= 0) |
395 |
|
|
throw new IllegalArgumentException(); |
396 |
dl |
1.16 |
if (initialDelay < 0) initialDelay = 0; |
397 |
dl |
1.14 |
long triggerTime = now() + unit.toNanos(initialDelay); |
398 |
peierls |
1.22 |
RunnableScheduledFuture<?> t = decorateTask(command, |
399 |
jsr166 |
1.21 |
new ScheduledFutureTask<Boolean>(command, |
400 |
dl |
1.13 |
null, |
401 |
|
|
triggerTime, |
402 |
peierls |
1.22 |
unit.toNanos(-delay))); |
403 |
dl |
1.1 |
delayedExecute(t); |
404 |
|
|
return t; |
405 |
|
|
} |
406 |
jsr166 |
1.21 |
|
407 |
dl |
1.1 |
|
408 |
|
|
/** |
409 |
jsr166 |
1.21 |
* Executes command with zero required delay. This has effect |
410 |
dl |
1.1 |
* equivalent to <tt>schedule(command, 0, anyUnit)</tt>. Note |
411 |
|
|
* that inspections of the queue and of the list returned by |
412 |
|
|
* <tt>shutdownNow</tt> will access the zero-delayed |
413 |
|
|
* {@link ScheduledFuture}, not the <tt>command</tt> itself. |
414 |
|
|
* |
415 |
|
|
* @param command the task to execute |
416 |
|
|
* @throws RejectedExecutionException at discretion of |
417 |
|
|
* <tt>RejectedExecutionHandler</tt>, if task cannot be accepted |
418 |
|
|
* for execution because the executor has been shut down. |
419 |
|
|
* @throws NullPointerException if command is null |
420 |
|
|
*/ |
421 |
|
|
public void execute(Runnable command) { |
422 |
|
|
if (command == null) |
423 |
|
|
throw new NullPointerException(); |
424 |
|
|
schedule(command, 0, TimeUnit.NANOSECONDS); |
425 |
|
|
} |
426 |
|
|
|
427 |
dl |
1.13 |
// Override AbstractExecutorService methods |
428 |
|
|
|
429 |
dl |
1.7 |
public Future<?> submit(Runnable task) { |
430 |
|
|
return schedule(task, 0, TimeUnit.NANOSECONDS); |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
public <T> Future<T> submit(Runnable task, T result) { |
434 |
jsr166 |
1.21 |
return schedule(Executors.callable(task, result), |
435 |
dl |
1.13 |
0, TimeUnit.NANOSECONDS); |
436 |
dl |
1.7 |
} |
437 |
|
|
|
438 |
|
|
public <T> Future<T> submit(Callable<T> task) { |
439 |
|
|
return schedule(task, 0, TimeUnit.NANOSECONDS); |
440 |
|
|
} |
441 |
dl |
1.1 |
|
442 |
|
|
/** |
443 |
jsr166 |
1.21 |
* Sets the policy on whether to continue executing existing periodic |
444 |
dl |
1.1 |
* tasks even when this executor has been <tt>shutdown</tt>. In |
445 |
|
|
* this case, these tasks will only terminate upon |
446 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
447 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
448 |
|
|
* false. |
449 |
|
|
* @param value if true, continue after shutdown, else don't. |
450 |
jsr166 |
1.25 |
* @see #getContinueExistingPeriodicTasksAfterShutdownPolicy |
451 |
dl |
1.1 |
*/ |
452 |
|
|
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) { |
453 |
|
|
continueExistingPeriodicTasksAfterShutdown = value; |
454 |
|
|
if (!value && isShutdown()) |
455 |
|
|
cancelUnwantedTasks(); |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
/** |
459 |
jsr166 |
1.21 |
* Gets the policy on whether to continue executing existing |
460 |
dl |
1.1 |
* periodic tasks even when this executor has been |
461 |
|
|
* <tt>shutdown</tt>. In this case, these tasks will only |
462 |
|
|
* terminate upon <tt>shutdownNow</tt> or after setting the policy |
463 |
|
|
* to <tt>false</tt> when already shutdown. This value is by |
464 |
|
|
* default false. |
465 |
|
|
* @return true if will continue after shutdown. |
466 |
dl |
1.16 |
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy |
467 |
dl |
1.1 |
*/ |
468 |
|
|
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() { |
469 |
|
|
return continueExistingPeriodicTasksAfterShutdown; |
470 |
|
|
} |
471 |
|
|
|
472 |
|
|
/** |
473 |
jsr166 |
1.21 |
* Sets the policy on whether to execute existing delayed |
474 |
dl |
1.1 |
* tasks even when this executor has been <tt>shutdown</tt>. In |
475 |
|
|
* this case, these tasks will only terminate upon |
476 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
477 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
478 |
|
|
* true. |
479 |
|
|
* @param value if true, execute after shutdown, else don't. |
480 |
dl |
1.16 |
* @see #getExecuteExistingDelayedTasksAfterShutdownPolicy |
481 |
dl |
1.1 |
*/ |
482 |
|
|
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) { |
483 |
|
|
executeExistingDelayedTasksAfterShutdown = value; |
484 |
|
|
if (!value && isShutdown()) |
485 |
|
|
cancelUnwantedTasks(); |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
/** |
489 |
jsr166 |
1.21 |
* Gets the policy on whether to execute existing delayed |
490 |
dl |
1.1 |
* tasks even when this executor has been <tt>shutdown</tt>. In |
491 |
|
|
* this case, these tasks will only terminate upon |
492 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
493 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
494 |
|
|
* true. |
495 |
|
|
* @return true if will execute after shutdown. |
496 |
dl |
1.16 |
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy |
497 |
dl |
1.1 |
*/ |
498 |
|
|
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() { |
499 |
|
|
return executeExistingDelayedTasksAfterShutdown; |
500 |
|
|
} |
501 |
|
|
|
502 |
|
|
|
503 |
|
|
/** |
504 |
|
|
* Initiates an orderly shutdown in which previously submitted |
505 |
|
|
* tasks are executed, but no new tasks will be accepted. If the |
506 |
|
|
* <tt>ExecuteExistingDelayedTasksAfterShutdownPolicy</tt> has |
507 |
|
|
* been set <tt>false</tt>, existing delayed tasks whose delays |
508 |
|
|
* have not yet elapsed are cancelled. And unless the |
509 |
|
|
* <tt>ContinueExistingPeriodicTasksAfterShutdownPolicy</tt> has |
510 |
|
|
* been set <tt>true</tt>, future executions of existing periodic |
511 |
|
|
* tasks will be cancelled. |
512 |
|
|
*/ |
513 |
|
|
public void shutdown() { |
514 |
|
|
cancelUnwantedTasks(); |
515 |
|
|
super.shutdown(); |
516 |
|
|
} |
517 |
|
|
|
518 |
|
|
/** |
519 |
|
|
* Attempts to stop all actively executing tasks, halts the |
520 |
|
|
* processing of waiting tasks, and returns a list of the tasks that were |
521 |
jsr166 |
1.21 |
* awaiting execution. |
522 |
|
|
* |
523 |
dl |
1.1 |
* <p>There are no guarantees beyond best-effort attempts to stop |
524 |
dl |
1.18 |
* processing actively executing tasks. This implementation |
525 |
|
|
* cancels tasks via {@link Thread#interrupt}, so if any tasks mask or |
526 |
dl |
1.1 |
* fail to respond to interrupts, they may never terminate. |
527 |
|
|
* |
528 |
|
|
* @return list of tasks that never commenced execution. Each |
529 |
|
|
* element of this list is a {@link ScheduledFuture}, |
530 |
dl |
1.18 |
* including those tasks submitted using <tt>execute</tt>, which |
531 |
dl |
1.1 |
* are for scheduling purposes used as the basis of a zero-delay |
532 |
|
|
* <tt>ScheduledFuture</tt>. |
533 |
|
|
*/ |
534 |
tim |
1.4 |
public List<Runnable> shutdownNow() { |
535 |
dl |
1.1 |
return super.shutdownNow(); |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
/** |
539 |
|
|
* Returns the task queue used by this executor. Each element of |
540 |
|
|
* this queue is a {@link ScheduledFuture}, including those |
541 |
|
|
* tasks submitted using <tt>execute</tt> which are for scheduling |
542 |
|
|
* purposes used as the basis of a zero-delay |
543 |
|
|
* <tt>ScheduledFuture</tt>. Iteration over this queue is |
544 |
dl |
1.15 |
* <em>not</em> guaranteed to traverse tasks in the order in |
545 |
dl |
1.1 |
* which they will execute. |
546 |
|
|
* |
547 |
|
|
* @return the task queue |
548 |
|
|
*/ |
549 |
|
|
public BlockingQueue<Runnable> getQueue() { |
550 |
|
|
return super.getQueue(); |
551 |
|
|
} |
552 |
|
|
|
553 |
dl |
1.13 |
/** |
554 |
jsr166 |
1.27 |
* An annoying wrapper class to convince javac to use a |
555 |
|
|
* DelayQueue<RunnableScheduledFuture> as a BlockingQueue<Runnable> |
556 |
jsr166 |
1.21 |
*/ |
557 |
|
|
private static class DelayedWorkQueue |
558 |
|
|
extends AbstractCollection<Runnable> |
559 |
dl |
1.13 |
implements BlockingQueue<Runnable> { |
560 |
jsr166 |
1.21 |
|
561 |
dl |
1.23 |
private final DelayQueue<RunnableScheduledFuture> dq = new DelayQueue<RunnableScheduledFuture>(); |
562 |
dl |
1.13 |
public Runnable poll() { return dq.poll(); } |
563 |
|
|
public Runnable peek() { return dq.peek(); } |
564 |
|
|
public Runnable take() throws InterruptedException { return dq.take(); } |
565 |
|
|
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException { |
566 |
|
|
return dq.poll(timeout, unit); |
567 |
|
|
} |
568 |
|
|
|
569 |
jsr166 |
1.27 |
public boolean add(Runnable x) { |
570 |
|
|
return dq.add((RunnableScheduledFuture)x); |
571 |
|
|
} |
572 |
|
|
public boolean offer(Runnable x) { |
573 |
|
|
return dq.offer((RunnableScheduledFuture)x); |
574 |
|
|
} |
575 |
dl |
1.13 |
public void put(Runnable x) { |
576 |
dl |
1.23 |
dq.put((RunnableScheduledFuture)x); |
577 |
dl |
1.13 |
} |
578 |
|
|
public boolean offer(Runnable x, long timeout, TimeUnit unit) { |
579 |
dl |
1.23 |
return dq.offer((RunnableScheduledFuture)x, timeout, unit); |
580 |
dl |
1.13 |
} |
581 |
|
|
|
582 |
|
|
public Runnable remove() { return dq.remove(); } |
583 |
|
|
public Runnable element() { return dq.element(); } |
584 |
|
|
public void clear() { dq.clear(); } |
585 |
|
|
public int drainTo(Collection<? super Runnable> c) { return dq.drainTo(c); } |
586 |
jsr166 |
1.21 |
public int drainTo(Collection<? super Runnable> c, int maxElements) { |
587 |
|
|
return dq.drainTo(c, maxElements); |
588 |
dl |
1.13 |
} |
589 |
|
|
|
590 |
|
|
public int remainingCapacity() { return dq.remainingCapacity(); } |
591 |
|
|
public boolean remove(Object x) { return dq.remove(x); } |
592 |
|
|
public boolean contains(Object x) { return dq.contains(x); } |
593 |
|
|
public int size() { return dq.size(); } |
594 |
|
|
public boolean isEmpty() { return dq.isEmpty(); } |
595 |
|
|
public Object[] toArray() { return dq.toArray(); } |
596 |
|
|
public <T> T[] toArray(T[] array) { return dq.toArray(array); } |
597 |
jsr166 |
1.21 |
public Iterator<Runnable> iterator() { |
598 |
dl |
1.13 |
return new Iterator<Runnable>() { |
599 |
dl |
1.23 |
private Iterator<RunnableScheduledFuture> it = dq.iterator(); |
600 |
dl |
1.13 |
public boolean hasNext() { return it.hasNext(); } |
601 |
|
|
public Runnable next() { return it.next(); } |
602 |
|
|
public void remove() { it.remove(); } |
603 |
|
|
}; |
604 |
|
|
} |
605 |
|
|
} |
606 |
dl |
1.1 |
} |