总结:

scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。

scheduleWithFixedDelay,是以上一个任务结束时开始计时,period时间过去后,立即执行。

重点:

两个方法以不同的时间点作为参考。

例子:

package com.yuankai.t1.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleExecutorServiceTest {

public static void main(String[] args) {ScheduleExecutorServiceTest test = new ScheduleExecutorServiceTest();test.testWithFixedDelay();
}private ScheduledExecutorService executor;public ScheduleExecutorServiceTest() {executor = Executors.newScheduledThreadPool(4);
}public void testAtFixedRate() {executor.scheduleAtFixedRate(new Runnable() {public void run() {System.out.println("====");try {Thread.sleep(10000);System.out.println("执行完毕");} catch (InterruptedException e) {e.printStackTrace();}}}, 1000, 3000, TimeUnit.MILLISECONDS);
}public void testWithFixedDelay() {executor.scheduleWithFixedDelay(new Runnable() {public void run() {System.out.println("====");try {int i = 1 / 0;} catch (Exception e) {e.printStackTrace();}/*try {Thread.sleep(10000);System.out.println("执行完毕");} catch (InterruptedException e) {e.printStackTrace();}*/}}, 1000, 3000, TimeUnit.MILLISECONDS);
}

}

注意:
通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常
————————————————
版权声明:本文为CSDN博主「琳凯」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013819945/article/details/47723091

scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别相关推荐

  1. JAVA定时器ScheduledExecutorService中,scheduleAtFixedRate和scheduleWithFixedDelay的区别

    我们通过运行代码来查看两者之间的区别: 1.首先创建一个需要执行5秒的任务: // 执行这个任务需要5秒 Runnable runnable = new Runnable() {public void ...

  2. 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

    scheduleAtFixedRate:是以period为间隔来执行任务的,如果任务执行时间小于period,则上次任务执行完成后会间隔period后再去执行下一次任务:但如果任务执行时间大于peri ...

  3. scheduleAtFixedRate和scheduleWithFixedDelay的区别

    1)scheduleAtFixedRate:可以传入runnable,定制第一次的初始化执行时间,周期时间,单位时间-------创建并执行一个周期性任务,过了给定的初始延迟时间(1min执行一次or ...

  4. scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别和实例

    scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需 ...

  5. ScheduledExecutorService的简单使用、scheduleAtFixedRate和scheduleWithFixedDelay区别

    ScheduledExecutorService的简单使用.scheduleAtFixedRate和scheduleWithFixedDelay区别 ScheduledExecutorService ...

  6. scheduleAtFixedRate和scheduleWithFixedDelay 区别

    https://www.jianshu.com/p/2900b4fd3bdd Executors提供的线程池ScheduledExecutorService中有两个方法,scheduleAtFixed ...

  7. 详解scheduleAtFixedRate与scheduleWithFixedDelay原理

    前言 前几天,肥佬分享了一篇关于定时器的文章你真的会使用定时器吗?,从使用角度为我们详细地说明了定时器的用法,包括 fixedDelay.fixedRate,为什么会有这样的区别呢?下面我们从源码角度 ...

  8. 写段代码理解 scheduleAtFixedRate和scheduleWithFixedDelay

    ① 如果任务在周期内正常结束掉的话scheduleAtFixedRate和scheduleWithFixedDelay没有差别 public void start() {ScheduledExecut ...

  9. Java定时线程实现:scheduleAtFixedRate 和 scheduleWithFixedDelay 的差别

    Java实现定时任务,一般都是用一个线程,设置个时间,让他定时执行,注意力一般都是集中在这个线程的实现,很少考虑到具体定时执行线程的这个过程.scheduleAtFixedRate 和 schedul ...

最新文章

  1. 收敛速度更快更稳定的Wasserstein GAN(WGAN)
  2. 智能媒体管理服务控制台功能体验
  3. CacheManager - 用 C# 编写的 .NET 的开源缓存抽象层
  4. 关于曼哈顿距离和切比雪夫距离的转换和应用
  5. TestStand版本切换注意事项(重启电脑)
  6. 定量数据和定性数据_定性数据:赋予大数据意义的上下文
  7. matlab学习笔记 clc和clear
  8. 游戏里的答题都有哪些类型
  9. 微信新增「炸屎」表情互动特效,玩上瘾了
  10. Poi的Excel的导入导出 支持2007
  11. 以低字节为字地址是什么意思?
  12. 用电脑键盘快速打出特殊符号!
  13. M1001 4G/5G应急指挥箱
  14. 【晚唐 杜牧】九日齐山登高
  15. 1140 单位矩阵初始化
  16. 慕课头条:拼多多海外版表现亮眼;小度配送机器人发布;土巴兔也玩儿元宇宙...
  17. springboot使用ajax上传文件
  18. php100以内质数求和,100以内的质数_PHP质数计算三种方法 php求100以内的质数
  19. 今日头条频道管理(自带动画效果)
  20. 拆解19年雅诗兰黛双十一25分钟预售破5亿的营销策略

热门文章

  1. 用科技装点套路,你的浪漫情人节将变得有点新奇
  2. 尘埃落定 所有你想了解的Apple Watch综述
  3. vue中获取页面滚动的高度
  4. 连马云都自愧不如 真正的草根创业者
  5. python的浮点数_python中浮点数的精度
  6. 牛客 考研复试 Old Bill
  7. ElementUI之el-tag标签移除
  8. 豆瓣石岩谈豆瓣阅读前端排版工作的感受
  9. PyCharm设置External Libraries
  10. H5 下载word文件时预览失败,提示无法预览此文件,它可能已损坏,有部分可可以查看预览