src/share/classes/java/util/concurrent/ScheduledExecutorService.java

Print this page




  55  *
  56  * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
  57  * periods as arguments, not absolute times or dates. It is a simple
  58  * matter to transform an absolute time represented as a {@link
  59  * java.util.Date} to the required form. For example, to schedule at
  60  * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
  61  * date.getTime() - System.currentTimeMillis(),
  62  * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
  63  * relative delay need not coincide with the current <tt>Date</tt> at
  64  * which the task is enabled due to network time synchronization
  65  * protocols, clock drift, or other factors.
  66  *
  67  * The {@link Executors} class provides convenient factory methods for
  68  * the ScheduledExecutorService implementations provided in this package.
  69  *
  70  * <h3>Usage Example</h3>
  71  *
  72  * Here is a class with a method that sets up a ScheduledExecutorService
  73  * to beep every ten seconds for an hour:
  74  *
  75  * <pre>
  76  * import static java.util.concurrent.TimeUnit.*;
  77  * class BeeperControl {
  78  *    private final ScheduledExecutorService scheduler =
  79  *       Executors.newScheduledThreadPool(1);
  80  *
  81  *    public void beepForAnHour() {
  82  *        final Runnable beeper = new Runnable() {
  83  *                public void run() { System.out.println("beep"); }
  84  *            };
  85  *        final ScheduledFuture&lt;?&gt; beeperHandle =
  86  *            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
  87  *        scheduler.schedule(new Runnable() {
  88  *                public void run() { beeperHandle.cancel(true); }
  89  *            }, 60 * 60, SECONDS);
  90  *    }
  91  * }
  92  * </pre>
  93  *
  94  * @since 1.5
  95  * @author Doug Lea
  96  */
  97 public interface ScheduledExecutorService extends ExecutorService {
  98 
  99     /**
 100      * Creates and executes a one-shot action that becomes enabled
 101      * after the given delay.
 102      *
 103      * @param command the task to execute
 104      * @param delay the time from now to delay execution
 105      * @param unit the time unit of the delay parameter
 106      * @return a ScheduledFuture representing pending completion of
 107      *         the task and whose <tt>get()</tt> method will return
 108      *         <tt>null</tt> upon completion
 109      * @throws RejectedExecutionException if the task cannot be
 110      *         scheduled for execution
 111      * @throws NullPointerException if command is null
 112      */




  55  *
  56  * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
  57  * periods as arguments, not absolute times or dates. It is a simple
  58  * matter to transform an absolute time represented as a {@link
  59  * java.util.Date} to the required form. For example, to schedule at
  60  * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
  61  * date.getTime() - System.currentTimeMillis(),
  62  * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
  63  * relative delay need not coincide with the current <tt>Date</tt> at
  64  * which the task is enabled due to network time synchronization
  65  * protocols, clock drift, or other factors.
  66  *
  67  * The {@link Executors} class provides convenient factory methods for
  68  * the ScheduledExecutorService implementations provided in this package.
  69  *
  70  * <h3>Usage Example</h3>
  71  *
  72  * Here is a class with a method that sets up a ScheduledExecutorService
  73  * to beep every ten seconds for an hour:
  74  *
  75  *  <pre> {@code
  76  * import static java.util.concurrent.TimeUnit.*;
  77  * class BeeperControl {
  78  *   private final ScheduledExecutorService scheduler =
  79  *     Executors.newScheduledThreadPool(1);
  80  *
  81  *   public void beepForAnHour() {
  82  *     final Runnable beeper = new Runnable() {
  83  *       public void run() { System.out.println("beep"); }
  84  *     };
  85  *     final ScheduledFuture<?> beeperHandle =
  86  *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
  87  *     scheduler.schedule(new Runnable() {
  88  *       public void run() { beeperHandle.cancel(true); }
  89  *     }, 60 * 60, SECONDS);
  90  *   }
  91  * }}</pre>

  92  *
  93  * @since 1.5
  94  * @author Doug Lea
  95  */
  96 public interface ScheduledExecutorService extends ExecutorService {
  97 
  98     /**
  99      * Creates and executes a one-shot action that becomes enabled
 100      * after the given delay.
 101      *
 102      * @param command the task to execute
 103      * @param delay the time from now to delay execution
 104      * @param unit the time unit of the delay parameter
 105      * @return a ScheduledFuture representing pending completion of
 106      *         the task and whose <tt>get()</tt> method will return
 107      *         <tt>null</tt> upon completion
 108      * @throws RejectedExecutionException if the task cannot be
 109      *         scheduled for execution
 110      * @throws NullPointerException if command is null
 111      */