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. IntelliJ IDEA中如何再次调出springboot的依赖窗口

    原文链接:https://blog.csdn.net/qq_38138069/article/details/102528587 IDEA中如何再次调出springboot的依赖窗口,随时可以根据喜好 ...

  2. 分布式数据库中间件 MyCat | 分库分表实践

    MyCat 简介 MyCat 是一个功能强大的分布式数据库中间件,是一个实现了 MySQL 协议的 Server,前端人员可以把它看做是一个数据库代理中间件,用 MySQL 客户端工具和命令行访问:而 ...

  3. DML语言(数据操纵语言)

    #DML语言/*数据操作语言:插入:insert修改:update删除:delete */ #一.插入语句#方式一:经典的插入/*语法:insert into 表名(列名,...) values(值1 ...

  4. Dubbo系列之 (五)服务订阅(2)

    辅助链接 Dubbo系列之 (一)SPI扩展 Dubbo系列之 (二)Registry注册中心-注册(1) Dubbo系列之 (三)Registry注册中心-注册(2) Dubbo系列之 (四)服务订 ...

  5. layui上传同一张图片第二次时choose没有反应

    将上传文件的input的val设置为空 $("#test11").parent().find("input").val('');

  6. kubeadm安装kubernetes(v18.8.8)

    1. 前言 kubernetes版本更新迭代非常快,上一篇写kubernetes搭建时,版本还是v1.15.0,现在已经更新到v1.18.看kubernetes在github的官方仓库,8月14日小版 ...

  7. Vue项目——Supermall移动端购物商城

    一.项目描述 基于Vue全家桶构建的移动端购物商城APP.页面一共分为:首页.详情页.分类页.购物车页面.登录页面和个人信息页面. 二.使用技术 使用Vue CLI3快速搭建Vue开发环境以及对应的w ...

  8. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  9. 模拟退火详解&P1433题解

    前排提示:LZ是个菜比,有可能有讲的不对的地方,请在评论区指出qwq 0.基本思想 模拟退火其实没有那么高大上.说白了就是初始化一个"温度".每次随机乱选一个方案,如果比以前的方案 ...

  10. VUE+ElementUI创建项目

    1.官网下载node,安装node.js环境 安装完成后进入cmd,输入node -v和npm -v查看node和npm是否安装成功及对应的版本 2.全局安装vue-cli:cnpm install ...