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

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

重点:

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

例子:

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();}}}, 5000, 1000, 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();}*/}}, 5000, 1000, TimeUnit.MILLISECONDS);}}

注意:
通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常。

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. i++与++i的区别,使用实例说明

    /*** 类名:TEST.java<br>* <p>* 功能:i++与++i的区别,使用实例说明* </p>* * @Author:<a href=" ...

  9. php class中public,private,protected的区别以及实例分析

    本篇文章是对php class中public,private,protected的区别以及实例进行了详细的分析介绍,需要的朋友参考下 一,public,private,protected的区别 pub ...

最新文章

  1. python 比较文件不同,在python中逐行比较两个不同的文件
  2. Thread.currentThread().getContextClassLoader()和Class.getClassLoader()区别
  3. 系统学习机器学习之特征工程(二)--离散型特征编码方式:LabelEncoder、one-hot与哑变量*
  4. Facebook怎样开发软件:工程师驱动的文化
  5. 13家电脑品牌来源大揭底
  6. 生意参谋指数之指数推理原值
  7. PaddleOCR+OpenCV实现中英文识别
  8. Windows Sockets错误码方案
  9. raid5加热备盘_Raid5、热备盘原理 | 与其焦虑
  10. linux tmp php文件怎么打开,tmp文件用什么打开
  11. [BZFZ友谊赛]火山喷发
  12. 劳动仲裁委员会的具体地址即(朝阳区酒仙桥南十里居28楼的具体路线)______转...
  13. MacOs在线重装系统教程
  14. MyBatis实现中间表关联查询
  15. 宅急送 项目第八天 JBPM工作流框架
  16. 批量付款到户接口,批量转账接口
  17. 如何驯服野生论文 | EndNote
  18. arm-linux内核中串口时钟频率及波特率的设置
  19. Web前端期末大作业-生鲜商城平台公司网站网页设计(HTML+CSS+JS)
  20. CAD如何导出JPG高清图片

热门文章

  1. qnx如何和Android传递数据,QNX操作系统下MDSL数据记录和转存的研究与实现
  2. 大学毕业标语计算机学院,大学生毕业横幅标语
  3. div地址跳转 vue_vue跳转页面的几种方法(推荐)
  4. 泰坦尼克生存概率预测
  5. 渗透测试方法论5---测试会话管理机制
  6. V神透露以太坊发展规划 未来十年,zkS将与区块链一样重要
  7. 开放创新平台,大公司的掘墓者
  8. 有效 QA 过程测量的 10 个基本指标
  9. 【云原生】第六篇--Dockerfile精讲及新型容器镜像构建技术
  10. 前缀、中缀、后缀的相互转换