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 ...
随机推荐
- flutter 添加插件
打開pubspec.yaml ---> cupertino_icons 下添加插件
- SQLI DUMB SERIES-15
(1)无论输入什么都没有回显,但是输入 admin'# 时会显示成功登录,说明闭合方式是单引号 (2)无回显,则使用盲注,可用用时间延迟攻击法. 测试时间延迟是否可行: uname=admin' an ...
- CSS选择器中的特殊性
我们来看一下一个简单的例子: <!DOCTYPE html><html lang="en"><head> <meta charset=&q ...
- java-15习题
通过键盘分别输入年份.月份.日把它存储到日期时间对象中,然后计算1000天以后的日期并输出. import java.util.Calendar; import java.util.Scanner; ...
- C语言简单计算一元二次方程
#include <stdio.h> #include <math.h> /*计算一元二次方程的根*/ void Cal(double a,double b,double c) ...
- 【步步为营 Entity Framework+Reporting service开发】-(2) Code Fir
也许有人问,为什么要用EF创建爱你数据表,code first好处是什么? 使用EF创建数据库/表,只需要设计简单的C#类,再表内容变化的时候他会自动更新数据库结构,并且保留原有数据. EF很强大,支 ...
- Redis事务和实现秒杀功能的实现
今天带着学生学习了Redis的事务功能,Redis的事务与传统的关系型数据库(如MySQL)有所不同,Redis的事务不能回滚. Redis中使用multi.exec.discard.watch.un ...
- React Native开发的一种代码规范:Eslint + FlowType
[这篇随笔记录的很简单,没有涉及具体的Eslint规则解释以及FlowType的类型说明和使用等,只是链接了所需的若干文档] js开发很舒服,但是代码一多起来就参差不齐,难以阅读了.所以加上一些代码规 ...
- Kali Linux系统的安装、配置、使用
这个随便写的,随便看看就好,主要给讲一下安装过程 这里因为我物理机装的本来就是kali.所以懒得重装了,直接拿虚拟机演示一下 物理机安装kali的话,推荐使用rufus使用dd模式刻盘,不会造成之后的 ...
- oracle工具sqluldr2和sqlldr的使用
在 Oracle 数据库中,我们通常在不同数据库的表间记录进行复制或迁移时会用以下几种方法:1. 逐条insert -- 只适用少量数据更新 ALTER TABLE order_items DISAB ...