scheduleWithFixedDelay和scheduleAtFixedRate的区别

scheduleWithFixedDelay和scheduleAtFixedRate的区别

          • scheduleWithFixedDelay:
          • scheduleAtFixedRate

  • scheduleWithFixedDelay:

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.

scheduleWithFixedDelay是每次任务基于"上一次结束时间"来延迟固定时间后执行下一次任务;

  • scheduleAtFixedRate

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.

scheduleAtFixedRate是每次任务基于"上一次开始时间"来决定下次启动的时间, 如果一次任务执行时间大于间隔时间, 会导致下一次延缓执行, 不会有两次任务并行执行

你可能感兴趣的:(java)