ScheduledExecutorService


ScheduledExecutorService java.util.concurrent

Java 5.0

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.ScheduledExecutorService

 public 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  );   } 

Implementations

ScheduledThreadPoolExecutor

Passed To

Executors.unconfigurableScheduledExecutorService( )

Returned By

Executors.{newScheduledThreadPool( ) , newSingleThreadScheduledExecutor( ) , unconfigurableScheduledExecutorService( )}



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net