理解ScheduledExecutorService中scheduleAtFixedRate和scheduleWithFixedDelay的区别
scheduleAtFixedRate
每间隔一段时间执行,分为两种情况:
当前任务执行时间小于间隔时间,每次到点即执行;
/**
* 任务执行时间(8s)小于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleAtFixedRate: " + new Date());
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleAtFixedRate: " + new Date());
}
}, 0, 10 , SECONDS);
}
} output: Start: scheduleAtFixedRate: Sun Apr 28 14:36:00 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:36:08 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:36:10 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:36:18 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:36:20 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:36:28 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:36:30 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:36:38 CST 2019
... 程序启动时间是14:36:00,以后每间隔10s执行一次(即14:36:10、14:36:20、14:36:30等)。
当前任务执行时间大于等于间隔时间,任务执行后立即执行下一次任务。相当于连续执行了。
/**
* 任务执行时间(12s)大于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleAtFixedRate: " + new Date());
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleAtFixedRate: " + new Date());
}
}, 0, 10 , SECONDS);
}
} output: Start: scheduleAtFixedRate: Sun Apr 28 14:30:13 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:30:25 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:30:25 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:30:37 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:30:37 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:30:49 CST 2019
Start: scheduleAtFixedRate: Sun Apr 28 14:30:49 CST 2019
End : scheduleAtFixedRate: Sun Apr 28 14:31:01 CST 2019 程序启动时间是14:30:13,按理说应该每间隔10s执行一次(即14:30:23、14:30:33等),但由于任务执行时间长于10s,下一次的任务要开始的时候发现上次的任务还没有完成,因此阻塞等待,一旦发现上次的任务完成,就马上启动。表现出来就是任务延时启动,最终的效果就是连续执行。
scheduleWithFixedDelay
每当上次任务执行完毕后,间隔一段时间执行。不管当前任务执行时间大于、等于还是小于间隔时间,执行效果都是一样的。
/**
* 任务执行时间(8s)小于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public static void main(String[] args) {
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleWithFixedDelay: " + new Date());
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleWithFixedDelay: " + new Date());
}
}, 0, 10 , SECONDS);
}
}
output:
Start: scheduleWithFixedDelay: Sun Apr 28 14:27:59 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:07 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:17 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:25 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:35 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:43 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:53 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:29:01 CST 2019
...
可以看出每个End后,等待了10秒,才启动下一次Start。
/**
* 任务执行时间(12s)大于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public static void main(String[] args) {
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleWithFixedDelay: " + new Date());
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleWithFixedDelay: " + new Date());
}
}, 0, 10 , SECONDS);
}
}
output:
Start: scheduleWithFixedDelay: Sun Apr 28 14:26:29 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:26:41 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:26:51 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:03 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:27:13 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:25 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:27:35 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:47 CST 2019
...
可以看出每个End后,等待了10秒,才启动下一次Start。
参考
scheduleAtFixedRate vs scheduleWithFixedDelay
扩展
Spring定时任务@Scheduled注解使用方式浅窥(cron表达式、fixedRate和fixedDelay)
理解ScheduledExecutorService中scheduleAtFixedRate和scheduleWithFixedDelay的区别的更多相关文章
- ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别
ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
总结: scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完 ...
- 深入理解css3中nth-child和 nth-of-type的区别
在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...
- 深入理解css3中 nth-child 和 nth-of-type 的区别
在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...
- ScheduledThreadPoolExecutor线程池scheduleAtFixedRate和scheduleWithFixedDelay的区别
ScheduledFuture<?> result = executor.scheduleAtFixedRate(task,2, 5, TimeUnit.SECONDS); 在延迟2秒之后 ...
- IOS基础:深入理解Objective-c中@class 和#import的区别
在面向对象objective-C语言中,当一个类使用到另一个类时,并且在类的头文件中需要创建被引用的指针时,可以#import方式引入,通过@class引入: 这两种的方式的区别在于: 1.运用#im ...
- 理解js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性__proto ...
- 【转】为什么我们都理解错了HTTP中GET与POST的区别
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...
随机推荐
- bootstrap模态对话框(最简单)
根据公司的需求,需要一个对话框来返回给客户的失败原因,刚刚开在百度上搜了老半天,嫩是没有搜索一个自己想要的,后来发送私信给一个博友,经过他哪里找到了自己想要的答案,废话不多说直接看源码: <!D ...
- CLR、程序集、反射和控制反转
以前面试包括自己学习的时候经常会碰到这3个东西,也查过相关介绍,晦涩难懂,虽然看完之后,当时勉强理解,不过过一段时间又忘了.其实这篇文章可以分两篇(clr.程序集)和(反射.控制反转)来写,但它们之间 ...
- 第一章——机器学习总览(The Machine Learning Landscape)
本章介绍了机器学习的一些基本概念,已经应用场景.这部分知识在其它地方也经常看到,不再赘述. 这里只记录一些作者提到的,有趣的知识点. 回归(regression)名字的来源:这是由Francis Ga ...
- .NET Orm 性能测试
.NET Orm 性能测试 简介 OrmBenchmark 这个项目主要是为了测试主要的Orm对于 SqlServer 数据库的查询并将数据转换成所需 POCO 对象的耗时情况(好吧,实际上不完全or ...
- web前端 DOM 详解
先来点概念 文档对象模型(DOM)是一个独立于语言的,使用 XML 和 HTML 文档操作的应用程序接口(API). 在浏览器中,主要与 HTML 文档打交道,在网页应用中检索 XML 文档也很常见. ...
- python中RabbitMQ的使用(安装和简单教程)
1,简介 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从"生产者"接收消息 ...
- poj-3522 最小生成树
Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...
- 在tomcat下部署两个或多个项目时 log4j和web.xml配置webAppRootKey 的问题(转)
在tomcat下部署两个或多个项目时 web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为"webapp.root",如下: <!-- 应用路径 ...
- procotol.go 源码阅读
) return } bufferOver = buffer[i:] return } //整形转换成字节 // func IntToBytes(n int) ...
- create_volume.go
package api import ( "net/http" "io/ioutil" "errors" & ...