246 |
|
/** |
247 |
|
* Creates and executes a one-shot action that may trigger after |
248 |
|
* the given delay. |
249 |
+ |
* @param command the task to execute. |
250 |
+ |
* @param delay the time from now to delay execution. |
251 |
+ |
* @param unit the time unit of the delay parameter. |
252 |
+ |
* @return a handle that can be used to cancel the task. |
253 |
|
*/ |
254 |
|
|
255 |
< |
public DelayedTask schedule(Runnable r, long delay, TimeUnit unit) { |
256 |
< |
DelayedTask t = new DelayedTask(r, TimeUnit.nanoTime() + unit.toNanos(delay)); |
255 |
> |
public DelayedTask schedule(Runnable command, long delay, TimeUnit unit) { |
256 |
> |
DelayedTask t = new DelayedTask(command, TimeUnit.nanoTime() + unit.toNanos(delay)); |
257 |
|
super.execute(t); |
258 |
|
return t; |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
|
* Creates and executes a one-shot action that may trigger after the given date. |
263 |
+ |
* @param command the task to execute. |
264 |
+ |
* @param date the time to commence excution. |
265 |
+ |
* @return a handle that can be used to cancel the task. |
266 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
267 |
+ |
* for execution because the executor has been shut down. |
268 |
|
*/ |
269 |
< |
public DelayedTask schedule(Runnable r, Date date) { |
269 |
> |
public DelayedTask schedule(Runnable command, Date date) { |
270 |
|
DelayedTask t = new DelayedTask |
271 |
< |
(r, |
271 |
> |
(command, |
272 |
|
TimeUnit.MILLISECONDS.toNanos(date.getTime() - |
273 |
|
System.currentTimeMillis())); |
274 |
|
super.execute(t); |
279 |
|
* Creates and executes a periodic action that may trigger first |
280 |
|
* after the given initial delay, and subsequently with the given |
281 |
|
* period. |
282 |
+ |
* @param command the task to execute. |
283 |
+ |
* @param initialDelay the time to delay first execution. |
284 |
+ |
* @param period the period between successive executions. |
285 |
+ |
* @param unit the time unit of the delay and period parameters |
286 |
+ |
* @return a handle that can be used to cancel the task. |
287 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
288 |
+ |
* for execution because the executor has been shut down. |
289 |
|
*/ |
290 |
< |
public DelayedTask schedule (Runnable r, long initialDelay, long period, TimeUnit unit) { |
290 |
> |
public DelayedTask schedule (Runnable command, long initialDelay, long period, TimeUnit unit) { |
291 |
|
DelayedTask t = new DelayedTask |
292 |
< |
(r, TimeUnit.nanoTime() + unit.toNanos(initialDelay), |
292 |
> |
(command, TimeUnit.nanoTime() + unit.toNanos(initialDelay), |
293 |
|
unit.toNanos(period)); |
294 |
|
super.execute(t); |
295 |
|
return t; |
298 |
|
/** |
299 |
|
* Creates a periodic action that may trigger first after the |
300 |
|
* given date, and subsequently with the given period. |
301 |
+ |
* @param command the task to execute. |
302 |
+ |
* @param initialDate the time to delay first execution. |
303 |
+ |
* @param period the period between successive executions. |
304 |
+ |
* @param unit the time unit of the period parameter. |
305 |
+ |
* @return a handle that can be used to cancel the task. |
306 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
307 |
+ |
* for execution because the executor has been shut down. |
308 |
|
*/ |
309 |
< |
public DelayedTask schedule(Runnable r, Date firstDate, long period, TimeUnit unit) { |
309 |
> |
public DelayedTask schedule(Runnable command, Date initialDate, long period, TimeUnit unit) { |
310 |
|
DelayedTask t = new DelayedTask |
311 |
< |
(r, |
312 |
< |
TimeUnit.MILLISECONDS.toNanos(firstDate.getTime() - |
311 |
> |
(command, |
312 |
> |
TimeUnit.MILLISECONDS.toNanos(initialDate.getTime() - |
313 |
|
System.currentTimeMillis()), |
314 |
|
unit.toNanos(period)); |
315 |
|
super.execute(t); |
319 |
|
|
320 |
|
/** |
321 |
|
* Creates and executes a Future that may trigger after the given delay. |
322 |
+ |
* @param callable the function to execute. |
323 |
+ |
* @param delay the time from now to delay execution. |
324 |
+ |
* @param unit the time unit of the delay parameter. |
325 |
+ |
* @return a Future that can be used to extract result or cancel. |
326 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
327 |
+ |
* for execution because the executor has been shut down. |
328 |
|
*/ |
329 |
|
public <V> DelayedFutureTask<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
330 |
|
DelayedFutureTask<V> t = new DelayedFutureTask<V> |
336 |
|
/** |
337 |
|
* Creates and executes a one-shot action that may trigger after |
338 |
|
* the given date. |
339 |
+ |
* @param callable the function to execute. |
340 |
+ |
* @param date the time to commence excution. |
341 |
+ |
* @return a Future that can be used to extract result or cancel. |
342 |
+ |
* @throws RejectedExecutionException if task cannot be scheduled |
343 |
+ |
* for execution because the executor has been shut down. |
344 |
|
*/ |
345 |
|
public <V> DelayedFutureTask<V> schedule(Callable<V> callable, Date date) { |
346 |
|
DelayedFutureTask<V> t = new DelayedFutureTask<V> |
351 |
|
|
352 |
|
/** |
353 |
|
* Execute command with zero required delay |
354 |
+ |
* @param command the task to execute |
355 |
+ |
* @throws RejectedExecutionException at discretion of |
356 |
+ |
* <tt>RejectedExecutionHandler</tt>, if task cannot be accepted |
357 |
+ |
* for execution because the executor has been shut down. |
358 |
|
*/ |
359 |
|
public void execute(Runnable command) { |
360 |
|
schedule(command, 0, TimeUnit.NANOSECONDS); |
370 |
|
super.afterExecute(r, t); |
371 |
|
DelayedTask d = (DelayedTask)r; |
372 |
|
DelayedTask next = d.nextTask(); |
373 |
< |
if (next != null) |
373 |
> |
if (next == null) |
374 |
> |
return; |
375 |
> |
try { |
376 |
|
super.execute(next); |
377 |
+ |
} |
378 |
+ |
catch(RejectedExecutionException ex) { |
379 |
+ |
// lost race to detect shutdown; ignore |
380 |
+ |
} |
381 |
|
} |
382 |
|
} |
383 |
|
|