ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ScheduledExecutor.java
Revision: 1.11
Committed: Wed Aug 6 18:22:09 2003 UTC (20 years, 10 months ago) by tim
Branch: MAIN
CVS Tags: JSR166_CR1
Changes since 1.10: +5 -3 lines
Log Message:
Fixes to minor errors found by DocCheck

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.7 * An <tt>Executor</tt> that can schedule command to run after a given
13     * 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     * @since 1.5
36     * @see Executors
37     *
38     * @spec JSR-166
39 dl 1.9 * @author Doug Lea
40 tim 1.1 */
41     public class ScheduledExecutor extends ThreadPoolExecutor {
42    
43 dl 1.4 /**
44 dl 1.5 * Sequence number to break scheduling ties, and in turn to
45     * guarantee FIFO order among tied entries.
46     */
47     private static final AtomicLong sequencer = new AtomicLong(0);
48    
49     /**
50 dl 1.4 * A delayed or periodic action.
51     */
52 dl 1.8 public static class DelayedTask extends CancellableTask implements Delayed {
53 dl 1.9 /** Sequence number to break ties FIFO */
54 dl 1.4 private final long sequenceNumber;
55 dl 1.9 /** The time the task is enabled to execute in nanoTime units */
56 dl 1.4 private final long time;
57 dl 1.9 /** The delay forllowing next time, or <= 0 if non-periodic */
58 dl 1.4 private final long period;
59 dl 1.9 /** true if at fixed rate; false if fixed delay */
60     private final boolean rateBased;
61    
62 dl 1.4 /**
63 dl 1.5 * Creates a one-shot action with given nanoTime-based trigger time
64 dl 1.4 */
65     DelayedTask(Runnable r, long ns) {
66     super(r);
67     this.time = ns;
68     this.period = 0;
69 dl 1.7 rateBased = false;
70 dl 1.4 this.sequenceNumber = sequencer.getAndIncrement();
71     }
72    
73     /**
74 dl 1.5 * Creates a periodic action with given nano time and period
75 dl 1.4 */
76 dl 1.7 DelayedTask(Runnable r, long ns, long period, boolean rateBased) {
77 dl 1.4 super(r);
78     if (period <= 0)
79     throw new IllegalArgumentException();
80     this.time = ns;
81     this.period = period;
82 dl 1.7 this.rateBased = rateBased;
83 dl 1.4 this.sequenceNumber = sequencer.getAndIncrement();
84     }
85    
86    
87     public long getDelay(TimeUnit unit) {
88 dl 1.10 return unit.convert(time - System.nanoTime(),
89 dl 1.7 TimeUnit.NANOSECONDS);
90 dl 1.4 }
91    
92     public int compareTo(Object other) {
93     DelayedTask x = (DelayedTask)other;
94     long diff = time - x.time;
95     if (diff < 0)
96     return -1;
97     else if (diff > 0)
98     return 1;
99     else if (sequenceNumber < x.sequenceNumber)
100     return -1;
101     else
102     return 1;
103     }
104    
105     /**
106     * Return true if this is a periodic (not a one-shot) action.
107 dl 1.9 * @return true if periodic
108 dl 1.4 */
109     public boolean isPeriodic() {
110     return period > 0;
111     }
112    
113     /**
114 tim 1.11 * Returns the period, or zero if non-periodic.
115     *
116 dl 1.9 * @return the period
117 dl 1.4 */
118     public long getPeriod(TimeUnit unit) {
119     return unit.convert(period, TimeUnit.NANOSECONDS);
120     }
121    
122     /**
123     * Return a new DelayedTask that will trigger in the period
124     * subsequent to current task, or null if non-periodic
125     * or canceled.
126     */
127 dl 1.5 DelayedTask nextTask() {
128 dl 1.4 if (period <= 0 || isCancelled())
129     return null;
130 dl 1.10 long nextTime = period + (rateBased ? time : System.nanoTime());
131 dl 1.7 return new DelayedTask(getRunnable(), nextTime, period, rateBased);
132 dl 1.4 }
133 dl 1.2
134 dl 1.4 }
135 dl 1.2
136 dl 1.4 /**
137     * A delayed result-bearing action.
138     */
139 dl 1.8 public static class DelayedFutureTask<V> extends DelayedTask implements Future<V> {
140 dl 1.4 /**
141     * Creates a Future that may trigger after the given delay.
142     */
143     DelayedFutureTask(Callable<V> callable, long delay, TimeUnit unit) {
144     // must set after super ctor call to use inner class
145 dl 1.10 super(null, System.nanoTime() + unit.toNanos(delay));
146 dl 1.4 setRunnable(new InnerCancellableFuture<V>(callable));
147 dl 1.2 }
148    
149 dl 1.4 /**
150     * Creates a one-shot action that may trigger after the given date.
151     */
152     DelayedFutureTask(Callable<V> callable, Date date) {
153     super(null,
154     TimeUnit.MILLISECONDS.toNanos(date.getTime() -
155     System.currentTimeMillis()));
156     setRunnable(new InnerCancellableFuture<V>(callable));
157     }
158    
159     public V get() throws InterruptedException, ExecutionException {
160     return ((InnerCancellableFuture<V>)getRunnable()).get();
161 dl 1.2 }
162    
163 dl 1.4 public V get(long timeout, TimeUnit unit)
164     throws InterruptedException, ExecutionException, TimeoutException {
165     return ((InnerCancellableFuture<V>)getRunnable()).get(timeout, unit);
166 dl 1.2 }
167 tim 1.1
168 dl 1.4 protected void set(V v) {
169     ((InnerCancellableFuture<V>)getRunnable()).set(v);
170 dl 1.2 }
171 tim 1.1
172 dl 1.4 protected void setException(Throwable t) {
173     ((InnerCancellableFuture<V>)getRunnable()).setException(t);
174     }
175     }
176    
177    
178     /**
179     * An annoying wrapper class to convince generics compiler to
180 dl 1.7 * use a DelayQueue<DelayedTask> as a BlockingQueue<Runnable>
181 dl 1.4 */
182     private static class DelayedWorkQueue extends AbstractQueue<Runnable> implements BlockingQueue<Runnable> {
183     private final DelayQueue<DelayedTask> dq = new DelayQueue<DelayedTask>();
184     public Runnable poll() { return dq.poll(); }
185     public Runnable peek() { return dq.peek(); }
186     public Runnable take() throws InterruptedException { return dq.take(); }
187     public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
188     return dq.poll(timeout, unit);
189 dl 1.2 }
190 dl 1.4 public boolean offer(Runnable x) { return dq.offer((DelayedTask)x); }
191     public void put(Runnable x) throws InterruptedException {
192     dq.put((DelayedTask)x);
193 dl 1.2 }
194 dl 1.4 public boolean offer(Runnable x, long timeout, TimeUnit unit) throws InterruptedException {
195     return dq.offer((DelayedTask)x, timeout, unit);
196 dl 1.2 }
197 dl 1.4 public int remainingCapacity() { return dq.remainingCapacity(); }
198     public boolean remove(Object x) { return dq.remove(x); }
199     public boolean contains(Object x) { return dq.contains(x); }
200     public int size() { return dq.size(); }
201 tim 1.11 public boolean isEmpty() { return dq.isEmpty(); }
202 dl 1.4 public Iterator<Runnable> iterator() {
203     return new Iterator<Runnable>() {
204     private Iterator<DelayedTask> it = dq.iterator();
205     public boolean hasNext() { return it.hasNext(); }
206     public Runnable next() { return it.next(); }
207     public void remove() { it.remove(); }
208     };
209 tim 1.1 }
210 dl 1.4 }
211 tim 1.1
212 dl 1.4 /**
213     * Creates a new ScheduledExecutor with the given initial parameters.
214     *
215     * @param corePoolSize the number of threads to keep in the pool,
216     * even if they are idle.
217     */
218     public ScheduledExecutor(int corePoolSize) {
219     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
220     new DelayedWorkQueue());
221     }
222 tim 1.1
223 dl 1.4 /**
224     * Creates a new ScheduledExecutor with the given initial parameters.
225     *
226     * @param corePoolSize the number of threads to keep in the pool,
227     * even if they are idle.
228     * @param threadFactory the factory to use when the executor
229     * creates a new thread.
230     */
231     public ScheduledExecutor(int corePoolSize,
232     ThreadFactory threadFactory) {
233     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
234     new DelayedWorkQueue(), threadFactory);
235 tim 1.1 }
236    
237 dl 1.4 /**
238     * Creates a new ScheduledExecutor with the given initial parameters.
239     *
240     * @param corePoolSize the number of threads to keep in the pool,
241     * even if they are idle.
242     * @param handler the handler to use when execution is blocked
243     * because the thread bounds and queue capacities are reached.
244     */
245     public ScheduledExecutor(int corePoolSize,
246     RejectedExecutionHandler handler) {
247     super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
248     new DelayedWorkQueue(), handler);
249     }
250 dl 1.2
251 tim 1.1 /**
252 dl 1.2 * Creates a new ScheduledExecutor with the given initial parameters.
253     *
254     * @param corePoolSize the number of threads to keep in the pool,
255     * even if they are idle.
256 dl 1.4 * @param threadFactory the factory to use when the executor
257     * creates a new thread.
258     * @param handler the handler to use when execution is blocked
259     * because the thread bounds and queue capacities are reached.
260 tim 1.1 */
261 dl 1.4 public ScheduledExecutor(int corePoolSize,
262     ThreadFactory threadFactory,
263     RejectedExecutionHandler handler) {
264 dl 1.2 super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
265 dl 1.4 new DelayedWorkQueue(), threadFactory, handler);
266     }
267    
268     /**
269 dl 1.7 * Creates and executes a one-shot action that becomes enabled after
270 dl 1.4 * the given delay.
271 dl 1.6 * @param command the task to execute.
272     * @param delay the time from now to delay execution.
273     * @param unit the time unit of the delay parameter.
274     * @return a handle that can be used to cancel the task.
275 dl 1.4 */
276    
277 dl 1.6 public DelayedTask schedule(Runnable command, long delay, TimeUnit unit) {
278 dl 1.10 DelayedTask t = new DelayedTask(command, System.nanoTime() + unit.toNanos(delay));
279 dl 1.4 super.execute(t);
280     return t;
281     }
282    
283     /**
284 dl 1.9 * Creates and executes a one-shot action that becomes enabled
285     * after the given date.
286 dl 1.6 * @param command the task to execute.
287     * @param date the time to commence excution.
288     * @return a handle that can be used to cancel the task.
289     * @throws RejectedExecutionException if task cannot be scheduled
290     * for execution because the executor has been shut down.
291 dl 1.4 */
292 dl 1.6 public DelayedTask schedule(Runnable command, Date date) {
293 dl 1.4 DelayedTask t = new DelayedTask
294 dl 1.6 (command,
295 dl 1.4 TimeUnit.MILLISECONDS.toNanos(date.getTime() -
296     System.currentTimeMillis()));
297     super.execute(t);
298     return t;
299     }
300    
301     /**
302 dl 1.7 * Creates and executes a periodic action that becomes enabled first
303 dl 1.4 * after the given initial delay, and subsequently with the given
304 dl 1.7 * period; that is executions will commence after
305     * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
306     * <tt>initialDelay + 2 * period</tt>, and so on.
307 dl 1.6 * @param command the task to execute.
308     * @param initialDelay the time to delay first execution.
309     * @param period the period between successive executions.
310     * @param unit the time unit of the delay and period parameters
311     * @return a handle that can be used to cancel the task.
312     * @throws RejectedExecutionException if task cannot be scheduled
313     * for execution because the executor has been shut down.
314 dl 1.4 */
315 dl 1.7 public DelayedTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
316 dl 1.4 DelayedTask t = new DelayedTask
317 dl 1.10 (command, System.nanoTime() + unit.toNanos(initialDelay),
318 dl 1.7 unit.toNanos(period), true);
319 dl 1.4 super.execute(t);
320     return t;
321     }
322    
323     /**
324 dl 1.7 * Creates a periodic action that becomes enabled first after the
325     * given date, and subsequently with the given period
326     * period; that is executions will commence after
327     * <tt>initialDate</tt> then <tt>initialDate+period</tt>, then
328     * <tt>initialDate + 2 * period</tt>, and so on.
329 dl 1.6 * @param command the task to execute.
330     * @param initialDate the time to delay first execution.
331 dl 1.7 * @param period the period between commencement of successive
332     * executions.
333 dl 1.6 * @param unit the time unit of the period parameter.
334     * @return a handle that can be used to cancel the task.
335     * @throws RejectedExecutionException if task cannot be scheduled
336     * for execution because the executor has been shut down.
337 dl 1.4 */
338 dl 1.7 public DelayedTask scheduleAtFixedRate(Runnable command, Date initialDate, long period, TimeUnit unit) {
339     DelayedTask t = new DelayedTask
340     (command,
341     TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() -
342     System.currentTimeMillis()),
343     unit.toNanos(period), true);
344     super.execute(t);
345     return t;
346     }
347    
348     /**
349     * Creates and executes a periodic action that becomes enabled first
350     * after the given initial delay, and and subsequently with the
351     * given delay between the termination of one execution and the
352     * commencement of the next.
353     * @param command the task to execute.
354     * @param initialDelay the time to delay first execution.
355     * @param delay the delay between the termination of one
356     * execution and the commencement of the next.
357     * @param unit the time unit of the delay and delay parameters
358     * @return a handle that can be used to cancel the task.
359     * @throws RejectedExecutionException if task cannot be scheduled
360     * for execution because the executor has been shut down.
361     */
362     public DelayedTask scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
363     DelayedTask t = new DelayedTask
364 dl 1.10 (command, System.nanoTime() + unit.toNanos(initialDelay),
365 dl 1.7 unit.toNanos(delay), false);
366     super.execute(t);
367     return t;
368     }
369    
370     /**
371     * Creates a periodic action that becomes enabled first after the
372     * given date, and subsequently with the given delay between
373     * the termination of one execution and the commencement of the
374     * next.
375     * @param command the task to execute.
376     * @param initialDate the time to delay first execution.
377     * @param delay the delay between the termination of one
378     * execution and the commencement of the next.
379     * @param unit the time unit of the delay parameter.
380     * @return a handle that can be used to cancel the task.
381     * @throws RejectedExecutionException if task cannot be scheduled
382     * for execution because the executor has been shut down.
383     */
384     public DelayedTask scheduleWithFixedDelay(Runnable command, Date initialDate, long delay, TimeUnit unit) {
385 dl 1.4 DelayedTask t = new DelayedTask
386 dl 1.6 (command,
387     TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() -
388 dl 1.4 System.currentTimeMillis()),
389 dl 1.7 unit.toNanos(delay), false);
390 dl 1.4 super.execute(t);
391     return t;
392 tim 1.1 }
393    
394    
395 dl 1.4 /**
396 dl 1.7 * Creates and executes a Future that becomes enabled after the
397     * given delay.
398 dl 1.6 * @param callable the function to execute.
399     * @param delay the time from now to delay execution.
400     * @param unit the time unit of the delay parameter.
401     * @return a Future that can be used to extract result or cancel.
402     * @throws RejectedExecutionException if task cannot be scheduled
403     * for execution because the executor has been shut down.
404 dl 1.4 */
405     public <V> DelayedFutureTask<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
406     DelayedFutureTask<V> t = new DelayedFutureTask<V>
407     (callable, delay, unit);
408     super.execute(t);
409     return t;
410     }
411    
412     /**
413 dl 1.7 * Creates and executes a one-shot action that becomes enabled after
414 dl 1.4 * the given date.
415 dl 1.6 * @param callable the function to execute.
416     * @param date the time to commence excution.
417     * @return a Future that can be used to extract result or cancel.
418     * @throws RejectedExecutionException if task cannot be scheduled
419     * for execution because the executor has been shut down.
420 dl 1.4 */
421     public <V> DelayedFutureTask<V> schedule(Callable<V> callable, Date date) {
422     DelayedFutureTask<V> t = new DelayedFutureTask<V>
423     (callable, date);
424     super.execute(t);
425     return t;
426 tim 1.1 }
427    
428 dl 1.4 /**
429 tim 1.11 * Execute command with zero required delay.
430     *
431 dl 1.6 * @param command the task to execute
432     * @throws RejectedExecutionException at discretion of
433     * <tt>RejectedExecutionHandler</tt>, if task cannot be accepted
434     * for execution because the executor has been shut down.
435 dl 1.4 */
436     public void execute(Runnable command) {
437     schedule(command, 0, TimeUnit.NANOSECONDS);
438 tim 1.1 }
439    
440 dl 1.4 /**
441     * If executed task was periodic, cause the task for the next
442     * period to execute.
443 dl 1.9 * @param r the task (assumed to be a DelayedTask)
444     * @param t the exception
445 dl 1.4 */
446     protected void afterExecute(Runnable r, Throwable t) {
447     if (isShutdown())
448     return;
449     super.afterExecute(r, t);
450     DelayedTask d = (DelayedTask)r;
451     DelayedTask next = d.nextTask();
452 dl 1.6 if (next == null)
453     return;
454     try {
455 dl 1.4 super.execute(next);
456 dl 1.6 }
457     catch(RejectedExecutionException ex) {
458     // lost race to detect shutdown; ignore
459     }
460 dl 1.4 }
461 tim 1.1 }
462 dl 1.4
463