scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别和实例
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 的区别和实例相关推荐
- JAVA定时器ScheduledExecutorService中,scheduleAtFixedRate和scheduleWithFixedDelay的区别
我们通过运行代码来查看两者之间的区别: 1.首先创建一个需要执行5秒的任务: // 执行这个任务需要5秒 Runnable runnable = new Runnable() {public void ...
- 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
scheduleAtFixedRate:是以period为间隔来执行任务的,如果任务执行时间小于period,则上次任务执行完成后会间隔period后再去执行下一次任务:但如果任务执行时间大于peri ...
- scheduleAtFixedRate和scheduleWithFixedDelay的区别
1)scheduleAtFixedRate:可以传入runnable,定制第一次的初始化执行时间,周期时间,单位时间-------创建并执行一个周期性任务,过了给定的初始延迟时间(1min执行一次or ...
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
总结: scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完 ...
- ScheduledExecutorService的简单使用、scheduleAtFixedRate和scheduleWithFixedDelay区别
ScheduledExecutorService的简单使用.scheduleAtFixedRate和scheduleWithFixedDelay区别 ScheduledExecutorService ...
- scheduleAtFixedRate和scheduleWithFixedDelay 区别
https://www.jianshu.com/p/2900b4fd3bdd Executors提供的线程池ScheduledExecutorService中有两个方法,scheduleAtFixed ...
- 详解scheduleAtFixedRate与scheduleWithFixedDelay原理
前言 前几天,肥佬分享了一篇关于定时器的文章你真的会使用定时器吗?,从使用角度为我们详细地说明了定时器的用法,包括 fixedDelay.fixedRate,为什么会有这样的区别呢?下面我们从源码角度 ...
- i++与++i的区别,使用实例说明
/*** 类名:TEST.java<br>* <p>* 功能:i++与++i的区别,使用实例说明* </p>* * @Author:<a href=" ...
- php class中public,private,protected的区别以及实例分析
本篇文章是对php class中public,private,protected的区别以及实例进行了详细的分析介绍,需要的朋友参考下 一,public,private,protected的区别 pub ...
最新文章
- python 比较文件不同,在python中逐行比较两个不同的文件
- Thread.currentThread().getContextClassLoader()和Class.getClassLoader()区别
- 系统学习机器学习之特征工程(二)--离散型特征编码方式:LabelEncoder、one-hot与哑变量*
- Facebook怎样开发软件:工程师驱动的文化
- 13家电脑品牌来源大揭底
- 生意参谋指数之指数推理原值
- PaddleOCR+OpenCV实现中英文识别
- Windows Sockets错误码方案
- raid5加热备盘_Raid5、热备盘原理 | 与其焦虑
- linux tmp php文件怎么打开,tmp文件用什么打开
- [BZFZ友谊赛]火山喷发
- 劳动仲裁委员会的具体地址即(朝阳区酒仙桥南十里居28楼的具体路线)______转...
- MacOs在线重装系统教程
- MyBatis实现中间表关联查询
- 宅急送 项目第八天 JBPM工作流框架
- 批量付款到户接口,批量转账接口
- 如何驯服野生论文 | EndNote
- arm-linux内核中串口时钟频率及波特率的设置
- Web前端期末大作业-生鲜商城平台公司网站网页设计(HTML+CSS+JS)
- CAD如何导出JPG高清图片