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

  • ScheduledExecutorService
    • 执行周期性任务的两个方法
      • 方法介绍
        • 1、scheduleAtFixedRate
        • 2、scheduleWithFixedDelay
        • 3、区别与联系
        • 4、总结:

ScheduledExecutorService

JAVA用于执行周期性任务的线程池:
ScheduledExecutorService
ScheduledExecutorService的继承关系

执行周期性任务的两个方法

  • scheduleAtFixedRate
  • scheduleWithFixedDelay

方法介绍

1、scheduleAtFixedRate

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);
  • Runnable command 任务对象,Runnable 及其子类
  • long initialDelay 任务开始执行的延迟时间
  • long period 任务执行的间隔周期 例如 1s 10min 1h 等
  • TimeUnit unit 任务执行周期的时间单位

示例:每隔1s打印一次hello world

@Slf4j
public class ThreadTest {private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);public static void main(String[] args) {log.info("task begin");executorService.scheduleAtFixedRate(() -> {log.info("hello world !");}, 1, 1, TimeUnit.SECONDS);}
}

运行结果:

2、scheduleWithFixedDelay

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);
  • Runnable command 任务对象
  • long initialDelay 任务开始执行的延迟时间
  • long period 任务执行的间隔周期 例如 1s 10min 1h 等
  • TimeUnit unit 任务执行周期的时间单位

示例:每隔1s打印一次hello world

@Slf4j
public class ThreadTest {private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);public static void main(String[] args) {log.info("task begin");executorService.scheduleWithFixedDelay(() -> {log.info("hello world !");}, 1, 1, TimeUnit.SECONDS);}
}

运行结果:

3、区别与联系

  • 两个方法都可以执行周期性的任务,在任务执行时间比较短时,小于任务执行周期的时候,两者几乎看不出什么区别

  • 当任务执行时间大于任务执行周期的时候,如下案例

    我们让两个方法执行周期都是1s,任务执行时间变成2s

@Slf4j
public class ThreadTest {private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);public static void main(String[] args) {log.info("task begin");executorService.scheduleAtFixedRate(() -> {log.info("hello world !");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}, 1, 1, TimeUnit.SECONDS);}
}

执行结果:

@Slf4j
public class ThreadTest {private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);public static void main(String[] args) {log.info("task begin");executorService.scheduleWithFixedDelay(() -> {log.info("hello world !");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}, 1, 1, TimeUnit.SECONDS);}
}

执行结果:

4、总结:

  • 可以看到 scheduleAtFixedRate 是从上个任务开始的时间计时,如果任务的执行时间小于任务的周期,则任务周期到了之后立即执行下个任务;如果执行任务的时间大于任务的执行周期,这时候的任务执行时间会覆盖任务的执行周期,真正的间隔时间变成了任务的执行时间
  • scheduleWithFixedDelay是等待上一个任务结束时才开始计时

ScheduledExecutorService的简单使用、scheduleAtFixedRate和scheduleWithFixedDelay区别相关推荐

  1. scheduleAtFixedRate和scheduleWithFixedDelay 区别

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

  2. scheduleAtFixedRate和scheduleWithFixedDelay区别

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

  3. 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

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

  4. 详解scheduleAtFixedRate与scheduleWithFixedDelay原理

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

  5. 写段代码理解 scheduleAtFixedRate和scheduleWithFixedDelay

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

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

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

  7. newScheduledThreadPool : scheduleAtFixedRate 与 scheduleWithFixedDelay 详解

    一.引言 newScheduledThreadPool 周期性线程池提供了周期执行任务的方法 scheduleAtFixedRate 与 scheduleWithFixedDelay,两者比较容易混淆 ...

  8. Java并发编程—schedule方法和scheduleAtFixedRate方法的区别

    原文作者:一叶丿清风 原文地址:schedule方法和scheduleAtFixedRate方法的区别 schedule方法和scheduleAtFixedRate方法都可以实现任务的延时和不延时执行 ...

  9. 深入源码,CompletableFuture 简单与链式的区别?

    导读:从 JDK 8 开始,在 Concurrent 包中提供了一个强大的异步编程工具 CompletableFuture.在 JDK8 之前,异步编程可以通过线程池和 Future 来实现,但功能还 ...

最新文章

  1. JAVA String format 方法使用介绍
  2. oddo docker 安装
  3. X/Open DTP模型与XA协议的学习笔记
  4. 0-1背包问题(物品不可分割)
  5. 手游传奇刷元宝_传奇手游 平民制霸刀刀爆元宝!
  6. Linux下Bash编程之算术运算符详解(三)
  7. Mysql数据库启动命令
  8. 集成电路模拟版图入门-版图基础学习笔记(四)
  9. 杰奇1.7--关关采集器使用教程
  10. Java | ProGuard——java代码混淆利器
  11. 低通滤波器转带通滤波器公式由来_无源滤波器应用或电路中的带通滤波器原理...
  12. 怎么 把计算机里的照片变成背景,用画图怎么改照片底色
  13. Android人脸识别的初步学习
  14. mpp的文件要下载什么来打开
  15. 如何快速为视频添加ai字幕的解决方案
  16. 为什么阿里那么难进,原来精髓在这
  17. 使用h5的方式来实现钟表
  18. 号外!号外!仅需5000元,即可配置Apollo计算平台!
  19. 质子交换膜燃料电池流场设计(节选自课程设计作业)
  20. 【数字逻辑】——逻辑函数及其简化(学习笔记)

热门文章

  1. 二嗨租车java,1嗨租车,租的放心用的更舒心
  2. PS学习笔记----图层蒙版
  3. Cocos Creator 3D 案例《弹弹乐》技术实现分享
  4. Win8下使用EasyBCD硬盘安装kubuntu12.10和CentOs5.5
  5. request.getParameter讲解
  6. Spring 消息模式
  7. web应用常见的攻击手段
  8. HDU 4528 小明系列故事——捉迷藏
  9. linux配置jdk rmp
  10. 【Minecraft java edition 模组开发】(一):实现一个简单的模组