ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ScheduledExecutor.java
Revision: 1.19
Committed: Thu Aug 14 15:59:04 2003 UTC (20 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.18: +1 -1 lines
Log Message:
javadoc typo fix

File Contents

# User Rev Content
1 tim 1.1 /*
2 dl 1.4 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain. Use, modify, and
4     * redistribute this code in any way without acknowledgement.
5 tim 1.1 */
6    
7     package java.util.concurrent;
8 dl 1.4 import java.util.concurrent.atomic.*;
9 dl 1.2 import java.util.*;
10 tim 1.1
11     /**
12 dl 1.17 * An <tt>Executor</tt> that can schedule commands to run after a given
13 dl 1.7 * delay, or to execute periodically. This class is preferable to
14 tim 1.1 * <tt>java.util.Timer</tt> when multiple worker threads are needed,
15     * or when the additional flexibility or capabilities of
16 dl 1.7 * <tt>ThreadPoolExecutor</tt> (which this class extends) are
17     * required.
18     *
19     * <p> The <tt>schedule</tt> methods create tasks with various delays
20     * and return a task object that can be used to cancel or check
21     * execution. The <tt>scheduleAtFixedRate</tt> and
22     * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
23     * that run periodically until cancelled. Commands submitted using
24     * the <tt>execute</tt> method are scheduled with a requested delay of
25     * zero.
26     *
27     * <p> Delayed tasks execute no sooner than they are enabled, but
28     * without any real-time guarantees about when, after they are enabled
29     * they will commence. Tasks tied for the same execution time are
30     * enabled in first-in-first-out (FIFO) order of submission. An
31     * internal {@link DelayQueue} used for scheduling relies on relative
32     * delays, which may drift from absolute times (as returned by
33     * <tt>System.currentTimeMillis</tt>) over sufficiently long periods.
34 tim 1.1 *
35 dl 1.13 * <p>While this class inherits from {@link ThreadPoolExecutor}, a few
36     * of the inherited tuning methods are not especially useful for
37     * it. In particular, because a <tt>ScheduledExecutor</tt> always acts
38     * as a fixed-sized pool using <tt>corePoolSize</tt> threads and an
39     * unbounded queue, adjustments to <tt>maximumPoolSize</tt> have no
40     * useful effect.
41     *
42 tim 1.1 * @since 1.5
43     * @see Executors
44     *
45     * @spec JSR-166
46 dl 1.9 * @author Doug Lea
47 tim 1.1 */
48     public class ScheduledExecutor extends ThreadPoolExecutor {
49    
50 dl 1.4 /**
51 dl 1.18 * False if should cancel/suppress periodic tasks on shutdown.
52     */
53     private volatile boolean continueExistingPeriodicTasksAfterShutdown;
54    
55     /**
56     * False if should cancel non-periodic tasks on shutdown.
57     */
58     private volatile boolean executeExistingDelayedTasksAfterShutdown = true;
59    
60    
61     /**
62 dl 1.5 * Sequence number to break scheduling ties, and in turn to
63     * guarantee FIFO order among tied entries.
64     */
65     private static final AtomicLong sequencer = new AtomicLong(0);
66    
67     /**
68 dl 1.4 * A delayed or periodic action.
69     */
70 dl 1.8 public static class DelayedTask extends CancellableTask implements Delayed {
71 dl 1.9 /** Sequence number to break ties FIFO */
72 dl 1.4 private final long sequenceNumber;
73 dl 1.9 /** The time the task is enabled to execute in nanoTime units */
74 dl 1.4 private final long time;
75 dl 1.9 /** The delay forllowing next time, or <= 0 if non-periodic */
76 dl 1.4 private final long period;
77 dl 1.9 /** true if at fixed rate; false if fixed delay */
78     private final boolean rateBased;
79    
80 dl 1.4 /**
81 dl 1.5 * Creates a one-shot action with given nanoTime-based trigger time
82 dl 1.4 */
83     DelayedTask(Runnable r, long ns) {
84     super(r);
85     this.time = ns;
86     this.period = 0;
87 dl 1.7 rateBased = false;
88 dl 1.4 this.sequenceNumber = sequencer.getAndIncrement();
89     }
90    
91     /**
92 dl 1.5 * Creates a periodic action with given nano time and period
93 dl 1.4 */
94 dl 1.7 DelayedTask(Runnable r, long ns, long period, boolean rateBased) {
95 dl 1.4 super(r);
96     if (period <= 0)
97     throw new IllegalArgumentException();
98     this.time = ns;
99     this.period = period;
100 dl 1.7 this.rateBased = rateBased;
101 dl 1.4 this.sequenceNumber = sequencer.getAndIncrement();
102     }
103    
104    
105     public long getDelay(TimeUnit unit) {
106 dl 1.12 long d = unit.convert(time - System.nanoTime(),
107 dl 1.7 TimeUnit.NANOSECONDS);
108 dl 1.12 return d;
109 dl 1.4 }
110    
111     public int compareTo(Object other) {
112 dl 1.18 if (other == this) // compare zero ONLY if same object
113 dl 1.15 return 0;
114 dl 1.4 DelayedTask x = (DelayedTask)other;
115     long diff = time - x.time;
116     if (diff < 0)
117     return -1;
118     else if (diff > 0)
119     return 1;
120     else if (sequenceNumber < x.sequenceNumber)
121     return -1;
122     else
123     return 1;
124     }
125    
126     /**
127     * Return true if this is a periodic (not a one-shot) action.
128 dl 1.9 * @return true if periodic
129 dl 1.4 */
130     public boolean isPeriodic() {
131     return period > 0;
132     }
133    
134     /**
135 tim 1.11 * Returns the period, or zero if non-periodic.
136     *
137 dl 1.9 * @return the period
138 dl 1.4 */
139     public long getPeriod(TimeUnit unit) {
140     return unit.convert(period, TimeUnit.NANOSECONDS);
141     }
142    
143     /**
144     * Return a new DelayedTask that will trigger in the period
145     * subsequent to current task, or null if non-periodic
146     * or canceled.
147     */
148 dl 1.5 DelayedTask nextTask() {
149 dl 1.4 if (period <= 0 || isCancelled())
150     return null;
151 dl 1.10 long nextTime = period + (rateBased ? time : System.nanoTime());
152 dl 1.7 return new DelayedTask(getRunnable(), nextTime, period, rateBased);
153 dl 1.4 }
154     }
155 dl 1.2
156 dl 1.4 /**
157     * A delayed result-bearing action.
158     */
159 dl 1.8 public static class DelayedFutureTask<V> extends DelayedTask implements Future<V> {
160 dl 1.4 /**
161     * Creates a Future that may trigger after the given delay.
162     */
163 dl 1.17 DelayedFutureTask(Callable<V> callable, long triggerTime) {
164 dl 1.4 // must set after super ctor call to use inner class
165 dl 1.17 super(null, triggerTime);
166 dl 1.4 setRunnable(new InnerCancellableFuture<V>(callable));
167 dl 1.2 }
168    
169 dl 1.4 public V get() throws InterruptedException, ExecutionException {
170     return ((InnerCancellableFuture<V>)getRunnable()).get();
171 dl 1.2 }
172    
173 dl 1.4 public V get(long timeout, TimeUnit unit)
174     throws InterruptedException, ExecutionException, TimeoutException {
175     return ((InnerCancellableFuture<V>)getRunnable()).get(timeout, unit);
176 dl 1.2 }
177 tim 1.1
178 dl 1.4 protected void set(V v) {
179     ((InnerCancellableFuture<V>)getRunnable()).set(v);
180 dl 1.2 }
181 tim 1.1
182 dl 1.4 protected void setException(Throwable t) {
183     ((InnerCancellableFuture<V>)getRunnable()).setException(t);
184     }
185     }
186    
187    
188     /**
189     * An annoying wrapper class to convince generics compiler to
190 dl 1.7 * use a DelayQueue<DelayedTask> as a BlockingQueue<Runnable>
191 dl 1.4 */
192 dl 1.12 private static class DelayedWorkQueue extends AbstractCollection<Runnable> implements BlockingQueue<Runnable> {
193 dl 1.4 private final DelayQueue<DelayedTask> dq = new DelayQueue<DelayedTask>();
194     public Runnable poll() { return dq.poll(); }
195     public Runnable peek() { return dq.peek(); }
196     public Runnable take() throws InterruptedException { return dq.take(); }
197     public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
198     return dq.poll(timeout, unit);
199 dl 1.2 }
200 dl 1.12
201     public boolean add(Runnable x) { return dq.add((DelayedTask)x); }
202 dl 1.4 public boolean offer(Runnable x) { return dq.offer((DelayedTask)x); }
203     public void put(Runnable x) throws InterruptedException {
204     dq.put((DelayedTask)x);
205 dl 1.2 }
206 dl 1.4 public boolean offer(Runnable x, long timeout, TimeUnit unit) throws InterruptedException {
207     return dq.offer((DelayedTask)x, timeout, unit);
208 dl 1.2 }
209 dl 1.12
210     public Runnable remove() { return dq.remove(); }
211     public Runnable element() { return dq.element(); }
212     public void clear() { dq.clear(); }
213    
214 dl 1.4 public int remainingCapacity() { return dq.remainingCapacity(); }
215     public boolean remove(Object x) { return dq.remove(x); }
216     public boolean contains(Object x) { return dq.contains(x); }
217     public int size() { return dq.size(); }
218 tim 1.11 public boolean isEmpty() { return dq.isEmpty(); }
219 dl 1.4 public Iterator<Runnable> iterator() {
220     return new Iterator<Runnable>() {
221     private Iterator<DelayedTask> it = dq.iterator();
222     public boolean hasNext() { return it.hasNext(); }
223     public Runnable next() { return it.next(); }
224     public void remove() { it.remove(); }
225     };
226 tim 1.1 }
227 dl 1.4 }
228 tim 1.1
229 dl 1.4 /**
230     * Creates a new ScheduledExecutor with the given initial parameters.
231     *
232     * @param corePoolSize the number of threads to keep in the pool,
233     * even if they are idle.
234     */
235     public ScheduledExecutor(int corePoolSize) {
236     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
237     new DelayedWorkQueue());
238     }
239 tim 1.1
240 dl 1.4 /**
241     * Creates a new ScheduledExecutor with the given initial parameters.
242     *
243     * @param corePoolSize the number of threads to keep in the pool,
244     * even if they are idle.
245     * @param threadFactory the factory to use when the executor
246     * creates a new thread.
247     */
248     public ScheduledExecutor(int corePoolSize,
249     ThreadFactory threadFactory) {
250     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
251     new DelayedWorkQueue(), threadFactory);
252 tim 1.1 }
253    
254 dl 1.4 /**
255     * Creates a new ScheduledExecutor with the given initial parameters.
256     *
257     * @param corePoolSize the number of threads to keep in the pool,
258     * even if they are idle.
259     * @param handler the handler to use when execution is blocked
260     * because the thread bounds and queue capacities are reached.
261     */
262     public ScheduledExecutor(int corePoolSize,
263     RejectedExecutionHandler handler) {
264     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
265     new DelayedWorkQueue(), handler);
266     }
267 dl 1.2
268 tim 1.1 /**
269 dl 1.2 * Creates a new ScheduledExecutor with the given initial parameters.
270     *
271     * @param corePoolSize the number of threads to keep in the pool,
272     * even if they are idle.
273 dl 1.4 * @param threadFactory the factory to use when the executor
274     * creates a new thread.
275     * @param handler the handler to use when execution is blocked
276     * because the thread bounds and queue capacities are reached.
277 tim 1.1 */
278 dl 1.4 public ScheduledExecutor(int corePoolSize,
279     ThreadFactory threadFactory,
280     RejectedExecutionHandler handler) {
281 dl 1.2 super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
282 dl 1.4 new DelayedWorkQueue(), threadFactory, handler);
283 dl 1.13 }
284    
285     /**
286     * Specialized variant of ThreadPoolExecutor.execute for delayed tasks.
287     */
288 dl 1.18 private void delayedExecute(Runnable command) {
289 dl 1.13 if (isShutdown()) {
290     reject(command);
291     return;
292     }
293 dl 1.18 // Prestart a thread if necessary. We cannot prestart it
294     // running the task because the task (probably) shouldn't be
295     // run yet, so thread will just idle until delay elapses.
296 dl 1.13 if (getPoolSize() < getCorePoolSize())
297 dl 1.18 prestartCoreThread();
298 dl 1.13
299     getQueue().offer(command);
300 dl 1.4 }
301    
302     /**
303 dl 1.7 * Creates and executes a one-shot action that becomes enabled after
304 dl 1.4 * the given delay.
305 dl 1.6 * @param command the task to execute.
306     * @param delay the time from now to delay execution.
307     * @param unit the time unit of the delay parameter.
308     * @return a handle that can be used to cancel the task.
309 dl 1.17 * @throws RejectedExecutionException if task cannot be scheduled
310     * for execution because the executor has been shut down.
311 dl 1.4 */
312    
313 dl 1.6 public DelayedTask schedule(Runnable command, long delay, TimeUnit unit) {
314 dl 1.17 long triggerTime = System.nanoTime() + unit.toNanos(delay);
315     DelayedTask t = new DelayedTask(command, triggerTime);
316 dl 1.13 delayedExecute(t);
317 dl 1.4 return t;
318     }
319    
320     /**
321 dl 1.9 * Creates and executes a one-shot action that becomes enabled
322     * after the given date.
323 dl 1.6 * @param command the task to execute.
324     * @param date the time to commence excution.
325     * @return a handle that can be used to cancel the task.
326     * @throws RejectedExecutionException if task cannot be scheduled
327     * for execution because the executor has been shut down.
328 dl 1.4 */
329 dl 1.6 public DelayedTask schedule(Runnable command, Date date) {
330 dl 1.17 long triggerTime = System.nanoTime() +
331     TimeUnit.MILLISECONDS.toNanos(date.getTime() -
332     System.currentTimeMillis());
333     DelayedTask t = new DelayedTask(command, triggerTime);
334 dl 1.13 delayedExecute(t);
335 dl 1.4 return t;
336     }
337    
338     /**
339 dl 1.7 * Creates and executes a periodic action that becomes enabled first
340 dl 1.4 * after the given initial delay, and subsequently with the given
341 dl 1.7 * period; that is executions will commence after
342     * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
343     * <tt>initialDelay + 2 * period</tt>, and so on.
344 dl 1.6 * @param command the task to execute.
345     * @param initialDelay the time to delay first execution.
346     * @param period the period between successive executions.
347     * @param unit the time unit of the delay and period parameters
348     * @return a handle that can be used to cancel the task.
349     * @throws RejectedExecutionException if task cannot be scheduled
350     * for execution because the executor has been shut down.
351 dl 1.4 */
352 dl 1.7 public DelayedTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
353 dl 1.17 long triggerTime = System.nanoTime() + unit.toNanos(initialDelay);
354     DelayedTask t = new DelayedTask(command,
355     triggerTime,
356     unit.toNanos(period),
357     true);
358 dl 1.13 delayedExecute(t);
359 dl 1.4 return t;
360     }
361    
362     /**
363 dl 1.7 * Creates a periodic action that becomes enabled first after the
364     * given date, and subsequently with the given period
365     * period; that is executions will commence after
366     * <tt>initialDate</tt> then <tt>initialDate+period</tt>, then
367     * <tt>initialDate + 2 * period</tt>, and so on.
368 dl 1.6 * @param command the task to execute.
369     * @param initialDate the time to delay first execution.
370 dl 1.7 * @param period the period between commencement of successive
371     * executions.
372 dl 1.6 * @param unit the time unit of the period parameter.
373     * @return a handle that can be used to cancel the task.
374     * @throws RejectedExecutionException if task cannot be scheduled
375     * for execution because the executor has been shut down.
376 dl 1.4 */
377 dl 1.7 public DelayedTask scheduleAtFixedRate(Runnable command, Date initialDate, long period, TimeUnit unit) {
378 dl 1.17 long triggerTime = System.nanoTime() +
379     TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() -
380     System.currentTimeMillis());
381     DelayedTask t = new DelayedTask(command,
382     triggerTime,
383     unit.toNanos(period),
384     true);
385 dl 1.13 delayedExecute(t);
386 dl 1.7 return t;
387     }
388    
389     /**
390     * Creates and executes a periodic action that becomes enabled first
391     * after the given initial delay, and and subsequently with the
392     * given delay between the termination of one execution and the
393     * commencement of the next.
394     * @param command the task to execute.
395     * @param initialDelay the time to delay first execution.
396     * @param delay the delay between the termination of one
397     * execution and the commencement of the next.
398     * @param unit the time unit of the delay and delay parameters
399     * @return a handle that can be used to cancel the task.
400     * @throws RejectedExecutionException if task cannot be scheduled
401     * for execution because the executor has been shut down.
402     */
403     public DelayedTask scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
404 dl 1.17 long triggerTime = System.nanoTime() + unit.toNanos(initialDelay);
405     DelayedTask t = new DelayedTask(command,
406     triggerTime,
407     unit.toNanos(delay),
408     false);
409 dl 1.13 delayedExecute(t);
410 dl 1.7 return t;
411     }
412    
413     /**
414     * Creates a periodic action that becomes enabled first after the
415     * given date, and subsequently with the given delay between
416     * the termination of one execution and the commencement of the
417     * next.
418     * @param command the task to execute.
419     * @param initialDate the time to delay first execution.
420     * @param delay the delay between the termination of one
421     * execution and the commencement of the next.
422     * @param unit the time unit of the delay parameter.
423     * @return a handle that can be used to cancel the task.
424     * @throws RejectedExecutionException if task cannot be scheduled
425     * for execution because the executor has been shut down.
426     */
427     public DelayedTask scheduleWithFixedDelay(Runnable command, Date initialDate, long delay, TimeUnit unit) {
428 dl 1.17 long triggerTime = System.nanoTime() +
429     TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() -
430     System.currentTimeMillis());
431     DelayedTask t = new DelayedTask(command,
432     triggerTime,
433     unit.toNanos(delay),
434     false);
435 dl 1.13 delayedExecute(t);
436 dl 1.4 return t;
437 tim 1.1 }
438    
439    
440 dl 1.4 /**
441 dl 1.7 * Creates and executes a Future that becomes enabled after the
442     * given delay.
443 dl 1.6 * @param callable the function to execute.
444     * @param delay the time from now to delay execution.
445     * @param unit the time unit of the delay parameter.
446     * @return a Future that can be used to extract result or cancel.
447     * @throws RejectedExecutionException if task cannot be scheduled
448     * for execution because the executor has been shut down.
449 dl 1.4 */
450     public <V> DelayedFutureTask<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
451 dl 1.17 long triggerTime = System.nanoTime() + unit.toNanos(delay);
452     DelayedFutureTask<V> t = new DelayedFutureTask<V>(callable,
453     triggerTime);
454 dl 1.13 delayedExecute(t);
455 dl 1.4 return t;
456     }
457    
458     /**
459 dl 1.7 * Creates and executes a one-shot action that becomes enabled after
460 dl 1.4 * the given date.
461 dl 1.6 * @param callable the function to execute.
462     * @param date the time to commence excution.
463     * @return a Future that can be used to extract result or cancel.
464     * @throws RejectedExecutionException if task cannot be scheduled
465     * for execution because the executor has been shut down.
466 dl 1.4 */
467     public <V> DelayedFutureTask<V> schedule(Callable<V> callable, Date date) {
468 dl 1.17 long triggerTime = System.nanoTime() +
469     TimeUnit.MILLISECONDS.toNanos(date.getTime() -
470     System.currentTimeMillis());
471     DelayedFutureTask<V> t = new DelayedFutureTask<V>(callable,
472     triggerTime);
473 dl 1.13 delayedExecute(t);
474 dl 1.4 return t;
475 tim 1.1 }
476    
477 dl 1.4 /**
478 dl 1.16 * Execute command with zero required delay. This has effect
479     * equivalent to <tt>schedule(command, 0, anyUnit)</tt>. Note
480     * that inspections of the queue and of the list returned by
481     * <tt>shutdownNow</tt> will access the zero-delayed
482     * <tt>DelayedTask</tt>, not the <tt>command</tt> itself.
483 tim 1.11 *
484 dl 1.6 * @param command the task to execute
485     * @throws RejectedExecutionException at discretion of
486     * <tt>RejectedExecutionHandler</tt>, if task cannot be accepted
487     * for execution because the executor has been shut down.
488 dl 1.4 */
489     public void execute(Runnable command) {
490     schedule(command, 0, TimeUnit.NANOSECONDS);
491 tim 1.1 }
492    
493 dl 1.18
494     /**
495     * Set policy on whether to continue executing existing periodic
496     * tasks even when this executor has been <tt>shutdown</tt>. In
497     * this case, these tasks will only terminate upon
498     * <tt>shutdownNow</tt>, or after setting the policy to
499     * <tt>false</tt> when already shutdown. This value is by default
500     * false.
501     * @param value if true, continue after shutdown, else don't.
502     */
503     public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) {
504     continueExistingPeriodicTasksAfterShutdown = value;
505     if (!value && isShutdown())
506     cancelUnwantedTasks();
507     }
508    
509     /**
510     * Get the policy on whether to continue executing existing
511     * periodic tasks even when this executor has been
512     * <tt>shutdown</tt>. In this case, these tasks will only
513     * terminate upon <tt>shutdownNow</tt> or after setting the policy
514     * to <tt>false</tt> when already shutdown. This value is by
515     * default false.
516     * @return true if will continue after shutdown.
517     */
518     public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() {
519     return continueExistingPeriodicTasksAfterShutdown;
520     }
521    
522     /**
523     * Set policy on whether to execute existing delayed
524     * tasks even when this executor has been <tt>shutdown</tt>. In
525     * this case, these tasks will only terminate upon
526     * <tt>shutdownNow</tt>, or after setting the policy to
527     * <tt>false</tt> when already shutdown. This value is by default
528     * true.
529     * @param value if true, execute after shutdown, else don't.
530     */
531     public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) {
532     executeExistingDelayedTasksAfterShutdown = value;
533     if (!value && isShutdown())
534     cancelUnwantedTasks();
535     }
536    
537     /**
538     * Set policy on whether to execute existing delayed
539     * tasks even when this executor has been <tt>shutdown</tt>. In
540     * this case, these tasks will only terminate upon
541     * <tt>shutdownNow</tt>, or after setting the policy to
542     * <tt>false</tt> when already shutdown. This value is by default
543     * true.
544     * @return true if will execute after shutdown.
545     */
546     public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() {
547     return executeExistingDelayedTasksAfterShutdown;
548     }
549    
550     /**
551     * Cancel and clear the queue of all tasks that should not be run
552     * due to shutdown policy.
553     */
554     private void cancelUnwantedTasks() {
555     boolean keepDelayed = getExecuteExistingDelayedTasksAfterShutdownPolicy();
556     boolean keepPeriodic = getContinueExistingPeriodicTasksAfterShutdownPolicy();
557     if (!keepDelayed && !keepPeriodic)
558     getQueue().clear();
559     else if (keepDelayed || keepPeriodic) {
560     Object[] entries = getQueue().toArray();
561     for (int i = 0; i < entries.length; ++i) {
562     DelayedTask t = (DelayedTask)entries[i];
563     if (t.isPeriodic()? !keepPeriodic : !keepDelayed)
564     t.cancel(false);
565     }
566     entries = null;
567     purge();
568     }
569     }
570    
571     /**
572     * Initiates an orderly shutdown in which previously submitted
573     * tasks are executed, but no new tasks will be accepted. If the
574     * <tt>ExecuteExistingDelayedTasksAfterShutdownPolicy</tt> has
575     * been set <tt>false</tt>, existing delayed tasks whose delays
576     * have not yet elapsed are cancelled. And unless the
577 dl 1.19 * <tt>ContinueExistingPeriodicTasksAfterShutdownPolicy</tt> hase
578 dl 1.18 * been set <tt>true</tt>, future executions of existing periodic
579     * tasks will be cancelled.
580     */
581     public void shutdown() {
582     cancelUnwantedTasks();
583     super.shutdown();
584     }
585    
586 dl 1.4 /**
587 dl 1.15 * Removes this task from internal queue if it is present, thus
588     * causing it not to be run if it has not already started. This
589     * method may be useful as one part of a cancellation scheme.
590     *
591     * @param task the task to remove
592     * @return true if the task was removed
593     */
594     public boolean remove(Runnable task) {
595     if (task instanceof DelayedTask && getQueue().remove(task))
596     return true;
597    
598     // The task might actually have been wrapped as a DelayedTask
599     // in execute(), in which case we need to maually traverse
600     // looking for it.
601    
602     DelayedTask wrap = null;
603     Object[] entries = getQueue().toArray();
604     for (int i = 0; i < entries.length; ++i) {
605     DelayedTask t = (DelayedTask)entries[i];
606     Runnable r = t.getRunnable();
607     if (task.equals(r)) {
608     wrap = t;
609     break;
610     }
611     }
612     entries = null;
613     return wrap != null && getQueue().remove(wrap);
614     }
615    
616     /**
617 dl 1.4 * If executed task was periodic, cause the task for the next
618     * period to execute.
619 dl 1.9 * @param r the task (assumed to be a DelayedTask)
620     * @param t the exception
621 dl 1.4 */
622     protected void afterExecute(Runnable r, Throwable t) {
623     super.afterExecute(r, t);
624 dl 1.18 DelayedTask next = ((DelayedTask)r).nextTask();
625     if (next != null &&
626     (!isShutdown() ||
627     (getContinueExistingPeriodicTasksAfterShutdownPolicy() &&
628     !isTerminating())))
629     getQueue().offer(next);
630    
631     // This might have been the final executed delayed task. Wake
632     // up threads to check.
633     else if (isShutdown())
634     interruptIdleWorkers();
635 dl 1.4 }
636 tim 1.1 }