ScheduledExecutorService的简单使用、scheduleAtFixedRate和scheduleWithFixedDelay区别
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区别相关推荐
- scheduleAtFixedRate和scheduleWithFixedDelay 区别
https://www.jianshu.com/p/2900b4fd3bdd Executors提供的线程池ScheduledExecutorService中有两个方法,scheduleAtFixed ...
- scheduleAtFixedRate和scheduleWithFixedDelay区别
scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需 ...
- 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
scheduleAtFixedRate:是以period为间隔来执行任务的,如果任务执行时间小于period,则上次任务执行完成后会间隔period后再去执行下一次任务:但如果任务执行时间大于peri ...
- 详解scheduleAtFixedRate与scheduleWithFixedDelay原理
前言 前几天,肥佬分享了一篇关于定时器的文章你真的会使用定时器吗?,从使用角度为我们详细地说明了定时器的用法,包括 fixedDelay.fixedRate,为什么会有这样的区别呢?下面我们从源码角度 ...
- 写段代码理解 scheduleAtFixedRate和scheduleWithFixedDelay
① 如果任务在周期内正常结束掉的话scheduleAtFixedRate和scheduleWithFixedDelay没有差别 public void start() {ScheduledExecut ...
- Java定时线程实现:scheduleAtFixedRate 和 scheduleWithFixedDelay 的差别
Java实现定时任务,一般都是用一个线程,设置个时间,让他定时执行,注意力一般都是集中在这个线程的实现,很少考虑到具体定时执行线程的这个过程.scheduleAtFixedRate 和 schedul ...
- newScheduledThreadPool : scheduleAtFixedRate 与 scheduleWithFixedDelay 详解
一.引言 newScheduledThreadPool 周期性线程池提供了周期执行任务的方法 scheduleAtFixedRate 与 scheduleWithFixedDelay,两者比较容易混淆 ...
- Java并发编程—schedule方法和scheduleAtFixedRate方法的区别
原文作者:一叶丿清风 原文地址:schedule方法和scheduleAtFixedRate方法的区别 schedule方法和scheduleAtFixedRate方法都可以实现任务的延时和不延时执行 ...
- 深入源码,CompletableFuture 简单与链式的区别?
导读:从 JDK 8 开始,在 Concurrent 包中提供了一个强大的异步编程工具 CompletableFuture.在 JDK8 之前,异步编程可以通过线程池和 Future 来实现,但功能还 ...
最新文章
- ie不支持getElementsByName的解决办法
- 【错误记录】360 加固后的运行错误 ( 加固 SO 动态库时不能对第三方动态库进行加固 )
- 神策数据汽车行业解决方案重磅上线,全面赋能车企数字化转型
- 【深度学习】从零开始 Mask RCNN 实战:基于 Win10 + Anaconda 的 Mask RCNN 环境搭建
- ASP.NET Button控件的UseSubmitBehavior属性引发的血案
- 外媒评出中国最美20个景点
- Android操作HTTP实现与服务器通信
- 对《RHCSA/RHCE Red Hat Linux认证学习指南(第6版):EX200 EX300》的评价
- 面试准备——Java回顾:基础编程(基本语法、面向对象、异常处理)
- python访问oracle_用Python操作Oracle
- 全国航空机场分布矢量数据/旅游景点poi/全国港口码头分布/地铁站分布/火车站分布/2020年POI矢量数据
- sharding技术
- 拯救者Y7000拆机清灰方法及加装机械硬盘
- sql重启oracle数据库,oracle重启数据库sql
- win10升级更新2004版卡在49%解决办法
- Codeforces Beta Round #14 (Div. 2) E. Camels
- 三.数 据 链 路 层
- EBS 分配指定快码维护权限
- less和more的区别
- Shopkick:从eBay、Amazon和Groupon当中杀出一条血路的移动购物应用
热门文章
- 【NLP】从双曲面到双曲几何庞加莱盘
- 2015合肥计算机专场招聘会,我院成功举办2015年计算机类、数学类就业实习专场招聘会...
- JavaSE-多线程(二)
- 程序员语录---温馨小家庭
- ubuntu查看内存占用情况的简单方法
- UNIBO大学博物馆网络设计—品牌重塑和数字产品设计
- 揪心!4岁幼童模仿动画片,撑伞从26楼跳下!
- 弯曲圆波导matlab_一种新型的90°弯曲圆波导TE01-TM11模式变换器的设计
- python零基础到实践——列表
- android内存卡测试,手机内存卡各种传输/跑分实测_手机_手机Android频道-中关村在线...