9 |
|
import java.util.*; |
10 |
|
|
11 |
|
/** |
12 |
< |
* A {@link ThreadPoolExecutor} that can schedule commands to run |
13 |
< |
* after a given delay, or to execute periodically. This class is |
14 |
< |
* preferable to {@link java.util.Timer} when multiple worker threads |
15 |
< |
* are needed, or when the additional flexibility or capabilities of |
16 |
< |
* {@link ThreadPoolExecutor} (which this class extends) are required. |
12 |
> |
* A {@link ThreadPoolExecutor} that can additionally schedule |
13 |
> |
* commands to run after a given delay, or to execute |
14 |
> |
* periodically. This class is preferable to {@link java.util.Timer} |
15 |
> |
* when multiple worker threads are needed, or when the additional |
16 |
> |
* flexibility or capabilities of {@link ThreadPoolExecutor} (which |
17 |
> |
* this class extends) are required. |
18 |
|
* |
19 |
|
* <p> Delayed tasks execute no sooner than they are enabled, but |
20 |
|
* without any real-time guarantees about when, after they are enabled, |
22 |
|
* enabled in first-in-first-out (FIFO) order of submission. |
23 |
|
* |
24 |
|
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
25 |
< |
* of the inherited tuning methods are not especially useful for |
25 |
> |
* of the inherited tuning methods are not useful for |
26 |
|
* it. In particular, because a <tt>ScheduledExecutor</tt> always acts |
27 |
|
* as a fixed-sized pool using <tt>corePoolSize</tt> threads and an |
28 |
|
* unbounded queue, adjustments to <tt>maximumPoolSize</tt> have no |
288 |
|
super.getQueue().add(command); |
289 |
|
} |
290 |
|
|
290 |
– |
/** |
291 |
– |
* Creates and executes a one-shot action that becomes enabled after |
292 |
– |
* the given delay. |
293 |
– |
* @param command the task to execute. |
294 |
– |
* @param delay the time from now to delay execution. |
295 |
– |
* @param unit the time unit of the delay parameter. |
296 |
– |
* @return a Future representing pending completion of the task, |
297 |
– |
* and whose <tt>get()</tt> method will return <tt>null</tt> |
298 |
– |
* upon completion. |
299 |
– |
* @throws RejectedExecutionException if task cannot be scheduled |
300 |
– |
* for execution because the executor has been shut down. |
301 |
– |
* @throws NullPointerException if command is null |
302 |
– |
*/ |
303 |
– |
|
291 |
|
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
292 |
|
if (command == null) |
293 |
|
throw new NullPointerException(); |
297 |
|
return t; |
298 |
|
} |
299 |
|
|
313 |
– |
/** |
314 |
– |
* Creates and executes a ScheduledFuture that becomes enabled after the |
315 |
– |
* given delay. |
316 |
– |
* @param callable the function to execute. |
317 |
– |
* @param delay the time from now to delay execution. |
318 |
– |
* @param unit the time unit of the delay parameter. |
319 |
– |
* @return a ScheduledFuture that can be used to extract result or cancel. |
320 |
– |
* @throws RejectedExecutionException if task cannot be scheduled |
321 |
– |
* for execution because the executor has been shut down. |
322 |
– |
* @throws NullPointerException if callable is null |
323 |
– |
*/ |
300 |
|
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
301 |
|
if (callable == null) |
302 |
|
throw new NullPointerException(); |
306 |
|
return t; |
307 |
|
} |
308 |
|
|
333 |
– |
/** |
334 |
– |
* Creates and executes a periodic action that becomes enabled first |
335 |
– |
* after the given initial delay, and subsequently with the given |
336 |
– |
* period; that is executions will commence after |
337 |
– |
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
338 |
– |
* <tt>initialDelay + 2 * period</tt>, and so on. The |
339 |
– |
* task will only terminate via cancellation. |
340 |
– |
* @param command the task to execute. |
341 |
– |
* @param initialDelay the time to delay first execution. |
342 |
– |
* @param period the period between successive executions. |
343 |
– |
* @param unit the time unit of the delay and period parameters |
344 |
– |
* @return a Future representing pending completion of the task, |
345 |
– |
* and whose <tt>get()</tt> method will throw an exception upon |
346 |
– |
* cancellation. |
347 |
– |
* @throws RejectedExecutionException if task cannot be scheduled |
348 |
– |
* for execution because the executor has been shut down. |
349 |
– |
* @throws NullPointerException if command is null |
350 |
– |
* @throws IllegalArgumentException if period less than or equal to zero. |
351 |
– |
*/ |
309 |
|
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { |
310 |
|
if (command == null) |
311 |
|
throw new NullPointerException(); |
321 |
|
return t; |
322 |
|
} |
323 |
|
|
367 |
– |
/** |
368 |
– |
* Creates and executes a periodic action that becomes enabled first |
369 |
– |
* after the given initial delay, and subsequently with the |
370 |
– |
* given delay between the termination of one execution and the |
371 |
– |
* commencement of the next. |
372 |
– |
* The task will only terminate via cancellation. |
373 |
– |
* @param command the task to execute. |
374 |
– |
* @param initialDelay the time to delay first execution. |
375 |
– |
* @param delay the delay between the termination of one |
376 |
– |
* execution and the commencement of the next. |
377 |
– |
* @param unit the time unit of the delay and delay parameters |
378 |
– |
* @return a Future representing pending completion of the task, |
379 |
– |
* and whose <tt>get()</tt> method will throw an exception upon |
380 |
– |
* cancellation. |
381 |
– |
* @throws RejectedExecutionException if task cannot be scheduled |
382 |
– |
* for execution because the executor has been shut down. |
383 |
– |
* @throws NullPointerException if command is null |
384 |
– |
* @throws IllegalArgumentException if delay less than or equal to zero. |
385 |
– |
*/ |
324 |
|
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { |
325 |
|
if (command == null) |
326 |
|
throw new NullPointerException(); |
357 |
|
schedule(command, 0, TimeUnit.NANOSECONDS); |
358 |
|
} |
359 |
|
|
360 |
+ |
public Future<?> submit(Runnable task) { |
361 |
+ |
return schedule(task, 0, TimeUnit.NANOSECONDS); |
362 |
+ |
} |
363 |
+ |
|
364 |
+ |
public <T> Future<T> submit(Runnable task, T result) { |
365 |
+ |
return schedule(Executors.callable(task, result), 0, TimeUnit.NANOSECONDS); |
366 |
+ |
} |
367 |
+ |
|
368 |
+ |
public <T> Future<T> submit(Callable<T> task) { |
369 |
+ |
return schedule(task, 0, TimeUnit.NANOSECONDS); |
370 |
+ |
} |
371 |
|
|
372 |
|
/** |
373 |
|
* Set policy on whether to continue executing existing periodic |
437 |
|
else if (keepDelayed || keepPeriodic) { |
438 |
|
Object[] entries = super.getQueue().toArray(); |
439 |
|
for (int i = 0; i < entries.length; ++i) { |
440 |
< |
ScheduledFutureTask<?> t = (ScheduledFutureTask<?>)entries[i]; |
441 |
< |
if (t.isPeriodic()? !keepPeriodic : !keepDelayed) |
442 |
< |
t.cancel(false); |
440 |
> |
Object e = entries[i]; |
441 |
> |
if (e instanceof ScheduledFutureTask) { |
442 |
> |
ScheduledFutureTask<?> t = (ScheduledFutureTask<?>)e; |
443 |
> |
if (t.isPeriodic()? !keepPeriodic : !keepDelayed) |
444 |
> |
t.cancel(false); |
445 |
> |
} |
446 |
|
} |
447 |
|
entries = null; |
448 |
|
purge(); |
503 |
|
ScheduledFuture wrap = null; |
504 |
|
Object[] entries = super.getQueue().toArray(); |
505 |
|
for (int i = 0; i < entries.length; ++i) { |
506 |
< |
ScheduledFutureTask<?> t = (ScheduledFutureTask<?>)entries[i]; |
507 |
< |
Object r = t.getTask(); |
508 |
< |
if (task.equals(r)) { |
509 |
< |
wrap = t; |
510 |
< |
break; |
506 |
> |
Object e = entries[i]; |
507 |
> |
if (e instanceof ScheduledFutureTask) { |
508 |
> |
ScheduledFutureTask<?> t = (ScheduledFutureTask<?>)e; |
509 |
> |
Object r = t.getTask(); |
510 |
> |
if (task.equals(r)) { |
511 |
> |
wrap = t; |
512 |
> |
break; |
513 |
> |
} |
514 |
|
} |
515 |
|
} |
516 |
|
entries = null; |