Name

ScheduledExecutorService

Synopsis

This interface extends Executor and ExecutorService to add methods for scheduling Callable or Runnable tasks for future execution on a one-time basis or a repeating basis. The schedule( ) methods schedule a Callable or a Runnable task for one-time execution after a specified delay. The delay is specified by a long plus a TimeUnit. When a Callable<V> is scheduled, the result is a ScheduledFuture<V>. This is like a Future<V> object but also implements the Delay interface so you can call getDelay( ) to find out how much time remains before execution begins. If you schedule( ) a Runnable object, the result is a ScheduledFuture<?>. Since a Runnable has no return value, the get( ) method of this ScheduledFuture returns null, but the cancel( ), getDelay( ), and isDone( ) methods remain useful.

ScheduledExecutorService provides two alternatives for scheduling Runnable tasks for repeated execution. (See also java.util.Timer, which has similar methods.) scheduleAtFixedRate( ) begins the first execution of the Runnable after initialDelay time units, and begins subsequent executions at multiples of period time units after that. This means that the Runnable runs at a fixed rate, regardless of how long each execution takes. scheduleWithFixedDelay( ) also begins the first execution after initialDelay time units. But it waits for this first execution (and all subsequent executions) to complete before scheduling the next execution for delay time units in the ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.