总结:

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. ReentrantLock+线程池+同步+线程锁

    1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...

  2. python从零开始 -- 第0篇之Hello World!

    为什么选择python以及版本选择 学习资料 学习方法和路径 1. 为什么选择python以及版本选择: Python  好玩,强大,更多关于关于为什么选择Python,在  编程小白的第一本 Pyt ...

  3. 数组之slice,splice,Concact,Reverse,Sort方法

    Slice(strart,end)用来从数组中提取元素.该方法不会改变元素数组,而是将截取到的元素封装到一个新数组中返回 参数start 截取开始的位置索引,包含开始索引 参数end 截取结束位置的索 ...

  4. C# autocomplete

    前台代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runa ...

  5. jmeter 上传附件脚本报Non HTTP response code: java.io.FileNotFoundException

    如果上传附件报如下错误,就需要把附件放到和脚本同一路径下就解决了

  6. svg 图片

    https://studio.qcloud.coding.net/rs2/d67e3c26b502365f8ab7c05d71c70471.svg 腾讯编辑器loading页面的svg

  7. 聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用

    1.用python实现K均值算法 import numpy as np x = np.random.randint(1,100,20)#产生的20个一到一百的随机整数 y = np.zeros(20) ...

  8. vue-cli 3.0生成的项目run build后为空白页

    vue-cli 3.0 生成的项目与2.x不同,其中并没有webpack配置文件config/index.js.这个时候需要我们在项目的根目录下创建一个vue.config.js文件,代码如下: mo ...

  9. 利用python破解sqlserver账号密码

    一.编写密码测试函数 在用python连接mssql数据库的时候,通常会使用pymssql模板中的connect函数,格式如下: connect(server,user,password,databa ...

  10. Visual Studio 2017 Android 调试无法连接到虚拟机

    输出窗口输出如下: 1>Starting deploy 4.5" KitKat (4.4) HDPI Phone ... 1>Starting emulator 4.5" ...