- 所有超接口:
-
AutoCloseable,Executor,ExecutorService
- 所有已知实现类:
-
ScheduledThreadPoolExecutor
一个可以在给定延迟后调度命令运行或定期执行的
ExecutorService。
schedule方法创建具有不同延迟的任务,并返回一个任务对象,该对象可用于取消或检查执行。 scheduleAtFixedRate和scheduleWithFixedDelay方法创建并执行定期运行的任务,直到被取消。
使用Executor.execute(Runnable)和ExecutorService submit方法提交的命令将被安排在请求的延迟为零的情况下。在schedule方法中也允许零和负延迟(但不允许周期),并且将被视为立即执行的请求。
所有schedule方法接受相对延迟和周期作为参数,而不是绝对时间或日期。将绝对时间表示为Date并转换为所需形式是一件简单的事情。例如,要在将来的某个特定date上安排任务,您可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。但要注意,由于网络时间同步协议、时钟漂移或其他因素,相对延迟的到期时间不一定与启用任务的当前Date相一致。
Executors类提供了此包中提供的ScheduledExecutorService实现的便捷工厂方法。
用法示例
这是一个具有设置ScheduledExecutorService每十秒响一次的方法的类,持续一个小时:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
Runnable beeper = () -> System.out.println("beep");
ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
Runnable canceller = () -> beeperHandle.cancel(false);
scheduler.schedule(canceller, 1, HOURS);
}
}
- 自 JDK 1.5 起:
- 1.5
-
Method Summary
Modifier and TypeMethodDescription提交一个一次性任务,在给定延迟后启用。<V> ScheduledFuture<V> 提交一个返回值的一次性任务,在给定延迟后启用。scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 提交一个定期动作,首次在给定初始延迟后启用,随后以给定周期启用;也就是说,执行将在initialDelay后开始,然后initialDelay + period,然后initialDelay + 2 * period,依此类推。scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) 提交一个定期动作,首次在给定初始延迟后启用,随后在一次执行的终止和下一次执行的开始之间具有给定延迟。Methods declared in interface java.util.concurrent.ExecutorService
awaitTermination, close, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
-
Method Details
-
schedule
提交一个一次性任务,在给定延迟后启用。- 参数:
-
command- 要执行的任务 -
delay- 从现在开始延迟执行的时间 -
unit- 延迟参数的时间单位 - 返回:
-
代表任务待完成的ScheduledFuture,其
get()方法在完成时将返回null - 抛出:
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果command或unit为null
-
schedule
提交一个返回值的一次性任务,在给定延迟后启用。- 类型参数:
-
V- 可调用结果的类型 - 参数:
-
callable- 要执行的函数 -
delay- 从现在开始延迟执行的时间 -
unit- 延迟参数的时间单位 - 返回:
- 一个ScheduledFuture,可用于提取结果或取消
- 抛出:
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果callable或unit为null
-
scheduleAtFixedRate
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 提交一个定期动作,首次在给定初始延迟后启用,随后以给定周期启用;也就是说,执行将在initialDelay后开始,然后initialDelay + period,然后initialDelay + 2 * period,依此类推。任务执行序列将无限期地继续,直到发生以下异常完成之一:
- 通过返回的future显式取消任务。
- 执行程序终止,也导致任务取消。
- 任务的执行引发异常。在这种情况下,调用返回的future上的
get将抛出ExecutionException,将异常作为其原因。
isDone()的后续调用将返回true。如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会同时执行。
- 参数:
-
command- 要执行的任务 -
initialDelay- 第一次执行的延迟时间 -
period- 连续执行之间的周期 -
unit- initialDelay和period参数的时间单位 - 返回:
-
代表重复任务系列待完成的ScheduledFuture。该future的
get()方法永远不会正常返回,并且在任务取消或任务执行异常终止时将抛出异常。 - 抛出:
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果command或unit为null -
IllegalArgumentException- 如果period小于或等于零
-
scheduleWithFixedDelay
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) 提交一个定期动作,首次在给定初始延迟后启用,随后在一次执行的终止和下一次执行的开始之间具有给定延迟。任务执行序列将无限期地继续,直到发生以下异常完成之一:
- 通过返回的future显式取消任务。
- 执行程序终止,也导致任务取消。
- 任务的执行引发异常。在这种情况下,调用返回的future上的
get将抛出ExecutionException,将异常作为其原因。
isDone()的后续调用将返回true。- 参数:
-
command- 要执行的任务 -
initialDelay- 第一次执行的延迟时间 -
delay- 一次执行的终止和下一次执行的开始之间的延迟 -
unit- initialDelay和delay参数的时间单位 - 返回:
-
代表重复任务系列待完成的ScheduledFuture。该future的
get()方法永远不会正常返回,并且在任务取消或任务执行异常终止时将抛出异常。 - 抛出:
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果command或unit为null -
IllegalArgumentException- 如果delay小于或等于零
-