9 |
|
import java.util.*; |
10 |
|
|
11 |
|
/** |
12 |
< |
* An <tt>Executor</tt> that can schedule command to run after a given |
12 |
> |
* An <tt>Executor</tt> that can schedule commands 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 |
150 |
|
/** |
151 |
|
* Creates a Future that may trigger after the given delay. |
152 |
|
*/ |
153 |
< |
DelayedFutureTask(Callable<V> callable, long delay, TimeUnit unit) { |
153 |
> |
DelayedFutureTask(Callable<V> callable, long triggerTime) { |
154 |
|
// must set after super ctor call to use inner class |
155 |
< |
super(null, System.nanoTime() + unit.toNanos(delay)); |
155 |
> |
super(null, triggerTime); |
156 |
|
setRunnable(new InnerCancellableFuture<V>(callable)); |
157 |
|
} |
158 |
|
|
159 |
– |
/** |
160 |
– |
* Creates a one-shot action that may trigger after the given date. |
161 |
– |
*/ |
162 |
– |
DelayedFutureTask(Callable<V> callable, Date date) { |
163 |
– |
super(null, |
164 |
– |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
165 |
– |
System.currentTimeMillis())); |
166 |
– |
setRunnable(new InnerCancellableFuture<V>(callable)); |
167 |
– |
} |
168 |
– |
|
159 |
|
public V get() throws InterruptedException, ExecutionException { |
160 |
|
return ((InnerCancellableFuture<V>)getRunnable()).get(); |
161 |
|
} |
296 |
|
* @param delay the time from now to delay execution. |
297 |
|
* @param unit the time unit of the delay parameter. |
298 |
|
* @return a handle that can be used to cancel the task. |
299 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
300 |
+ |
* for execution because the executor has been shut down. |
301 |
|
*/ |
302 |
|
|
303 |
|
public DelayedTask schedule(Runnable command, long delay, TimeUnit unit) { |
304 |
< |
DelayedTask t = new DelayedTask(command, System.nanoTime() + unit.toNanos(delay)); |
304 |
> |
long triggerTime = System.nanoTime() + unit.toNanos(delay); |
305 |
> |
DelayedTask t = new DelayedTask(command, triggerTime); |
306 |
|
delayedExecute(t); |
307 |
|
return t; |
308 |
|
} |
317 |
|
* for execution because the executor has been shut down. |
318 |
|
*/ |
319 |
|
public DelayedTask schedule(Runnable command, Date date) { |
320 |
< |
DelayedTask t = new DelayedTask |
321 |
< |
(command, |
322 |
< |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
323 |
< |
System.currentTimeMillis())); |
320 |
> |
long triggerTime = System.nanoTime() + |
321 |
> |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
322 |
> |
System.currentTimeMillis()); |
323 |
> |
DelayedTask t = new DelayedTask(command, triggerTime); |
324 |
|
delayedExecute(t); |
325 |
|
return t; |
326 |
|
} |
340 |
|
* for execution because the executor has been shut down. |
341 |
|
*/ |
342 |
|
public DelayedTask scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { |
343 |
< |
DelayedTask t = new DelayedTask |
344 |
< |
(command, System.nanoTime() + unit.toNanos(initialDelay), |
345 |
< |
unit.toNanos(period), true); |
343 |
> |
long triggerTime = System.nanoTime() + unit.toNanos(initialDelay); |
344 |
> |
DelayedTask t = new DelayedTask(command, |
345 |
> |
triggerTime, |
346 |
> |
unit.toNanos(period), |
347 |
> |
true); |
348 |
|
delayedExecute(t); |
349 |
|
return t; |
350 |
|
} |
365 |
|
* for execution because the executor has been shut down. |
366 |
|
*/ |
367 |
|
public DelayedTask scheduleAtFixedRate(Runnable command, Date initialDate, long period, TimeUnit unit) { |
368 |
< |
DelayedTask t = new DelayedTask |
369 |
< |
(command, |
370 |
< |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
371 |
< |
System.currentTimeMillis()), |
372 |
< |
unit.toNanos(period), true); |
368 |
> |
long triggerTime = System.nanoTime() + |
369 |
> |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
370 |
> |
System.currentTimeMillis()); |
371 |
> |
DelayedTask t = new DelayedTask(command, |
372 |
> |
triggerTime, |
373 |
> |
unit.toNanos(period), |
374 |
> |
true); |
375 |
|
delayedExecute(t); |
376 |
|
return t; |
377 |
|
} |
391 |
|
* for execution because the executor has been shut down. |
392 |
|
*/ |
393 |
|
public DelayedTask scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { |
394 |
< |
DelayedTask t = new DelayedTask |
395 |
< |
(command, System.nanoTime() + unit.toNanos(initialDelay), |
396 |
< |
unit.toNanos(delay), false); |
394 |
> |
long triggerTime = System.nanoTime() + unit.toNanos(initialDelay); |
395 |
> |
DelayedTask t = new DelayedTask(command, |
396 |
> |
triggerTime, |
397 |
> |
unit.toNanos(delay), |
398 |
> |
false); |
399 |
|
delayedExecute(t); |
400 |
|
return t; |
401 |
|
} |
415 |
|
* for execution because the executor has been shut down. |
416 |
|
*/ |
417 |
|
public DelayedTask scheduleWithFixedDelay(Runnable command, Date initialDate, long delay, TimeUnit unit) { |
418 |
< |
DelayedTask t = new DelayedTask |
419 |
< |
(command, |
420 |
< |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
421 |
< |
System.currentTimeMillis()), |
422 |
< |
unit.toNanos(delay), false); |
418 |
> |
long triggerTime = System.nanoTime() + |
419 |
> |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
420 |
> |
System.currentTimeMillis()); |
421 |
> |
DelayedTask t = new DelayedTask(command, |
422 |
> |
triggerTime, |
423 |
> |
unit.toNanos(delay), |
424 |
> |
false); |
425 |
|
delayedExecute(t); |
426 |
|
return t; |
427 |
|
} |
438 |
|
* for execution because the executor has been shut down. |
439 |
|
*/ |
440 |
|
public <V> DelayedFutureTask<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
441 |
< |
DelayedFutureTask<V> t = new DelayedFutureTask<V> |
442 |
< |
(callable, delay, unit); |
441 |
> |
long triggerTime = System.nanoTime() + unit.toNanos(delay); |
442 |
> |
DelayedFutureTask<V> t = new DelayedFutureTask<V>(callable, |
443 |
> |
triggerTime); |
444 |
|
delayedExecute(t); |
445 |
|
return t; |
446 |
|
} |
455 |
|
* for execution because the executor has been shut down. |
456 |
|
*/ |
457 |
|
public <V> DelayedFutureTask<V> schedule(Callable<V> callable, Date date) { |
458 |
< |
DelayedFutureTask<V> t = new DelayedFutureTask<V> |
459 |
< |
(callable, date); |
458 |
> |
long triggerTime = System.nanoTime() + |
459 |
> |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
460 |
> |
System.currentTimeMillis()); |
461 |
> |
DelayedFutureTask<V> t = new DelayedFutureTask<V>(callable, |
462 |
> |
triggerTime); |
463 |
|
delayedExecute(t); |
464 |
|
return t; |
465 |
|
} |