ScheduledExecutorService定时任务学习
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定时任务学习的更多相关文章
- Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Quartz定时任务学习(二)web应用
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Quartz定时任务学习(一)简单任务
学习quartz首先了解三个概念: 调度器:负责调度作业和触发器: 触发器:设置作业执行的时间.参数.条件等:(简单触发器和Cron触发器) 作业:定时任务内容,被执行的程序: 下载必要的jar包,直 ...
- Quartz定时任务学习(五)触发器
顾名思义,Trigger(触发器)的责任就是触发一个 Job 去执行.当用 Scheduler 注册一个 Job 的时候要创建一个 Trigger 与这个 Job 相关联.Quartz 提供了四种类型 ...
- Quartz3.0定时任务学习之异步调度器
前言 Quartz3与Quartz2的主要区别有两点: 1,Quartz3将它的资源类库拆的更细了,比如,想使用Quartz3开发,最少要引用Quartz,Quartz.Jobs,Quartz.Plu ...
- Quartz定时任务学习(九)Quartz监听器
Quartz 提供了三种类型的监听器:监听 Job 的,监听 Trigger 的,和监听 Scheduler 自已的. 本章解释如何应用每一种类型来更好的管理你的 Quartz 应用,并获悉到什么事件 ...
- Quartz定时任务学习(七)Cron 触发器
Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 位置 时间域名 允许值 允许的特殊字符 1 秒 0-59 ...
- Quartz定时任务学习(六)作业
org.quartz.Job 接口 把 Quartz 作用到 Java 类上唯一要做的就是让它实现 org.quartz.Job 接口.你的 Job 类可以实现任何其他想要的接口或继承任何需要的基类, ...
- Quartz定时任务学习(四)调度器
org.quartz.Scheduler 类层次 作为一个 Quartz 用户,你要与实现了 org.quartz.Scheduler 接口的类交互.在你调用它的任何 API 之前,你需要知道如何创建 ...
随机推荐
- 非对称加密与HTTPS(转)
序:HTTPS更安全,为什么? 因为HTTP协议本身毫无安全性可言. 当你访问一个纯HTTP的网站(以及与这个网站有任何网络交互)时,你发出去一个请求.在这个请求到达网站服务器的路途上,不管是你家的路 ...
- CentOS7 安装图形化桌面
1.装好CentOS7后,我们一开始是上不了网的 (ping 百度报错:Name or service not know) 2.输入dhclient,可以自动获取一个IP地址,再用命令ip addr查 ...
- autotools使用
autotools制作makefile 下面以hello.c来说明生成makefile的过程. 基本步骤如下: 1)autoscan命令生成configure.scan文件,重命名configure. ...
- Go interface 操作示例
原文链接:Go interface操作示例 特点: 1. interface 是一种类型 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为.go 允许不带任何方 ...
- 结对项目:四则运算(C语言)
github地址:https://github.com/nilonger/arithmetic 结对伙伴:杨锐龙+黄海钊 一.项目要求 1.1 题目:实现一个自动生成小学四则运算题目的命令行程序(也可 ...
- IntelliJ IDEA下git版本回退,版本还原
原文链接:https://blog.csdn.net/hehyyoulan/article/details/80005272
- React其它相关知识点
React其它相关知识点 一,解释一下React Fiber? 简单来说,核心就是在虚拟dom和浏览器的调用栈之间多了一个虚拟调用栈,和虚拟dom一样,这个虚拟调用栈也是在内存中的,这个虚拟调用栈就类 ...
- 理解Word2Vec
一.简介 Word2vec 是 Word Embedding 的方法之一,属于NLP 领域.它是将词转化为「可计算」「结构化」的向量的过程.它是 2013 年由谷歌的 Mikolov 提出了一套新的词 ...
- LinuxIP配置方法
一.双网卡双IP. eth0为电信,eth1为联通. # cd /etc/sysconfig/network-scripts/ # vi ifcfg-eth0 DEVICE=eth0 HWADDR=0 ...
- 地图_SDK
不仅仅是Google,您必须知道的全球十大地图API https://blog.csdn.net/u013068887/article/details/79322096