scheduleAtFixedRate

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

scheduleWithFixedDelay

以上一个任务***结束的时间***开始计时,period时间过去后,立即执行。
重点:
两个方法以不同的时间点作为参考。
注意:
通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常

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);//此地方可以试下把try catch去掉,任务就会停掉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);}}

彻底理解scheduleAtFixedRate 与 scheduleWithFixedDelay相关推荐

  1. 写段代码理解 scheduleAtFixedRate和scheduleWithFixedDelay

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

  2. scheduleAtFixedRate和scheduleWithFixedDelay 区别

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

  3. 详解scheduleAtFixedRate与scheduleWithFixedDelay原理

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

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

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

  5. newScheduledThreadPool : scheduleAtFixedRate 与 scheduleWithFixedDelay 详解

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

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

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

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

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

  8. scheduleAtFixedRate和scheduleWithFixedDelay

    来自: http://www.cnblogs.com/trust-freedom/p/6594270.html ScheduledExecutorService#scheduleAtFixedRate ...

  9. 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

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

最新文章

  1. MySQL导出数据反单引号_使用mysqldump导出数据时对字段中包含的单引号的处理
  2. [转]C++ 使用Makefile文件
  3. js new Date()不带时分秒时,时间变了 问题解决
  4. 要成为年薪五十万的数据分析师,除了技术还需要什么?
  5. 实验4.2 实现客户机(CLIENT)类
  6. 你必须要找到你所爱的东西
  7. 最新M1芯片的MacBook Pro打开软件闪退解决方法
  8. 【边尝试边写博客】入侵学校服务器
  9. 高等数学-不定积分基本公式
  10. Oracle数据库备份与恢复
  11. [树状数组模板] 洛谷P3368
  12. 洛谷每日三题之第六天
  13. 简易秒表Android源代码
  14. 电子护照阅读器现身出入境办证大厅
  15. 车载诊断系统-OBD
  16. 考研单词不用死记硬背,必备资料23考研英语朱伟恋恋有词资料硬,老师废话少
  17. 读书百客:《点绛唇·燕雁无心》鉴赏
  18. CentOS7下使用rpm安装RabbitMQ
  19. VS Test: No connection could be made because the target machine actively refused it 127.0.0.1:6910
  20. MDX基本概念和语法

热门文章

  1. 安装centos系统,搭建LAMP服务器并使其外网可见
  2. 不知道你们有没有充钱看小说,方正我没钱,所以我就把它爬下来了
  3. windows server2012安装telnet
  4. 现代农业展呈现科技创新“广阔天地”
  5. 如果黄光裕与刘强东联手对抗马云,什么结局?
  6. 《哈利·波特:霍格沃茨之谜》邀请玩家为魔法世界的圣诞节装饰大厅
  7. PG pg_stat_statements配置
  8. windows 下 mySQL 镜像安装文件下载
  9. php construct 返回值,php – 在类__construct()中间返回是否可以接受
  10. 大话Android自动化测试--金阳光自动化测试Android系列(1)--金阳光于2013年4月份