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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - Sun_china

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