惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
GbyAI
GbyAI
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
B
Blog
博客园 - 叶小钗
V
Visual Studio Blog
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
博客园 - Franky
V
V2EX
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
IT之家
IT之家
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
N
Netflix TechBlog - Medium
博客园 - 【当耐特】

博客园 - Sun_china

服务注册中心,Eureka比Zookeeper好在哪里? 数据库锁 【转】数据库连接池优化配置(druid,dbcp,c3p0) 在Spring Boot中使用 @ConfigurationProperties 注解 Shiro简介——《跟我学Shiro》 【转】Lombok 安装、入门 - 消除冗长的 java 代码 java中抽象类是否可以继承实体类? MyBatis中使用#和$书写占位符有什么区别? 什么是悲观锁和乐观锁? java为什么不能根据返回值重载? java中String、StringBuffer、StringBuilder的区别 【转】java面试题 分布式系统中关于转账案例的剖析 支付平台架构师谈大规模高并发服务化系统设计经验 全面了解Ngnix的主要应用的场景 HashMap和HashSet的区别 hashmap和hashtable的区别? java多线程中并发集合和同步集合有哪些?区别是什么? java多线程中最佳的实践方案是什么?
scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
Sun_china · 2017-08-29 · via 博客园 - Sun_china

总结:

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);  
    }  
  
}  

View Code

注意:

通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常。