scheduleAtFixedRate :每隔Xs执行任务

scheduleWithFixedDelay :上轮任务结束后的Xs后执行下次任务

如下是测试代码,就是at和with方法不同

public static void atFixed() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(3);
long time = System.currentTimeMillis();
for( int i=0;i<1;i++) {
final int j=i;
Integer[] is = {1, 2, 3, 4, 5, 6, 7, 8, 9};
List<Integer> list = Arrays.asList(is);
Iterator<Integer> it = list.iterator();
service.scheduleAtFixedRate(new Runnable() { int k=j;
@Override
public void run() {
if (it.hasNext()) {
System.out.println(k+"*****"+it.next() + "******" +this.hashCode()+"*****"+ (System.currentTimeMillis() - time));
try {
//Thread.sleep(1000L);
Thread.sleep(50L);
} catch (InterruptedException e) {
e.printStackTrace();
} } else {
service.shutdown();
}
// System.out.println(k+"无动作" + this.hashCode());
}
}, 100, 100, TimeUnit.MILLISECONDS);
}
}
scheduleAtFixedRate的执行结果
0*****1******1949088124*****102
0*****2******1949088124*****203
0*****3******1949088124*****302
0*****4******1949088124*****403
0*****5******1949088124*****502
0*****6******1949088124*****602
0*****7******1949088124*****703
0*****8******1949088124*****804
0*****9******1949088124*****903
scheduleWithFixedDelay的执行结果
0*****1******732719140*****104
0*****2******732719140*****258
0*****3******732719140*****410
0*****4******732719140*****578
0*****5******732719140*****731
0*****6******732719140*****895
0*****7******732719140*****1048
0*****8******732719140*****1199
0*****9******732719140*****1352

但是如果执行的时间超过了等待时间的话,例如我把上面的sleep时间增加到500ms,

scheduleAtFixedRate的执行结果
0*****1******732719140*****102
0*****2******732719140*****602
0*****3******732719140*****1103
0*****4******732719140*****1604
0*****5******732719140*****2104
0*****6******732719140*****2604
0*****7******732719140*****3105
0*****8******732719140*****3606
0*****9******732719140*****4106
scheduleWithFixedDelay的执行结果
0*****1******602404570*****103
0*****2******602404570*****705
0*****3******602404570*****1308
0*****4******602404570*****1912
0*****5******602404570*****2515
0*****6******602404570*****3116
0*****7******602404570*****3717
0*****8******602404570*****4320
0*****9******602404570*****4921

修改几个参数

public static void atFixed() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
long time = System.currentTimeMillis();
for( int i=0;i<6;i++) {
final int j=i;
Integer[] is = {1, 2, 3, 4,};
List<Integer> list = Arrays.asList(is);
Iterator<Integer> it = list.iterator();
service.scheduleWithFixedDelay(new Runnable() { int k=j;
@Override
public void run() {
if (it.hasNext()) {
System.out.println(k+"*****"+it.next() + "******" +this.hashCode()+"*****"+ (System.currentTimeMillis() - time));
try {
//Thread.sleep(1000L);
Thread.sleep(50L);
} catch (InterruptedException e) {
e.printStackTrace();
} } else {
service.shutdown();
}
// System.out.println(k+"无动作" + this.hashCode());
}
}, 0, 1000, TimeUnit.MILLISECONDS);
}
}
scheduleAtFixedRate运行结果

0*****1******2062239825*****2
1*****1******874924232*****2
2*****1******987865148*****53
3*****1******1949088124*****53
4*****1******1054745528*****104
5*****1******1880302369*****104
0*****2******2062239825*****1003
1*****2******874924232*****1003
2*****2******987865148*****1054
3*****2******1949088124*****1054
4*****2******1054745528*****1105
5*****2******1880302369*****1105
0*****3******2062239825*****2004
1*****3******874924232*****2004
2*****3******987865148*****2055
3*****3******1949088124*****2055
5*****3******1880302369*****2106
4*****3******1054745528*****2106
0*****4******2062239825*****3003
1*****4******874924232*****3003
2*****4******987865148*****3054
3*****4******1949088124*****3054
4*****4******1054745528*****3105
5*****4******1880302369*****3105

scheduleWithFixedDelay的运行结果
1*****1******874924232*****2
0*****1******2062239825*****2
2*****1******987865148*****53
3*****1******1949088124*****53
4*****1******1880302369*****104
5*****1******1054745528*****104
1*****2******874924232*****1055
0*****2******2062239825*****1055
2*****2******987865148*****1106
3*****2******1949088124*****1108
5*****2******1054745528*****1157
4*****2******1880302369*****1159
0*****3******2062239825*****2108
1*****3******874924232*****2109
2*****3******987865148*****2159
3*****3******1949088124*****2160
5*****3******1054745528*****2210
4*****3******1880302369*****2211
0*****4******2062239825*****3159
1*****4******874924232*****3160
2*****4******987865148*****3210
3*****4******1949088124*****3211
5*****4******1054745528*****3261
4*****4******1880302369*****3262

