ScheduledExecutorService接口定义

Executor接口的定义: http://donald-draper.iteye.com/blog/2365625
ExecutorService接口定义: http://donald-draper.iteye.com/blog/2365738
Future接口定义: http://donald-draper.iteye.com/blog/2365798
FutureTask解析: http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义: http://donald-draper.iteye.com/blog/2366239
ExecutorCompletionService解析: http://donald-draper.iteye.com/blog/2366254
AbstractExecutorService解析: http://donald-draper.iteye.com/blog/2366348
package java.util.concurrent;
import java.util.concurrent.atomic.*;
import java.util.*;

/**
 * An {@link ExecutorService} that can schedule commands to run after a given
 * delay, or to execute periodically.
 *
 ScheduledExecutorService可以在给定的时延后执行任务,或周期性、间隔性
 执行任务。
 * <p> The <tt>schedule</tt> methods create tasks with various delays
 * and return a task object that can be used to cancel or check
 * execution. The <tt>scheduleAtFixedRate</tt> and
 * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
 * that run periodically until cancelled.
 *
schedule可以执行指定时延的任务,返回一个ScheduledFuture,我们可以通过
ScheduledFuture取消或检查任务的执行。scheduleAtFixedRate和scheduleWithFixedDelay
方法可以创建和执行周期性的任务,直到任务取消
 * <p> Commands submitted using the {@link Executor#execute} and
 * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
 * a requested delay of zero. Zero and negative delays (but not
 * periods) are also allowed in <tt>schedule</tt> methods, and are
 * treated as requests for immediate execution.
 *
 如果提交的任务延时为0,则提交的任务用Executor#execute方法,或ExecutorService的
 submit方法执行任务。在schedule方法中,允许延时为0或负数,对于这种情况,
 我们当立即执行任务的请求对待。
 * <p>All <tt>schedule</tt> methods accept [i]relative[/i] delays and
 * periods as arguments, not absolute times or dates. It is a simple
 * matter to transform an absolute time represented as a {@link
 * java.util.Date} to the required form. For example, to schedule at
 * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
 * date.getTime() - System.currentTimeMillis(),
 * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
 * relative delay need not coincide with the current <tt>Date</tt> at
 * which the task is enabled due to network time synchronization
 * protocols, clock drift, or other factors.
 *
schedule接受一个相对的延时时间和时间单元做为参数,非绝对的时间或日期。
我们可以将一个java.util.Date表示的绝对时间转化为需要的格式。比如想在一个
确定的时间执行,可以用schedule(task,date.getTime() - System.currentTimeMillis(),TimeUnit.MILLISECONDS)。
使用绝对时间的转换的时候,要注意相对的过期时间,由于服务器网络时间同步协议导致的时间偏移,不能在
准确的时间调度延时任务。

 * The {@link Executors} class provides convenient factory methods for
 * the ScheduledExecutorService implementations provided in this package.
 *
 在执行器Executors提供了便利的工厂方法,用于创建ScheduledExecutorService。
 * <h3>Usage Example</h3>
 *
 * Here is a class with a method that sets up a ScheduledExecutorService
 * to beep every ten seconds for an hour:
 *
 下面是一个在一小时内,每10调度一次周期任务的示例:
 *  <pre> {@code
 * import static java.util.concurrent.TimeUnit.*;
 * class BeeperControl {
 *   private final ScheduledExecutorService scheduler =
 *     Executors.newScheduledThreadPool(1);
 *
 *   public void beepForAnHour() {
 *     final Runnable beeper = new Runnable() {
 *       public void run() { System.out.println("beep"); }
 *     };
 *     final ScheduledFuture<?> beeperHandle =
         //每10秒执行一次,周期任务
 *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
       //一个小时后,取消周期任务
 *     scheduler.schedule(new Runnable() {
 *       public void run() { beeperHandle.cancel(true); }
 *     }, 60 * 60, SECONDS);
 *   }
 * }}</pre>
 *
 * @since 1.5
 * @author Doug Lea
 */
public interface ScheduledExecutorService extends ExecutorService {

