基于ScheduledExecutorService的兩種方法(詳解)
開(kāi)發(fā)中,往往遇到另起線程執(zhí)行其他代碼的情況,用java定時(shí)任務(wù)接口ScheduledExecutorService來(lái)實(shí)現(xiàn)。
ScheduledExecutorService是基于線程池設(shè)計(jì)的定時(shí)任務(wù)類,每個(gè)調(diào)度任務(wù)都會(huì)分配到線程池中的一個(gè)線程去執(zhí)行,也就是說(shuō),任務(wù)是并發(fā)執(zhí)行,互不影響。
注意,只有當(dāng)調(diào)度任務(wù)來(lái)的時(shí)候,ScheduledExecutorService才會(huì)真正啟動(dòng)一個(gè)線程,其余時(shí)間ScheduledExecutorService都是處于輪詢?nèi)蝿?wù)的狀態(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");//設(shè)置日期格式
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()));
}
}
運(yùn)行結(jié)果:
++++++++++++++++++++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
可以看出來(lái),在2s后,子線程開(kāi)始執(zhí)行,并且每過(guò)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");//設(shè)置日期格式
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()));
}
}
運(yùn)行結(jié)果:
++++++++++++++++++++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.兩個(gè)區(qū)別
ScheduleAtFixedRate每次執(zhí)行時(shí)間為上一次任務(wù)開(kāi)始起向后推一個(gè)時(shí)間間隔,即每次執(zhí)行時(shí)間為initialDelay,initialDelay+period,initialDelay+2*period。。。。。
ScheduleWithFixedDelay每次執(zhí)行時(shí)間為上一次任務(wù)結(jié)束起向后推一個(gè)時(shí)間間隔,即每次執(zhí)行時(shí)間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。
由此可見(jiàn),ScheduleAtFixedRate是基于固定時(shí)間間隔進(jìn)行任務(wù)調(diào)度,ScheduleWithFixedDelay取決于每次任務(wù)執(zhí)行的時(shí)間長(zhǎng)短,是基于不固定時(shí)間間隔進(jìn)行任務(wù)調(diào)度。
以上這篇基于ScheduledExecutorService的兩種方法(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java自帶定時(shí)任務(wù)ScheduledThreadPoolExecutor實(shí)現(xiàn)定時(shí)器和延時(shí)加載功能
- Java并發(fā)之線程池Executor框架的深入理解
- Spring線程池ThreadPoolTaskExecutor配置詳情
- Java ExecutorService四種線程池使用詳解
- ScheduledExecutorService任務(wù)定時(shí)代碼示例
- ThreadPoolExecutor線程池原理及其execute方法(詳解)
- 簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法
- java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解
- 詳解Java利用ExecutorService實(shí)現(xiàn)同步執(zhí)行大量線程
- Executor攔截器高級(jí)教程QueryInterceptor的規(guī)范
相關(guān)文章
jar的MANIFEST.MF配置Class-Path, java -classpath設(shè)置無(wú)效的解
這篇文章主要介紹了jar的MANIFEST.MF配置Class-Path, java -classpath設(shè)置無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
解決MyBatis @param注解參數(shù)類型錯(cuò)誤異常的問(wèn)題
這篇文章主要介紹了解決MyBatis @param注解參數(shù)類型錯(cuò)誤異常的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
SpringSecurity授權(quán)機(jī)制的實(shí)現(xiàn)(AccessDecisionManager與投票決策)
本文主要介紹了SpringSecurity授權(quán)機(jī)制的實(shí)現(xiàn),其核心是AccessDecisionManager和投票系統(tǒng),下面就來(lái)介紹一下,感興趣的可以了解一下2025-04-04
詳解SpringBoot優(yōu)雅編碼之Lombok加持
這篇文章主要介紹了詳解SpringBoot優(yōu)雅編碼之Lombok加持,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

