Most visited

Recently visited

Added in API level 1

ScheduledExecutorService

public interface ScheduledExecutorService
implements ExecutorService

java.util.concurrent.ScheduledExecutorService
Known Indirect Subclasses


一个 ExecutorService ,可以安排命令在给定的延迟后运行,或定期执行。

schedule方法创建具有各种延迟的任务并返回可用于取消或检查执行的任务对象。 scheduleAtFixedRatescheduleWithFixedDelay方法创建并执行定期运行的任务,直到取消。

使用execute(Runnable)ExecutorService submit方法提交的命令被调度,请求延迟为零。 schedule方法中也允许零和负延迟(但不包括周期),并被视为立即执行的请求。

所有schedule方法接受相对延迟和时间段作为参数,而不是绝对时间或日期。 把一个代表Date的绝对时间转换成所需的形式是一件简单的事情。 例如,要安排在某个未来date ,您可以使用: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS) 然而,请注意,由于网络时间同步协议,时钟漂移或其他因素,相对延迟的到期不必与启用任务的当前Date一致。

Executors类为此包中提供的ScheduledExecutorService实现提供了方便的工厂方法。

Usage Example

Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:
 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 =
       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
     scheduler.schedule(new Runnable() {
       public void run() { beeperHandle.cancel(true); }
     }, 60 * 60, SECONDS);
   }
 }

Summary

Public methods

abstract <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)

创建并执行一个ScheduledFuture,在给定的延迟后变为启用状态。

abstract ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)

创建并执行在给定延迟后变为启用的一次性操作。

abstract ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

创建并执行一个定期动作,在给定的初始延迟之后首先变为启用,然后在给定的时间段内启用; 即执行将在initialDelay之后开始,然后是initialDelay + period ,然后是initialDelay + 2 * period ,依此类推。

abstract ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

创建并执行一个定期动作,该动作在给定的初始延迟后首先变为有效,随后在一次执行终止和下一次执行终止之间给定延迟。

Inherited methods

From interface java.util.concurrent.ExecutorService
From interface java.util.concurrent.Executor

Public methods

schedule

Added in API level 1
ScheduledFuture<V> schedule (Callable<V> callable, 
                long delay, 
                TimeUnit unit)

创建并执行一个ScheduledFuture,在给定的延迟后变为启用状态。

Parameters
callable Callable: the function to execute
delay long: the time from now to delay execution
unit TimeUnit: the time unit of the delay parameter
Returns
ScheduledFuture<V> a ScheduledFuture that can be used to extract result or cancel
Throws
RejectedExecutionException if the task cannot be scheduled for execution
NullPointerException if callable is null

schedule

Added in API level 1
ScheduledFuture<?> schedule (Runnable command, 
                long delay, 
                TimeUnit unit)

创建并执行在给定延迟后变为启用的一次性操作。

Parameters
command Runnable: the task to execute
delay long: the time from now to delay execution
unit TimeUnit: the time unit of the delay parameter
Returns
ScheduledFuture<?> a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
Throws
RejectedExecutionException if the task cannot be scheduled for execution
NullPointerException if command is null

scheduleAtFixedRate

Added in API level 1
ScheduledFuture<?> scheduleAtFixedRate (Runnable command, 
                long initialDelay, 
                long period, 
                TimeUnit unit)

创建并执行一个定期动作,在给定的初始延迟之后首先变为启用,然后在给定的时间段内启用; 也就是说,处决将在initialDelay之后开始,然后是initialDelay + period ,然后是initialDelay + 2 * period ,依此类推。

任务执行顺序无限期地继续,直到发生以下异常完成之一:

  • The task is explicitly cancelled via the returned future.
  • The executor terminates, also resulting in task cancellation.
  • An execution of the task throws an exception. In this case calling get on the returned future will throw ExecutionException.
Subsequent executions are suppressed. Subsequent calls to isDone() on the returned future will return true.

如果任务的执行时间比其周期长,则后续执行可能会晚点,但不会同时执行。

Parameters
command Runnable: the task to execute
initialDelay long: the time to delay first execution
period long: the period between successive executions
unit TimeUnit: the time unit of the initialDelay and period parameters
Returns
ScheduledFuture<?> a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.
Throws
RejectedExecutionException if the task cannot be scheduled for execution
NullPointerException if command is null
IllegalArgumentException if period less than or equal to zero

scheduleWithFixedDelay

Added in API level 1
ScheduledFuture<?> scheduleWithFixedDelay (Runnable command, 
                long initialDelay, 
                long delay, 
                TimeUnit unit)

创建并执行一个定期动作,该动作在给定的初始延迟后首先变为有效,随后在一次执行终止和下一次执行终止之间给定延迟。

任务执行顺序无限期地继续,直到发生以下异常完成之一:

  • The task is explicitly cancelled via the returned future.
  • The executor terminates, also resulting in task cancellation.
  • An execution of the task throws an exception. In this case calling get on the returned future will throw ExecutionException.
Subsequent executions are suppressed. Subsequent calls to isDone() on the returned future will return true.

Parameters
command Runnable: the task to execute
initialDelay long: the time to delay first execution
delay long: the delay between the termination of one execution and the commencement of the next
unit TimeUnit: the time unit of the initialDelay and delay parameters
Returns
ScheduledFuture<?> a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.
Throws
RejectedExecutionException if the task cannot be scheduled for execution
NullPointerException if command is null
IllegalArgumentException if delay less than or equal to zero

Hooray!