scheduleAtFixedRate

每间隔一段时间执行,分为两种情况:

  1. 当前任务执行时间小于间隔时间,每次到点即执行;

    /**
    * 任务执行时间(8s)小于间隔时间(10s)
    */
    public class ScheduleTest {
    static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
    scheduler.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
    System.out.println("Start: scheduleAtFixedRate: " + new Date());
    try {
    Thread.sleep(8000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("End : scheduleAtFixedRate: " + new Date());
    }
    }, 0, 10 , SECONDS);
    }
    } output: Start: scheduleAtFixedRate: Sun Apr 28 14:36:00 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:36:08 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:36:10 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:36:18 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:36:20 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:36:28 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:36:30 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:36:38 CST 2019
    ... 程序启动时间是14:36:00,以后每间隔10s执行一次(即14:36:10、14:36:20、14:36:30等)。
  2. 当前任务执行时间大于等于间隔时间,任务执行后立即执行下一次任务。相当于连续执行了。

    /**
    * 任务执行时间(12s)大于间隔时间(10s)
    */
    public class ScheduleTest {
    static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
    scheduler.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
    System.out.println("Start: scheduleAtFixedRate: " + new Date());
    try {
    Thread.sleep(12000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("End : scheduleAtFixedRate: " + new Date());
    }
    }, 0, 10 , SECONDS);
    }
    } output: Start: scheduleAtFixedRate: Sun Apr 28 14:30:13 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:30:25 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:30:25 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:30:37 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:30:37 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:30:49 CST 2019
    Start: scheduleAtFixedRate: Sun Apr 28 14:30:49 CST 2019
    End : scheduleAtFixedRate: Sun Apr 28 14:31:01 CST 2019 程序启动时间是14:30:13,按理说应该每间隔10s执行一次(即14:30:23、14:30:33等),但由于任务执行时间长于10s,下一次的任务要开始的时候发现上次的任务还没有完成,因此阻塞等待,一旦发现上次的任务完成,就马上启动。表现出来就是任务延时启动,最终的效果就是连续执行。

scheduleWithFixedDelay

每当上次任务执行完毕后,间隔一段时间执行。不管当前任务执行时间大于、等于还是小于间隔时间,执行效果都是一样的。

/**
* 任务执行时间(8s)小于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleWithFixedDelay: " + new Date());
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleWithFixedDelay: " + new Date());
}
}, 0, 10 , SECONDS);
}
} output: Start: scheduleWithFixedDelay: Sun Apr 28 14:27:59 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:07 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:17 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:25 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:35 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:28:43 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:28:53 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:29:01 CST 2019
... 可以看出每个End后,等待了10秒,才启动下一次Start。
/**
* 任务执行时间(12s)大于间隔时间(10s)
*/
public class ScheduleTest {
static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public static void main(String[] args) {
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("Start: scheduleWithFixedDelay: " + new Date());
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End : scheduleWithFixedDelay: " + new Date());
}
}, 0, 10 , SECONDS);
}
} output: Start: scheduleWithFixedDelay: Sun Apr 28 14:26:29 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:26:41 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:26:51 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:03 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:27:13 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:25 CST 2019
Start: scheduleWithFixedDelay: Sun Apr 28 14:27:35 CST 2019
End : scheduleWithFixedDelay: Sun Apr 28 14:27:47 CST 2019
... 可以看出每个End后,等待了10秒,才启动下一次Start。

参考

scheduleAtFixedRate vs scheduleWithFixedDelay

扩展

Spring定时任务@Scheduled注解使用方式浅窥(cron表达式、fixedRate和fixedDelay)

理解ScheduledExecutorService中scheduleAtFixedRate和scheduleWithFixedDelay的区别的更多相关文章

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

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

  2. scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

    总结: scheduleAtFixedRate ,是以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完 ...

  3. 深入理解css3中nth-child和 nth-of-type的区别

    在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...

  4. 深入理解css3中 nth-child 和 nth-of-type 的区别

    在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...

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

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

  6. IOS基础:深入理解Objective-c中@class 和#import的区别

    在面向对象objective-C语言中,当一个类使用到另一个类时,并且在类的头文件中需要创建被引用的指针时,可以#import方式引入,通过@class引入: 这两种的方式的区别在于: 1.运用#im ...

  7. 理解js中__proto__和prototype的区别和关系

    首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性__proto ...

  8. 【转】为什么我们都理解错了HTTP中GET与POST的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  9. 简单理解Struts2中拦截器与过滤器的区别及执行顺序

    简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...

随机推荐

  1. ScalaPB(4): 通用跨系统protobuf数据,sbt设置

    我们知道,在集群环境节点之间进行交换的数据必须经过序列化/反序列化处理过程,而在这方面protobuf是一个比较高效.易用的模式.用户首先在.proto文件中用IDL来定义系统中各种需要进行交换的数据 ...

  2. Xamarin.Android 使用AsyncTask提示上传动态

    我们有时候会通过WebServices上传数据,如果信息量过大并没有提示,用户会觉得是死机,或是系统崩溃,这时候我们可以用到AsyncTask(异步任务)来提示上传信息,例如:正在上传数据... 这里 ...

  3. HTTP相关:TCP/IP、DNS

    最近在看HTTP的书,看得有点慢,而且断断续续的,很多东西看完就忘了.知识点多且零散,感觉要多看几遍才能消化. TCP/IP协议族按层次分为4层: 应用层: 应用层决定了向用户提供应用服务时通信的活动 ...

  4. cas 4.1.4单点登录实战

    使用工具 maven-3.3.9 cas-4.1.4 Tomcat-7.0.57-win-x64 cas-sample-Java-webapp 一.Hello cas 1.下载Tomcat,解压:修改 ...

  5. Java JFrame图形界面 ----一个简单的窗口

    #开始 申请博客已经有一段时间了 但是一直没有时间写博文(其实还是懒虫侵蚀了大脑) 最近正在学习JFrame做窗口 遇到了很多的问题 为了解决问题也谋杀了很多的脑细胞 为了让更多的朋友不死的很多脑细胞 ...

  6. Ribbon负载均衡策略配置

    在这里吐槽一句:网上很多文章真是神坑,你不看还好,看了只会问题越来越多,就连之前的问题都没有解决!!! 不多说了,Ribbon作为后端负载均衡器,比Nginx更注重的是请求分发而不是承担并发,可以直接 ...

  7. Python 的 urllib.parse 库解析 URL

      Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...

  8. 大话RabbitMQ 基础入门

    ----------写在前面---------- 近些年微服务越来越火,让我也忍不住想去一窥微服务究竟,讲到微服务,就离不开分布式,而分布式,也离不开消息队列,在消息队列中,RabbitMQ可以说是比 ...

  9. 如何修改Tomcat默认端口?

    修改的原因: 关于8080端口:8080端口同80端口,是被用于WWW代理服务的,可以实现网页浏览,经常在访问某个网站或使用代理服务器的时候,会加上":8080"端口号.另外Apa ...

  10. sprintf、strcpy 及 memcpy 函数区别

    这些函数的区别在于 实现功能 以及 操作对象 不同.strcpy 函数操作的对象是 字符串 ,完成 从 源字符串 到 目的字符串 的 拷贝 功能. sprintf 函数操作的对象 不限于字符串 :虽然 ...