ScheduledExecutorService定时任务学习的更多相关文章

  1. Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar

    web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...

  2. Quartz定时任务学习(二)web应用

    web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...

  3. Quartz定时任务学习(一)简单任务

    学习quartz首先了解三个概念: 调度器:负责调度作业和触发器: 触发器:设置作业执行的时间.参数.条件等:(简单触发器和Cron触发器) 作业:定时任务内容,被执行的程序: 下载必要的jar包,直 ...

  4. Quartz定时任务学习(五)触发器

    顾名思义,Trigger(触发器)的责任就是触发一个 Job 去执行.当用 Scheduler 注册一个 Job 的时候要创建一个 Trigger 与这个 Job 相关联.Quartz 提供了四种类型 ...

  5. Quartz3.0定时任务学习之异步调度器

    前言 Quartz3与Quartz2的主要区别有两点: 1,Quartz3将它的资源类库拆的更细了,比如,想使用Quartz3开发,最少要引用Quartz,Quartz.Jobs,Quartz.Plu ...

  6. Quartz定时任务学习(九)Quartz监听器

    Quartz 提供了三种类型的监听器:监听 Job 的,监听 Trigger 的,和监听 Scheduler 自已的. 本章解释如何应用每一种类型来更好的管理你的 Quartz 应用,并获悉到什么事件 ...

  7. Quartz定时任务学习(七)Cron 触发器

    Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 位置 时间域名 允许值 允许的特殊字符 1 秒 0-59 ...

  8. Quartz定时任务学习(六)作业

    org.quartz.Job 接口 把 Quartz 作用到 Java 类上唯一要做的就是让它实现 org.quartz.Job 接口.你的 Job 类可以实现任何其他想要的接口或继承任何需要的基类, ...

  9. Quartz定时任务学习(四)调度器

    org.quartz.Scheduler 类层次 作为一个 Quartz 用户,你要与实现了 org.quartz.Scheduler 接口的类交互.在你调用它的任何 API 之前,你需要知道如何创建 ...

随机推荐

  1. int ,long , long long , __int64类型的范围

    首先见测试代码(在g++/gcc下运行): #include<iostream> using namespace std; int main() { cout<<sizeof( ...

  2. 7. oracle表的管理*

    一.表名和列名的命名规则: 1.必须以字母开头 2.长度不能超过30个字符 3.不能使用oracle的保留字 4.只能使用如下字符 A-Z,a-z,0-9,$,#等 二.Oracle数据类型1.字符类 ...

  3. 2020.5.24 第四篇 Scrum冲刺博客

    Team:银河超级无敌舰队 Project:招新通 项目冲刺集合贴:链接 目录 一.每日站立会议 1.1 会议照片 1.2 项目完成情况 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3. ...

  4. 0基础掌握接口测试神器-Postman

    一:Postman环境搭建 1:postman是什么?Postman是一款功能强大的网页调试与发送网页HTTP请求的接口测试工具.2:postman有几种安装方式?两种,应用程序和浏览器插件 3:po ...

  5. 解Bug之路-dubbo应用无法重连zookeeper

    前言 dubbo是一个成熟且被广泛运用的框架.饶是如此,在某些极端条件下基于dubbo的应用还会出现无法重连zookeeper的问题.由于此问题容易导致比较大的故障,所以笔者费了一番功夫去定位,现将排 ...

  6. log4j升级到log4j2

    log4j升级到log4j2 1.导入依赖 log4j2应尽量使用同一版本,否则可能出现不兼容的情况 <!-- log4j2 start --> <!-- log4j-1.2-api ...

  7. 【译】Introducing “Web Live Preview”

    如果你开发的应用有 UI,你可能经历开发->编译->测试->修改->编译->测试的循环.根据所使用的框架或技术,有些可以改善这一流程,比如 edit-and-contin ...

  8. [PKUWC2018]Minimax 题解

    根据题意,若一个点有子节点,则给出权值:否则可以从子节点转移得来. 若没有子节点,则直接给出权值: 若只有一个子节点,则概率情况与该子节点完全相同: 若有两个子节点,则需要从两个子节点中进行转移. 如 ...

  9. 特性预览:Apache 顶级项目 Apache Pulsar 2.6.1 版本

    在正式分享 2.6.1 版本更新细节之前,冉小龙首先为我们分享了两个相关 PIP 的内容. 一个是 PIP-47 中关于「基于时间来进行版本更新」的计划.该 PIP 提出后,从 2.5.0 版本到目前 ...

  10. 给IE9及其以下等不支持classList属性的浏览器,添加classList属性

    // 解决IE9及其以下 不支持classList属性的问题 if (!("classList" in document.documentElement)) { Object.de ...