基于ScheduledExecutorService的兩種方法(詳解)
開發(fā)中,往往遇到另起線程執(zhí)行其他代碼的情況,用java定時任務(wù)接口ScheduledExecutorService來實現(xiàn)。
ScheduledExecutorService是基于線程池設(shè)計的定時任務(wù)類,每個調(diào)度任務(wù)都會分配到線程池中的一個線程去執(zhí)行,也就是說,任務(wù)是并發(fā)執(zhí)行,互不影響。
注意,只有當(dāng)調(diào)度任務(wù)來的時候,ScheduledExecutorService才會真正啟動一個線程,其余時間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())); } }
運行結(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
可以看出來,在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");//設(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())); } }
運行結(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.兩個區(qū)別
ScheduleAtFixedRate每次執(zhí)行時間為上一次任務(wù)開始起向后推一個時間間隔,即每次執(zhí)行時間為initialDelay,initialDelay+period,initialDelay+2*period。。。。。
ScheduleWithFixedDelay每次執(zhí)行時間為上一次任務(wù)結(jié)束起向后推一個時間間隔,即每次執(zhí)行時間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。
由此可見,ScheduleAtFixedRate是基于固定時間間隔進行任務(wù)調(diào)度,ScheduleWithFixedDelay取決于每次任務(wù)執(zhí)行的時間長短,是基于不固定時間間隔進行任務(wù)調(diào)度。
以上這篇基于ScheduledExecutorService的兩種方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Java自帶定時任務(wù)ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能
- Java并發(fā)之線程池Executor框架的深入理解
- Spring線程池ThreadPoolTaskExecutor配置詳情
- Java ExecutorService四種線程池使用詳解
- ScheduledExecutorService任務(wù)定時代碼示例
- ThreadPoolExecutor線程池原理及其execute方法(詳解)
- 簡單談?wù)凾hreadPoolExecutor線程池之submit方法
- java ThreadPoolExecutor 并發(fā)調(diào)用實例詳解
- 詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程
- Executor攔截器高級教程QueryInterceptor的規(guī)范
相關(guān)文章
Spring如何使用PropertyPlaceholderConfigurer讀取文件
這篇文章主要介紹了Spring如何使用PropertyPlaceholderConfigurer讀取文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12SpringCloud微服務(wù)應(yīng)用config配置中心詳解
這篇文章主要介紹了SpringCloud微服務(wù)應(yīng)用-config配置中心,包括相關(guān)知識介紹、搭建、動態(tài)刷新、測試,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07java線程并發(fā)blockingqueue類使用示例
BlockingQueue是一種特殊的Queue,若BlockingQueue是空的,從BlockingQueue取東西的操作將會被阻斷進入等待狀態(tài)直到BlocingkQueue進了新貨才會被喚醒,下面是用BlockingQueue來實現(xiàn)Producer和Consumer的例子2014-01-01解決spring-integration-mqtt頻繁報Lost connection錯誤問題
這篇文章主要介紹了解決spring-integration-mqtt頻繁報Lost connection錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03