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 future. Both methods return a ScheduledFuture object that you can use to cancel( ) the repeated execution of tasks. If the task is not canceled , the ScheduledExecutorService runs it repeatedly until the service is shut down (see ExecutorService ) or the Runnable throws an exception. Figure 16-89. java.util.concurrent.ScheduledExecutorServicepublic interface ScheduledExecutorService extends ExecutorService { // Public Instance Methods <V> ScheduledFuture<V> schedule (Callable<V> callable , long delay , TimeUnit unit ); ScheduledFuture<?> schedule (Runnable command , long delay , TimeUnit unit ); ScheduledFuture<?> scheduleAtFixedRate (Runnable command , long initialDelay , long period , TimeUnit unit ); ScheduledFuture<?> scheduleWithFixedDelay (Runnable command , long initialDelay , long delay , TimeUnit unit ); } ImplementationsScheduledThreadPoolExecutor Passed ToExecutors.unconfigurableScheduledExecutorService( ) Returned ByExecutors.{newScheduledThreadPool( ) , newSingleThreadScheduledExecutor( ) , unconfigurableScheduledExecutorService( )} |