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 |
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. |
30 |
> |
* enabled in first-in-first-out (FIFO) order of submission. |
31 |
> |
* |
32 |
> |
* <p>All <t>schedule</tt> methods accept <em>relative</em> delays and |
33 |
> |
* periods as arguments, not absolute times or dates. It is a simple |
34 |
> |
* matter to transform an absolute time represented as a |
35 |
> |
* <tt>java.util.Date</tt>, to the required form. For example, to |
36 |
> |
* schedule at a certain future <tt>date</tt>, you can use: |
37 |
> |
* <tt>schedule(task, date.getTime() - System.currentTimeMillis, |
38 |
> |
* TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a |
39 |
> |
* relative delay need not coincide with the current <tt>Date</tt> at |
40 |
> |
* which the task is enabled due to network time synchronization |
41 |
> |
* protocols, clock drift, or other factors. |
42 |
|
* |
43 |
|
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
44 |
|
* of the inherited tuning methods are not especially useful for |
326 |
|
} |
327 |
|
|
328 |
|
/** |
321 |
– |
* Creates and executes a one-shot action that becomes enabled |
322 |
– |
* after the given date. |
323 |
– |
* @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 |
– |
*/ |
329 |
– |
public ScheduledCancellable schedule(Runnable command, Date date) { |
330 |
– |
long triggerTime = System.nanoTime() + |
331 |
– |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
332 |
– |
System.currentTimeMillis()); |
333 |
– |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, triggerTime); |
334 |
– |
delayedExecute(t); |
335 |
– |
return t; |
336 |
– |
} |
337 |
– |
|
338 |
– |
/** |
329 |
|
* Creates and executes a periodic action that becomes enabled first |
330 |
|
* after the given initial delay, and subsequently with the given |
331 |
|
* period; that is executions will commence after |
349 |
|
return t; |
350 |
|
} |
351 |
|
|
362 |
– |
/** |
363 |
– |
* 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 |
– |
* @param command the task to execute. |
369 |
– |
* @param initialDate the time to delay first execution. |
370 |
– |
* @param period the period between commencement of successive |
371 |
– |
* executions. |
372 |
– |
* @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 |
– |
*/ |
377 |
– |
public ScheduledCancellable scheduleAtFixedRate(Runnable command, Date initialDate, long period, TimeUnit unit) { |
378 |
– |
long triggerTime = System.nanoTime() + |
379 |
– |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
380 |
– |
System.currentTimeMillis()); |
381 |
– |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, |
382 |
– |
triggerTime, |
383 |
– |
unit.toNanos(period), |
384 |
– |
true); |
385 |
– |
delayedExecute(t); |
386 |
– |
return t; |
387 |
– |
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Creates and executes a periodic action that becomes enabled first |
375 |
|
} |
376 |
|
|
377 |
|
/** |
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 ScheduledCancellable scheduleWithFixedDelay(Runnable command, Date initialDate, long delay, TimeUnit unit) { |
428 |
– |
long triggerTime = System.nanoTime() + |
429 |
– |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
430 |
– |
System.currentTimeMillis()); |
431 |
– |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, |
432 |
– |
triggerTime, |
433 |
– |
unit.toNanos(delay), |
434 |
– |
false); |
435 |
– |
delayedExecute(t); |
436 |
– |
return t; |
437 |
– |
} |
438 |
– |
|
439 |
– |
|
440 |
– |
/** |
378 |
|
* Creates and executes a ScheduledFuture that becomes enabled after the |
379 |
|
* given delay. |
380 |
|
* @param callable the function to execute. |
392 |
|
} |
393 |
|
|
394 |
|
/** |
458 |
– |
* Creates and executes a one-shot action that becomes enabled after |
459 |
– |
* the given date. |
460 |
– |
* @param callable the function to execute. |
461 |
– |
* @param date the time to commence excution. |
462 |
– |
* @return a ScheduledFuture that can be used to extract result or cancel. |
463 |
– |
* @throws RejectedExecutionException if task cannot be scheduled |
464 |
– |
* for execution because the executor has been shut down. |
465 |
– |
*/ |
466 |
– |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, Date date) { |
467 |
– |
long triggerTime = System.nanoTime() + |
468 |
– |
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
469 |
– |
System.currentTimeMillis()); |
470 |
– |
ScheduledFutureTask<V> t = new ScheduledFutureTask<V>(callable, triggerTime); |
471 |
– |
delayedExecute(t); |
472 |
– |
return t; |
473 |
– |
} |
474 |
– |
|
475 |
– |
/** |
395 |
|
* Execute command with zero required delay. This has effect |
396 |
|
* equivalent to <tt>schedule(command, 0, anyUnit)</tt>. Note |
397 |
|
* that inspections of the queue and of the list returned by |
528 |
|
* @return true if the task was removed |
529 |
|
*/ |
530 |
|
public boolean remove(Runnable task) { |
531 |
< |
if (task instanceof ScheduledCancellable && super.getQueue().remove(task)) |
532 |
< |
return true; |
531 |
> |
if (task instanceof ScheduledCancellable) |
532 |
> |
return super.remove(task); |
533 |
|
|
534 |
|
// The task might actually have been wrapped as a ScheduledCancellable |
535 |
|
// in execute(), in which case we need to maually traverse |
551 |
|
|
552 |
|
|
553 |
|
/** |
554 |
< |
* Returns the task queue used by this executor. Each element |
555 |
< |
* of this queue is a <tt>ScheduledCancellable</tt>, including those |
556 |
< |
* tasks submitted using <tt>execute</tt> which are for |
557 |
< |
* scheduling purposes used as the basis of a zero-delay |
558 |
< |
* <tt>ScheduledCancellable</tt>. |
554 |
> |
* Returns the task queue used by this executor. Each element of |
555 |
> |
* this queue is a <tt>ScheduledCancellable</tt>, including those |
556 |
> |
* tasks submitted using <tt>execute</tt> which are for scheduling |
557 |
> |
* purposes used as the basis of a zero-delay |
558 |
> |
* <tt>ScheduledCancellable</tt>. Iteration over this queue is |
559 |
> |
* </em>not</em> guaranteed to travserse tasks in the order in |
560 |
> |
* which they will execute. |
561 |
|
* |
562 |
|
* @return the task queue |
563 |
|
*/ |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
< |
* If executed task was periodic, cause the task for the next |
570 |
< |
* period to execute. |
569 |
> |
* Override of <tt>Executor</tt> hook method to support periodic |
570 |
> |
* tasks. If the executed task was periodic, causes the task for |
571 |
> |
* the next period to execute. |
572 |
|
* @param r the task (assumed to be a ScheduledCancellable) |
573 |
|
* @param t the exception |
574 |
|
*/ |