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 period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, 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指定的时长为周期运行任务;如果遇到异常,则后续的执行被压制不执行;如果任务的执行时间超过了period,后续的执行会延迟启动,不会并发执行,线程池会连续执行被积压的任务
(此时rate)实际上是不保证的,例如因为某次执行超时积压了3次执行任务,则此超时任务完成后,线程池可能会连续执行3次(假设正常是1s执行一次)从而“赶上”步伐,保证后续的执行正常
进行。】
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
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.
【保证两次任务执行之间的间隔为delay指定的时长;如果执行的任务出现异常,后续的任务均会被压制住不执行;正常情况下,只有cancel或者shutdown操作才会停止执行。】
[JAVA学习笔记-73]Executors的两种定时执行机制
最新推荐文章于 2023-07-05 15:20:24 发布