总结:

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 的区别的更多相关文章

  1. 理解ScheduledExecutorService中scheduleAtFixedRate和scheduleWithFixedDelay的区别

    scheduleAtFixedRate 每间隔一段时间执行,分为两种情况: 当前任务执行时间小于间隔时间,每次到点即执行: /** * 任务执行时间(8s)小于间隔时间(10s) */ public ...

  2. ScheduledThreadPoolExecutor线程池scheduleAtFixedRate和scheduleWithFixedDelay的区别

    ScheduledFuture<?> result = executor.scheduleAtFixedRate(task,2, 5, TimeUnit.SECONDS); 在延迟2秒之后 ...

  3. Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)

    package com.thread.test.thread; import java.util.Timer; import java.util.TimerTask; import java.util ...

  4. ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别

    ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...

  5. 深入理解Java线程池:ScheduledThreadPoolExecutor

    介绍 自JDK1.5开始,JDK提供了ScheduledThreadPoolExecutor类来支持周期性任务的调度.在这之前的实现需要依靠Timer和TimerTask或者其它第三方工具来完成.但T ...

  6. 和朱晔一起复习Java并发(一):线程池

    和我之前的Spring系列文章一样,我们会以做一些Demo做实验的方式来复习一些知识点. 本文我们先从Java并发中最最常用的线程池开始. 从一个线程池实验开始 首先我们写一个方法来每秒一次定时输出线 ...

  7. Java并发包线程池之ScheduledThreadPoolExecutor

    前言 它是一种可以安排在给定的延迟之后执行一次或周期性执行任务的ThreadPoolExecutor.因为它继承了ThreadPoolExecutor, 当然也具有处理普通Runnable.Calla ...

  8. Java 多线程:线程池

    Java 多线程:线程池 作者:Grey 原文地址: 博客园:Java 多线程:线程池 CSDN:Java 多线程:线程池 工作原理 线程池内部是通过队列结合线程实现的,当我们利用线程池执行任务时: ...

  9. 线程池之 newScheduledThreadPool中scheduleAtFixedRate(四个参数)

    转自:https://blog.csdn.net/weixin_35756522/article/details/81707276 说明:在处理消费数据的时候,统计tps,需要用一个线程监控来获得tp ...

随机推荐

  1. ngix请求转发

    实际运用中,当我们有对用户隐藏真实url的需求时,可以使用ngix转发. 1.转发所有请求: location / { proxy_pass http://localhost:8080 ; } 2.转 ...

  2. []TLD code run

    opencv3.3.1 https://github.com/alantrrs/OpenTLD https://github.com/arthurv/OpenTLD problem opencv3.0 ...

  3. 【EMV L2】终端风险管理(Terminal Risk Management)

    终端风险管理使大额交易联机授权,并确保芯片交易能够周期性地进行联机以防止在脱机环境中也许无法察觉的风险. 虽然发卡行被强制要求在应用交互特征(AIP)中将终端风险管理位设置成1以触发终端风险管理,但终 ...

  4. linux 常用命令积累

    工作中常用的linux记录一下,方便查询使用 1.ln 创建连接 ,就是window上的快捷方式 创建软连接  ln -s 源文件 目标文件名   经常访问的文件夹(项目文件夹),在~创建一个软连很方 ...

  5. Golang微服务:Micro Trace使用opentracing jaeger

    trace Micro通过Wrapper实现了三种trace接口,aswxray,opencensus,opentracing,这里主要关注opentracing,opentracing已成为行业标准 ...

  6. Python相关

    当Python源码编译以后,需要对一些依赖库重新安装,而且有些是要用pip去安装. python 文件.目录属性的操作os.path等os模块函数 https://blog.csdn.net/bytx ...

  7. webstorm 2018 LICENSE SERVER 最新激活网址

    2018年8月更新:https://s.tuzhihao.com:666 (亲测)

  8. Linux压缩和解压缩

    1.tar.gz tar.gz这种格式是Linux下使用得最多的压缩格式.它在压缩时不会占用太多CPU的,而且可以得到一个非常理想的压缩率. tar -zcvf archive_name.tar.gz ...

  9. 简述osi七层模型和TCP/IP五层模型

    OSI七层模型 OSI七层模型的划分 应用层(Application).表示层(presentation).会话层(session).传输层(Transport).网络层(Network).数据链路层 ...

  10. opencv+qt+vtk,编程时报错'detail':ambiguous symbol

    解决办法: 把#include <vtkSmartPointer.h>放到所有头文件的最前面: