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. Antd cracoTs Js 配置流程

    JS:文档:0.1.4 配置 js 环境.note链接:http://note.youdao.com/noteshare?id=e32fa75c1baa014b5819fa5e22887dbc& ...

  2. Project ACRN documentation

    Project ACRN documentation https://projectacrn.github.io/latest/index.html Virtio devices high-level ...

  3. 通过索引优化sql

    sql语句的优化最重要的一点就是要合理使用索引,下面介绍一下使用索引的一些原则: 1.最左前缀匹配原则.mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹 ...

  4. “路由大当家”OSPF的小秘密

    引入 OPSF是应用最广的路由协议,基本上,所有的IGP用到的都是OSPF,下面我们看看它的“小秘密” 优点: •没有跳数限制 •使用组播更新变化的路由和网络信息 •路由收敛速度较快 •以开销(Cos ...

  5. springboot-swagger配置

    原文地址 https://www.cnblogs.com/softidea/p/6251249.html https://www.cnblogs.com/xiebq/p/9181517.html 1p ...

  6. 接口测试中GET方法的获取

    今天在这里给大家介绍一下get方法,其实这些方法大家可以看一下源码里面的介绍只需要在代码中输入: import requests help(requests) 就可以看到带有示例的解释: 现在我们来完 ...

  7. MSP430-LED中断闪烁代码详解

    使用MSP430F149的开发板,首先对LED闪烁灯的例程进行讲解,然后下边是自己写的,将部分代码写入了新建的led.c程序中 #include  <msp430x14x.h>       ...

  8. 沈阳做假证z

    沈阳做假证[电/薇:187ヘ1184ヘ0909同号]办各类证件-办毕业证-办离婚证,办学位证书,办硕士毕业证,办理文凭学历,办资格证,办房产证不. 这是一个简单的取最大值程序,可以用于处理 i32 数 ...

  9. 集成react-native-image-picker时,报错Couldn't get file path for photo

    1. 版本环境: "react": "16.13.1", "react-native": "0.63.2", " ...

  10. css动画是否会被js阻塞

    css动画是否会被js阻塞 css的动画部分是会被js阻塞的,不过transform的动画则不会受影响. 下面举一个margin-left移动的动画下,启动js阻塞动画的性能图表 <style& ...