    /**
     * Creates and executes a one-shot action that becomes enabled
     * after the given delay.
     根据Runnable,delay创建一个只执行一次的延时无结果任务ScheduledFuture<?>,延时时间过后,执行无结果任务
     *
     * @param command the task to execute
     * @param delay the time from now to delay execution
     * @param unit the time unit of the delay parameter
     * @return a ScheduledFuture representing pending completion of
     *         the task and whose <tt>get()</tt> method will return
     *         <tt>null</tt> upon completion
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     */
    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);

    /**
     * Creates and executes a ScheduledFuture that becomes enabled after the
     * given delay.
     *
      根据Callable,delay创建一个只执行一次的延时有结果任务ScheduledFuture<V>,延时时间过后,执行任务
     * @param callable the function to execute
     * @param delay the time from now to delay execution
     * @param unit the time unit of the delay parameter
     * @return a ScheduledFuture that can be used to extract result or cancel
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if callable is null
     */
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first
     * after the given initial delay, and subsequently with the given
     * period; that is executions will commence after
     * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
     * <tt>initialDelay + 2 * period</tt>, and so on.
     * If any execution of the task
     * encounters an exception, subsequent executions are suppressed.
     * Otherwise, the task will only terminate via cancellation or
     * termination of the executor.  If any execution of this task
     * takes longer than its period, then subsequent executions
     * may start late, but will not concurrently execute.
     *
     创建执行一个周期性间隔为period的任务,在初始化延时initialDelay后,执行任务,
     然后在initialDelay+period执行任务,接着在initialDelay + 2 * period执行任务,依次类推。
     如果任务一个任务执行异常,则后续的任务将会被取消。另外任务执行被结束通过取消或执行器执行结束。
     如果周期性任务中的一个任务所花时间大于period,后续的任务执行将会延迟,而不是并发执行。
     initialDelay为0,period为2,每个任务执行需要3秒
     0--2--4--6--....
     t0--t1--t2--t3--...
     * @param command the task to execute
     * @param initialDelay the time to delay first execution
     * @param period the period between successive executions
     * @param unit the time unit of the initialDelay and period parameters
     * @return a ScheduledFuture representing pending completion of
     *         the task, and whose <tt>get()</tt> method will throw an
     *         exception upon cancellation
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     * @throws IllegalArgumentException if period less than or equal to zero
     */
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first
     * after the given initial delay, and subsequently with the
     * given delay between the termination of one execution and the
     * commencement of the next.  If any execution of the task
     * encounters an exception, subsequent executions are suppressed.
     * Otherwise, the task will only terminate via cancellation or
     * termination of the executor.
     创建执行一个周期性间隔为period的任务,在初始化延时initialDelay后,执行第一个周期性任务,
     后续任务执行的延迟时间为前一个任务执行结束和下一个任务开始的时间间隔。我们假设initialDelay为0,delay为2,
     每个任务执行需要3秒,第一个任务执行后为3秒,下一个任务执行的时间为5秒(3+2)。有时候
     0---3--5---8--10----14--16-----...
     t0-----t1(5)-----t2(10)------t3(16)-----...
    我们假设t2执行的时间为4秒,那t3执行的时间点为16。
     如果任务有一个任务执行异常,则后续的任务将会被取消。另外任务执行被结束通过取消或执行器执行结束。
     *
     * @param command the task to execute
     * @param initialDelay the time to delay first execution
     * @param delay the delay between the termination of one
     * execution and the commencement of the next
     * @param unit the time unit of the initialDelay and delay parameters
     * @return a ScheduledFuture representing pending completion of
     *         the task, and whose <tt>get()</tt> method will throw an
     *         exception upon cancellation
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if command is null
     * @throws IllegalArgumentException if delay less than or equal to zero
     */
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

}



//ScheduledFuture,继承了Delayed和Future接口
/**
 * A delayed result-bearing action that can be cancelled.
 * Usually a scheduled future is the result of scheduling
 * a task with a {@link ScheduledExecutorService}.
 *
 * @since 1.5
 * @author Doug Lea
 * @param <V> The result type returned by this Future
 */
public interface ScheduledFuture<V> extends Delayed, Future<V> {
}

猜你喜欢

转载自donald-draper.iteye.com/blog/2366436