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 之前,你需要知道如何创建 ...
随机推荐
- 谈谈BUG严重级别(severity)管理
在软件工程理论中,BUG严重级别(severity)是用于指示软件质量问题导致的负面影响的程度.但在大部分实际的软件开发组织中,对BUG严重级别(severity)的定义和使用常常充斥着大量的争议和分 ...
- node.js 模拟自动发送邮件验证码
node.js 模拟自动发送邮件验证码 引言 正文 1. QQ邮箱设置 2. 安装nodemailer 3.配置信息 4.综合 5.讲解 结束语 引言 先点赞,再看博客,顺手可以点个关注. 微信公众号 ...
- “程序包com.sun.org.apache.xml.internal.security.utils不存在”的问题
方法一(eclipse): 网上大神的回答: 自己写的程序是不建议用com.sun这个玩意儿的..这东西属于“Deprecated and restricted API”.. 而且各种com.sun的 ...
- 「查缺补漏」巩固你的RocketMQ知识体系
Windows安装部署 下载 地址:[https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.2/rocketmq-all-4.5.2-bin- ...
- Jmeter 常用函数(1)- 详解 __Random
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 产生一个随机数 语法格式 ${__Ra ...
- Jmeter 常用函数(10)- 详解 __threadNum
如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 返回当前线程组产生的线程的线程编号 语 ...
- 你真的了解 get 和 post 的区别么
get 和 post 是两种最常用的 HTTP 请求方法,要说它们两个的区别,相必接触过 WEB 开发的人都能够说出一二. 如果我问你这个问题,你的内心充满了自信和喜悦.你可能已经写过无数个 GET ...
- Open MPI 4.0 编译安装
电脑上目前使用的mpi环境是2.1.1版本的openmpi,是我之前直接使用系统的包管理工具安装的.但是系统包版本一般都比较老旧,现在openmpi最新版已经出到了4.0,即将出4.1了,所以我打算升 ...
- 结对项目 实现自动生成四则运算题目的程序 (C++)
本次作业由 陈余 与 郭奕材 结对完成 零.github地址: https://github.com/King-Authur/-Automatically-generate-four-arithmet ...
- 牛客网PAT练兵场-福尔摩斯的约会
题目地址:https://www.nowcoder.com/pat/6/problem/4040 题意:模拟题,循环找相同的字母,但是注意题目的坑 /** * *作者:Ycute *时间:2019-1 ...