基于ScheduledExecutorService的兩種方法(詳解)
開發(fā)中,往往遇到另起線程執(zhí)行其他代碼的情況,用java定時任務接口ScheduledExecutorService來實現(xiàn)。
ScheduledExecutorService是基于線程池設計的定時任務類,每個調(diào)度任務都會分配到線程池中的一個線程去執(zhí)行,也就是說,任務是并發(fā)執(zhí)行,互不影響。
注意,只有當調(diào)度任務來的時候,ScheduledExecutorService才會真正啟動一個線程,其余時間ScheduledExecutorService都是處于輪詢?nèi)蝿盏臓顟B(tài)。
1.scheduleAtFixedRate方法
例子:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduleAtFixedRateDemo {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
SimpleDateFormat df =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
executorService.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
}
}, 2, 3, TimeUnit.SECONDS);
System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
}
}
運行結果:
++++++++++++++++++++main:2017-10-20 15:20:52 ++++++++++++++++++++thread:2017-10-20 15:20:54 ++++++++++++++++++++thread:2017-10-20 15:20:57 ++++++++++++++++++++thread:2017-10-20 15:21:00 ++++++++++++++++++++thread:2017-10-20 15:21:03 ++++++++++++++++++++thread:2017-10-20 15:21:06
可以看出來,在2s后,子線程開始執(zhí)行,并且每過3s輪詢執(zhí)行一次。
2.scheduleWithFixedDelay方法
例子:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* ScheduleWithFixedDelay的用法
* @author Administrator
*
*/
public class ScheduleWithFixedDelayDemo {
public static void main(String[] args) {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
SimpleDateFormat df =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
executorService.scheduleWithFixedDelay(new Runnable(){
@Override
public void run() {
System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
}
}, 2, 3, TimeUnit.SECONDS);
System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
}
}
運行結果:
++++++++++++++++++++main:2017-10-20 15:24:32 ++++++++++++++++++++thread:2017-10-20 15:24:34 ++++++++++++++++++++thread:2017-10-20 15:24:37 ++++++++++++++++++++thread:2017-10-20 15:24:40 ++++++++++++++++++++thread:2017-10-20 15:24:43
3.兩個區(qū)別
ScheduleAtFixedRate每次執(zhí)行時間為上一次任務開始起向后推一個時間間隔,即每次執(zhí)行時間為initialDelay,initialDelay+period,initialDelay+2*period。。。。。
ScheduleWithFixedDelay每次執(zhí)行時間為上一次任務結束起向后推一個時間間隔,即每次執(zhí)行時間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。
由此可見,ScheduleAtFixedRate是基于固定時間間隔進行任務調(diào)度,ScheduleWithFixedDelay取決于每次任務執(zhí)行的時間長短,是基于不固定時間間隔進行任務調(diào)度。
以上這篇基于ScheduledExecutorService的兩種方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Java自帶定時任務ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能
- Java并發(fā)之線程池Executor框架的深入理解
- Spring線程池ThreadPoolTaskExecutor配置詳情
- Java ExecutorService四種線程池使用詳解
- ScheduledExecutorService任務定時代碼示例
- ThreadPoolExecutor線程池原理及其execute方法(詳解)
- 簡單談談ThreadPoolExecutor線程池之submit方法
- java ThreadPoolExecutor 并發(fā)調(diào)用實例詳解
- 詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程
- Executor攔截器高級教程QueryInterceptor的規(guī)范
相關文章
jar的MANIFEST.MF配置Class-Path, java -classpath設置無效的解
這篇文章主要介紹了jar的MANIFEST.MF配置Class-Path, java -classpath設置無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
解決MyBatis @param注解參數(shù)類型錯誤異常的問題
這篇文章主要介紹了解決MyBatis @param注解參數(shù)類型錯誤異常的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringSecurity授權機制的實現(xiàn)(AccessDecisionManager與投票決策)
本文主要介紹了SpringSecurity授權機制的實現(xiàn),其核心是AccessDecisionManager和投票系統(tǒng),下面就來介紹一下,感興趣的可以了解一下2025-04-04
詳解SpringBoot優(yōu)雅編碼之Lombok加持
這篇文章主要介紹了詳解SpringBoot優(yōu)雅編碼之Lombok加持,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

