1. /*
  2. * @(#)ScheduledExecutorService.java 1.2 04/04/14
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent;
  8. import java.util.concurrent.atomic.*;
  9. import java.util.*;
  10. /**
  11. * An {@link ExecutorService} that can schedule commands to run after a given
  12. * delay, or to execute periodically.
  13. *
  14. * <p> The <tt>schedule</tt> methods create tasks with various delays
  15. * and return a task object that can be used to cancel or check
  16. * execution. The <tt>scheduleAtFixedRate</tt> and
  17. * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
  18. * that run periodically until cancelled.
  19. *
  20. * <p> Commands submitted using the {@link Executor#execute} and
  21. * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
  22. * a requested delay of zero. Zero and negative delays (but not
  23. * periods) are also allowed in <tt>schedule</tt> methods, and are
  24. * treated as requests for immediate execution.
  25. *
  26. * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
  27. * periods as arguments, not absolute times or dates. It is a simple
  28. * matter to transform an absolute time represented as a {@link
  29. * java.util.Date} to the required form. For example, to schedule at
  30. * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
  31. * date.getTime() - System.currentTimeMillis(),
  32. * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
  33. * relative delay need not coincide with the current <tt>Date</tt> at
  34. * which the task is enabled due to network time synchronization
  35. * protocols, clock drift, or other factors.
  36. *
  37. * The {@link Executors} class provides convenient factory methods for
  38. * the ScheduledExecutorService implementations provided in this package.
  39. *
  40. * <h3>Usage Example</h3>
  41. *
  42. * Here is a class with a method that sets up a ScheduledExecutorService
  43. * to beep every ten seconds for an hour:
  44. *
  45. * <pre>
  46. * import static java.util.concurrent.TimeUnit.*;
  47. * class BeeperControl {
  48. * private final ScheduledExecutorService scheduler =
  49. * Executors.newScheduledThreadPool(1);
  50. *
  51. * public void beepForAnHour() {
  52. * final Runnable beeper = new Runnable() {
  53. * public void run() { System.out.println("beep"); }
  54. * };
  55. * final ScheduledFuture<?> beeperHandle =
  56. * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
  57. * scheduler.schedule(new Runnable() {
  58. * public void run() { beeperHandle.cancel(true); }
  59. * }, 60 * 60, SECONDS);
  60. * }
  61. * }
  62. * </pre>
  63. *
  64. * @since 1.5
  65. * @author Doug Lea
  66. */
  67. public interface ScheduledExecutorService extends ExecutorService {
  68. /**
  69. * Creates and executes a one-shot action that becomes enabled
  70. * after the given delay.
  71. * @param command the task to execute.
  72. * @param delay the time from now to delay execution.
  73. * @param unit the time unit of the delay parameter.
  74. * @return a Future representing pending completion of the task,
  75. * and whose <tt>get()</tt> method will return <tt>null</tt>
  76. * upon completion.
  77. * @throws RejectedExecutionException if task cannot be scheduled
  78. * for execution.
  79. * @throws NullPointerException if command is null
  80. */
  81. public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
  82. /**
  83. * Creates and executes a ScheduledFuture that becomes enabled after the
  84. * given delay.
  85. * @param callable the function to execute.
  86. * @param delay the time from now to delay execution.
  87. * @param unit the time unit of the delay parameter.
  88. * @return a ScheduledFuture that can be used to extract result or cancel.
  89. * @throws RejectedExecutionException if task cannot be scheduled
  90. * for execution.
  91. * @throws NullPointerException if callable is null
  92. */
  93. public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
  94. /**
  95. * Creates and executes a periodic action that becomes enabled first
  96. * after the given initial delay, and subsequently with the given
  97. * period; that is executions will commence after
  98. * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
  99. * <tt>initialDelay + 2 * period</tt>, and so on.
  100. * If any execution of the task
  101. * encounters an exception, subsequent executions are suppressed.
  102. * Otherwise, the task will only terminate via cancellation or
  103. * termination of the executor.
  104. * @param command the task to execute.
  105. * @param initialDelay the time to delay first execution.
  106. * @param period the period between successive executions.
  107. * @param unit the time unit of the initialDelay and period parameters
  108. * @return a Future representing pending completion of the task,
  109. * and whose <tt>get()</tt> method will throw an exception upon
  110. * cancellation.
  111. * @throws RejectedExecutionException if task cannot be scheduled
  112. * for execution.
  113. * @throws NullPointerException if command is null
  114. * @throws IllegalArgumentException if period less than or equal to zero.
  115. */
  116. public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
  117. /**
  118. * Creates and executes a periodic action that becomes enabled first
  119. * after the given initial delay, and subsequently with the
  120. * given delay between the termination of one execution and the
  121. * commencement of the next. If any execution of the task
  122. * encounters an exception, subsequent executions are suppressed.
  123. * Otherwise, the task will only terminate via cancellation or
  124. * termination of the executor.
  125. * @param command the task to execute.
  126. * @param initialDelay the time to delay first execution.
  127. * @param delay the delay between the termination of one
  128. * execution and the commencement of the next.
  129. * @param unit the time unit of the initialDelay and delay parameters
  130. * @return a Future representing pending completion of the task,
  131. * and whose <tt>get()</tt> method will throw an exception upon
  132. * cancellation.
  133. * @throws RejectedExecutionException if task cannot be scheduled
  134. * for execution.
  135. * @throws NullPointerException if command is null
  136. * @throws IllegalArgumentException if delay less than or equal to zero.
  137. */
  138. public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
  139. }