java.util.concurrent
Interface ScheduledExecutorService
-
All Superinterfaces:
所有已知实现类:
public interface ScheduledExecutorService extends ExecutorService
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设置为每10秒钟发出一个小时的蜂鸣声: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); } }
从以下版本开始:
1.5
-
-
方法摘要
所有方法 接口方法 抽象方法 Modifier and Type Method and Description <V> ScheduledFuture<V>
schedule(Callable<V> callable, long delay, TimeUnit unit)
创建并执行在给定延迟后启用的ScheduledFuture。
ScheduledFuture<?>
schedule(Runnable command, 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)
创建并执行在给定的初始延迟之后首先启用的定期动作,随后在一个执行的终止和下一个执行的开始之间给定的延迟。
-
方法详细信息
-
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)
创建并执行在给定延迟后启用的ScheduledFuture。
参数类型
V
- 可调用结果的类型参数
callable
- 执行的功能delay
- 从现在开始延迟执行的时间unit
- 延迟参数的时间单位结果
一个可用于提取结果或取消的ScheduledFuture
异常
RejectedExecutionException
- 如果该任务无法安排执行NullPointerException
- 如果可调用为空 -
scheduleAtFixedRate
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建并执行在给定的初始延迟之后,随后以给定的时间段首先启用的周期性动作; 那就是执行将在
initialDelay
之后开始,然后initialDelay+period
,然后是initialDelay + 2 * period
,等等。 如果任务的执行遇到异常,则后续的执行被抑制。 否则,任务将仅通过取消或终止执行人终止。 如果任务执行时间比其周期长,则后续执行可能会迟到,但不会同时执行。参数
command
- 要执行的任务initialDelay
- 延迟第一次执行的时间period
- 连续执行之间的时期unit
- initialDelay和period参数的时间单位结果
一个ScheduledFuture代表待完成的任务,其
get()
方法将在取消时抛出异常异常
RejectedExecutionException
- 如果任务无法安排执行NullPointerException
- 如果命令为空IllegalArgumentException
- 如果周期小于或等于零 -
scheduleWithFixedDelay
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
创建并执行在给定的初始延迟之后首先启用的定期动作,随后在一个执行的终止和下一个执行的开始之间给定的延迟。 如果任务的执行遇到异常,则后续的执行被抑制。 否则,任务将仅通过取消或终止执行人终止。
参数
command
- 要执行的任务initialDelay
- 延迟第一次执行的时间delay
- 一个执行终止与下一个执行的开始之间的延迟unit
- initialDelay和delay参数的时间单位结果
一个ScheduledFuture代表待完成的任务,其
get()
方法将在取消时抛出异常异常
RejectedExecutionException
- 如果任务不能安排执行NullPointerException
- 如果命令为空IllegalArgumentException
- 如果延迟小于或等于零
使用实例:
schedule + Runnable 延迟执行任务
1、ScheduledFuture<?> schedule(Runnable command,long delay,TimeUnit unit) :创建并执行在给定延迟后启用的一次性操作。
2、参数:command - 要执行的任务;delay - 从现在开始延迟执行的时间;unit - 延迟参数的时间单位
schedule + Callable 延迟执行任务
1、ScheduledFuture<V> schedule(Callable<V> callable, long delay,TimeUnit unit):创建并执行在给定延迟后启用的 一次性操作
2、参数:callable - 要执行的功能;delay - 从现在开始延迟执行的时间;unit - 延迟参数的时间单位
scheduleAtFixedRate 周期性执行任务
1、ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay, long period, TimeUnit unit):创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;
2、在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。意思是下一次执行任务的时间与任务执行过程花费的时间无关,只与period有关!
3、如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。
4、如果任务的任何一个执行遇到异常,则后续执行都会被取消。否则,只能通过执行程序的取消或终止方法来终止该任务。
5、参数:command - 要执行的任务;initialDelay - 首次执行的延迟时间;period - 连续执行之间的周期;unit - initialDelay 和 period 参数的时间单位
scheduleWithFixedDelay 周期性执行任务
1、ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit):创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
2、与 scheduleFixedDelay 区别仅仅在于前后两次任务执行的时间间隔不同而已