-
- All Superinterfaces:
-
Executor,ExecutorService
- 所有已知实现类:
-
ScheduledThreadPoolExecutor
public interface ScheduledExecutorService extends ExecutorService
ExecutorService,可以安排命令在给定延迟后运行,或定期执行。schedule方法创建具有各种延迟的任务,并返回可用于取消或检查执行的任务对象。scheduleAtFixedRate和scheduleWithFixedDelay方法创建并执行定期运行的任务,直到被取消。使用
Executor.execute(Runnable)和ExecutorServicesubmit方法提交的命令被调度,请求的延迟为零。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); } }- 从以下版本开始:
- 1.5
-
-
方法摘要
所有方法 实例方法 抽象方法 变量和类型 方法 描述 ScheduledFuture<?>schedule(Runnable command, long delay, TimeUnit unit)提交在给定延迟后启用的一次性任务。<V> ScheduledFuture<V>schedule(Callable<V> callable, long delay, TimeUnit unit)提交一个返回值的一次性任务,该任务在给定的延迟后变为启用状态。ScheduledFuture<?>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)提交定期操作,该操作在给定的初始延迟后首先启用,随后在给定的时间段内启用; 也就是说,执行将在initialDelay之后开始,然后是initialDelay + period,然后是initialDelay + 2 * period,依此类推。ScheduledFuture<?>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)提交在给定的初始延迟之后首先启用的定期动作,并且随后在一次执行的终止和下一次执行的开始之间给定延迟。-
声明方法的接口 java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
-
-
-
-
方法详细信息
-
schedule
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
提交在给定延迟后启用的一次性任务。- 参数
-
command- 要执行的任务 -
delay- 从现在起延迟执行的时间 -
unit- 延迟参数的时间单位 - 结果
-
表示任务未完成的ScheduledFuture,其完成后
get()方法将返回null - 异常
-
RejectedExecutionException- 如果无法安排执行任务 -
NullPointerException- 如果命令或单位为空
-
schedule
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
提交一个返回值的一次性任务,该任务在给定的延迟后变为启用状态。- 参数类型
-
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,依此类推。任务执行的顺序将无限期地继续,直到发生以下异常完成之一:
- 通过返回的未来任务是explicitly cancelled 。
- 执行程序终止,也导致任务取消。
- 执行任务会引发异常。 在这种情况下,在返回的将来调用
get将抛出ExecutionException,将异常作为其原因。
isDone()将返回true。如果此任务的执行时间超过其周期,则后续执行可能会延迟,但不会同时执行。
- 参数
-
command- 要执行的任务 -
initialDelay- 延迟首次执行的时间 -
period- 连续执行之间的时间间隔 -
unit- initialDelay和period参数的时间单位 - 结果
-
ScheduledFuture表示一系列重复任务的待完成。
未来的
get()方法永远不会正常返回,并且会在任务取消或任务执行异常终止时抛出异常。 - 异常
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果命令或单位为空 -
IllegalArgumentException- 如果周期小于或等于零
-
scheduleWithFixedDelay
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
提交在给定的初始延迟之后首先启用的定期动作,并且随后在一次执行的终止和下一次执行的开始之间给定延迟。任务执行的顺序将无限期地继续,直到发生以下异常完成之一:
- 通过返回的未来任务是explicitly cancelled 。
- 执行程序终止,也导致任务取消。
- 执行任务会引发异常。 在这种情况下,对返回的未来调用
get将抛出ExecutionException,将异常作为其原因。
isDone()将返回true。- 参数
-
command- 要执行的任务 -
initialDelay- 延迟首次执行的时间 -
delay- 一次执行终止与下一次执行开始之间的延迟 -
unit- initialDelay和delay参数的时间单位 - 结果
-
ScheduledFuture表示一系列重复任务的待完成。
未来的
get()方法永远不会正常返回,并且会在任务取消或任务执行异常终止时抛出异常。 - 异常
-
RejectedExecutionException- 如果无法安排任务执行 -
NullPointerException- 如果命令或单位为空 -
IllegalArgumentException- 如果延迟小于或等于零
-
-