scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
总结:
scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。
scheduleWithFixedDelay,是以上一个任务结束时开始计时,period时间过去后,立即执行。
重点:
两个方法以不同的时间点作为参考。
例子:
package com.yuankai.t1.thread; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; public class ScheduleExecutorServiceTest { public static void main(String[] args) {
ScheduleExecutorServiceTest test = new ScheduleExecutorServiceTest();
test.testWithFixedDelay();
} private ScheduledExecutorService executor; public ScheduleExecutorServiceTest() {
executor = Executors.newScheduledThreadPool(4);
} public void testAtFixedRate() {
executor.scheduleAtFixedRate(new Runnable() { public void run() {
System.out.println("====");
try {
Thread.sleep(10000);
System.out.println("执行完毕");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 1000, 3000, TimeUnit.MILLISECONDS);
} public void testWithFixedDelay() {
executor.scheduleWithFixedDelay(new Runnable() { public void run() {
System.out.println("====");
try {
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
}
/*
try {
Thread.sleep(10000);
System.out.println("执行完毕");
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
}
}, 1000, 3000, TimeUnit.MILLISECONDS);
} }
注意:
通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常。
scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别的更多相关文章
- 理解ScheduledExecutorService中scheduleAtFixedRate和scheduleWithFixedDelay的区别
scheduleAtFixedRate 每间隔一段时间执行,分为两种情况: 当前任务执行时间小于间隔时间,每次到点即执行: /** * 任务执行时间(8s)小于间隔时间(10s) */ public ...
- ScheduledThreadPoolExecutor线程池scheduleAtFixedRate和scheduleWithFixedDelay的区别
ScheduledFuture<?> result = executor.scheduleAtFixedRate(task,2, 5, TimeUnit.SECONDS); 在延迟2秒之后 ...
- Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)
package com.thread.test.thread; import java.util.Timer; import java.util.TimerTask; import java.util ...
- ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别
ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...
- 深入理解Java线程池:ScheduledThreadPoolExecutor
介绍 自JDK1.5开始,JDK提供了ScheduledThreadPoolExecutor类来支持周期性任务的调度.在这之前的实现需要依靠Timer和TimerTask或者其它第三方工具来完成.但T ...
- 和朱晔一起复习Java并发(一):线程池
和我之前的Spring系列文章一样,我们会以做一些Demo做实验的方式来复习一些知识点. 本文我们先从Java并发中最最常用的线程池开始. 从一个线程池实验开始 首先我们写一个方法来每秒一次定时输出线 ...
- Java并发包线程池之ScheduledThreadPoolExecutor
前言 它是一种可以安排在给定的延迟之后执行一次或周期性执行任务的ThreadPoolExecutor.因为它继承了ThreadPoolExecutor, 当然也具有处理普通Runnable.Calla ...
- Java 多线程:线程池
Java 多线程:线程池 作者:Grey 原文地址: 博客园:Java 多线程:线程池 CSDN:Java 多线程:线程池 工作原理 线程池内部是通过队列结合线程实现的,当我们利用线程池执行任务时: ...
- 线程池之 newScheduledThreadPool中scheduleAtFixedRate(四个参数)
转自:https://blog.csdn.net/weixin_35756522/article/details/81707276 说明:在处理消费数据的时候,统计tps,需要用一个线程监控来获得tp ...
随机推荐
- java开发150个建议
阅读目录 建议1:不要在常量和变量中出现易混淆的字母 建议2:莫让常量蜕变成变量 建议3:三元操作符的类型务必一致 建议4:避免带有变长参数的方法重载 建议5:别让null值和空值威胁到变长方法 建议 ...
- 看书记笔记 书名21天学C#
☆:为重点★:为科普△:注▲:术语 前言概述 ☆一门语言必须包括诸如异常处理,无用单元收集,可扩展数据类型以及代码安全性等特征☆C#特性:简单性,面向对象,模块性,灵活性,简明性 ☆C#面向对象的封装 ...
- Qt学习--信号与槽(多窗口的实现)
按照helloword的创建过程 创建一个新的项目(项目名:window) 之后进行多窗口的实现过程: (参考:http://www.qter.org/portal.php?mod=view& ...
- 安装grub到U盘分区,实现多系统引导
目录 1.分区工具及分区类型 1.1 显示分区表和分区信息 1.1.1 fdisk -l 1.1.2 gdisk -l 1.1.3 parted -l 1.2 常见分区类型 1.3 分区样例 1.3. ...
- 用turtle实现动态汉诺塔
代码如下: (此代码最多可支持七层) import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): ...
- redis(4.0.11)编译安装
一: redis数据库安装 系统环境:linux系统(centos/redhat):Red Hat Enterprise Linux Server release 6.8 (Santiago) red ...
- 15. Password auditing (密码审核 12个)
Aircrack是一套用于802.11a / b / g WEP和WPA破解的工具. 一旦收集到足够的加密数据包,它就会实现最有效的破解算法来恢复无线密钥.. 该套件包括十多个分离工具,包括airod ...
- linux下的c程序排版工具:indent
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010839382/article/details/30779523 Linux下有一个方便的c语言 ...
- rs232接口定义
- 通过企业微信API接口发送消息
最近给公司测试组内部开发一个记账小工具,当账目出现问题的时候需要发送消息通知大家,前期主要采用的QQ发送通知消息,但是有一天突然无法连接到QQ服务器,运维的同学建议采用微信的方式对接然后进行告警,所以 ...