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

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 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一切可能的异常。