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

# Content
1 /*
2 * 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 */
6
7 package java.util.concurrent;
8 import java.util.concurrent.atomic.*;
9 import java.util.*;
10
11 /**
12 * 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 * <tt>java.util.Timer</tt> when multiple worker threads are needed,
15 * or when the additional flexibility or capabilities of
16 * <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 *
35 * @since 1.5
36 * @see Executors
37 *
38 * @spec JSR-166
39 * @author Doug Lea
40 */
41 public class ScheduledExecutor extends ThreadPoolExecutor {
42
43 /**
44 * 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 * A delayed or periodic action.
51 */
52 public static class DelayedTask extends CancellableTask implements Delayed {
53 /** Sequence number to break ties FIFO */
54 private final long sequenceNumber;
55 /** The time the task is enabled to execute in nanoTime units */
56 private final long time;
57 /** The delay forllowing next time, or <= 0 if non-periodic */
58 private final long period;
59 /** true if at fixed rate; false if fixed delay */
60 private final boolean rateBased;
61
62 /**
63 * Creates a one-shot action with given nanoTime-based trigger time
64 */
65 DelayedTask(Runnable r, long ns) {
66 super(r);
67 this.time = ns;
68 this.period = 0;
69 rateBased = false;
70 this.sequenceNumber = sequencer.getAndIncrement();
71 }
72
73 /**
74 * Creates a periodic action with given nano time and period
75 */
76 DelayedTask(Runnable r, long ns, long period, boolean rateBased) {
77 super(r);
78 if (period <= 0)
79 throw new IllegalArgumentException();
80 this.time = ns;
81 this.period = period;
82 this.rateBased = rateBased;
83 this.sequenceNumber = sequencer.getAndIncrement();
84 }
85
86
87 public long getDelay(TimeUnit unit) {
88 return unit.convert(time - System.nanoTime(),
89 TimeUnit.NANOSECONDS);
90 }
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 * @return true if periodic
108 */
109 public boolean isPeriodic() {
110 return period > 0;
111 }
112
113 /**
114 * Returns the period, or zero if non-periodic.
115 *
116 * @return the period
117 */
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 DelayedTask nextTask() {
128 if (period <= 0 || isCancelled())
129 return null;
130 long nextTime = period + (rateBased ? time : System.nanoTime());
131 return new DelayedTask(getRunnable(), nextTime, period, rateBased);
132 }
133
134 }
135
136 /**
137 * A delayed result-bearing action.
138 */
139 public static class DelayedFutureTask<V> extends DelayedTask implements Future<V> {
140 /**
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 super(null, System.nanoTime() + unit.toNanos(delay));
146 setRunnable(new InnerCancellableFuture<V>(callable));
147 }
148
149 /**
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 }
162
163 public V get(long timeout, TimeUnit unit)
164 throws InterruptedException, ExecutionException, TimeoutException {
165 return ((InnerCancellableFuture<V>)getRunnable()).get(timeout, unit);
166 }
167
168 protected void set(V v) {
169 ((InnerCancellableFuture<V>)getRunnable()).set(v);
170 }
171
172 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 * use a DelayQueue<DelayedTask> as a BlockingQueue<Runnable>
181 */
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 }
190 public boolean offer(Runnable x) { return dq.offer((DelayedTask)x); }
191 public void put(Runnable x) throws InterruptedException {
192 dq.put((DelayedTask)x);
193 }
194 public boolean offer(Runnable x, long timeout, TimeUnit unit) throws InterruptedException {
195 return dq.offer((DelayedTask)x, timeout, unit);
196 }
197 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 public boolean isEmpty() { return dq.isEmpty(); }
202 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 }
210 }
211
212 /**
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
223 /**
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 }
236
237 /**
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
251 /**
252 * 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 * @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 */
261 public ScheduledExecutor(int corePoolSize,
262 ThreadFactory threadFactory,
263 RejectedExecutionHandler handler) {
264 super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
265 new DelayedWorkQueue(), threadFactory, handler);
266 }
267
268 /**
269 * Creates and executes a one-shot action that becomes enabled after
270 * the given delay.
271 * @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 */
276
277 public DelayedTask schedule(Runnable command, long delay, TimeUnit unit) {
278 DelayedTask t = new DelayedTask(command, System.nanoTime() + unit.toNanos(delay));
279 super.execute(t);
280 return t;
281 }
282
283 /**
284 * Creates and executes a one-shot action that becomes enabled
285 * after the given date.
286 * @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 */
292 public DelayedTask schedule(Runnable command, Date date) {
293 DelayedTask t = new DelayedTask
294 (command,
295 TimeUnit.MILLISECONDS.toNanos(date.getTime() -
296 System.currentTimeMillis()));
297 super.execute(t);
298 return t;
299 }
300
301 /**
302 * Creates and executes a periodic action that becomes enabled first
303 * after the given initial delay, and subsequently with the given
304 * 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 * @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 */
315 public DelayedTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
316 DelayedTask t = new DelayedTask
317 (command, System.nanoTime() + unit.toNanos(initialDelay),
318 unit.toNanos(period), true);
319 super.execute(t);
320 return t;
321 }
322
323 /**
324 * 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 * @param command the task to execute.
330 * @param initialDate the time to delay first execution.
331 * @param period the period between commencement of successive
332 * executions.
333 * @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 */
338 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 (command, System.nanoTime() + unit.toNanos(initialDelay),
365 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 DelayedTask t = new DelayedTask
386 (command,
387 TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() -
388 System.currentTimeMillis()),
389 unit.toNanos(delay), false);
390 super.execute(t);
391 return t;
392 }
393
394
395 /**
396 * Creates and executes a Future that becomes enabled after the
397 * given delay.
398 * @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 */
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 * Creates and executes a one-shot action that becomes enabled after
414 * the given date.
415 * @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 */
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 }
427
428 /**
429 * Execute command with zero required delay.
430 *
431 * @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 */
436 public void execute(Runnable command) {
437 schedule(command, 0, TimeUnit.NANOSECONDS);
438 }
439
440 /**
441 * If executed task was periodic, cause the task for the next
442 * period to execute.
443 * @param r the task (assumed to be a DelayedTask)
444 * @param t the exception
445 */
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 if (next == null)
453 return;
454 try {
455 super.execute(next);
456 }
457 catch(RejectedExecutionException ex) {
458 // lost race to detect shutdown; ignore
459 }
460 }
461 }
462
463