Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線程池的特性,也可以實現(xiàn)任務(wù)循環(huán)執(zhí)行,可以看作是一個簡單地定時任務(wù)組件,因為有線程池特性,所以任務(wù)之間可以多線程并發(fā)執(zhí)行,互不影響,當(dāng)任務(wù)來的時候,才會真正創(chuàng)建線程去執(zhí)行
我們在做一些普通定時循環(huán)任務(wù)時可以用它,比如定時刷新字典常量,只需要不斷重復(fù)執(zhí)行即可,這篇文章講解一下它的用法以及注意事項,不涉及底層原理
注意:我們都知道,在使用線程池的時候,如果我們的任務(wù)出現(xiàn)異常沒有捕獲,那么線程會銷毀被回收,不會影響其他任務(wù)繼續(xù)提交并執(zhí)行,但是在這里,如果你的任務(wù)出現(xiàn)異常沒有捕獲,會導(dǎo)致后續(xù)的任務(wù)不再執(zhí)行,所以一定要try...catch
1. 延遲不循環(huán)任務(wù)schedule方法
schedule(Runnable command, long delay, TimeUnit unit)
參數(shù)1:任務(wù)
參數(shù)2:方法第一次執(zhí)行的延遲時間
參數(shù)3:延遲單位
說明:延遲任務(wù),只執(zhí)行一次(不會再次執(zhí)行),參數(shù)2為延遲時間
案例說明:
@Component
@Slf4j
public class MineExecutors {
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
@PostConstruct
public void init() {
scheduler.schedule(() -> {
try {
log.info("開始執(zhí)行...time {}", format.format(new Date()));
Thread.sleep(1000);
log.info("執(zhí)行結(jié)束...time {}", format.format(new Date()));
} catch (Exception e) {
log.error("定時任務(wù)執(zhí)行出錯");
}
}, 5, TimeUnit.SECONDS);
log.info("初始化成功 {}", format.format(new Date()));
}
}可以看到任務(wù)執(zhí)行時間為初始化完成后5s才開始執(zhí)行,且只執(zhí)行一次

2. 延遲且循環(huán)cheduleAtFixedRate方法
cheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
參數(shù)1:任務(wù)
參數(shù)2:初始化完成后延遲多長時間執(zhí)行第一次任務(wù)
參數(shù)3:任務(wù)時間間隔
參數(shù)4:單位
方法解釋:是以上一個任務(wù)開始的時間計時,比如period為5,那5秒后,檢測上一個任務(wù)是否執(zhí)行完畢,如果上一個任務(wù)執(zhí)行完畢,則當(dāng)前任務(wù)立即執(zhí)行,如果上一個任務(wù)沒有執(zhí)行完畢,則需要等上一個任務(wù)執(zhí)行完畢后立即執(zhí)行,如果你的任務(wù)執(zhí)行時間超過5秒,那么任務(wù)時間間隔參數(shù)將無效,任務(wù)會不停地循環(huán)執(zhí)行,由此可得出該方法不能嚴(yán)格保證任務(wù)按一定時間間隔執(zhí)行
錯誤:任務(wù)連續(xù)執(zhí)行案例:
@Component
@Slf4j
public class MineExecutors {
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@PostConstruct
public void init() {
scheduler.scheduleAtFixedRate(() -> {
try {
log.info("開始執(zhí)行...time {}", format.format(new Date()));
Thread.sleep(3000);
log.info("執(zhí)行結(jié)束...time {}", format.format(new Date()));
} catch (Exception e) {
log.error("定時任務(wù)執(zhí)行出錯");
}
}, 0, 2, TimeUnit.SECONDS);
log.info("初始化成功 {}", format.format(new Date()));
}
}由上面代碼可以看出,任務(wù)執(zhí)行需要3秒,而我們設(shè)定的任務(wù)時間間隔為2秒,如此就會導(dǎo)致任務(wù)連續(xù)執(zhí)行,該方法不能嚴(yán)格保證任務(wù)按照規(guī)定的時間間隔執(zhí)行,如果你的任務(wù)執(zhí)行時間可以保證忽略不計,則可以使用該方法,我們可以看到下面日志,上一個任務(wù)的執(zhí)行結(jié)束時間與下一個任務(wù)的開始時間一致,所以任務(wù)連續(xù)循環(huán)執(zhí)行了

正確案例:
@Component
@Slf4j
public class MineExecutors {
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@PostConstruct
public void init() {
scheduler.scheduleAtFixedRate(() -> {
try {
log.info("開始執(zhí)行...time {}", format.format(new Date()));
Thread.sleep(1000);
log.info("執(zhí)行結(jié)束...time {}", format.format(new Date()));
} catch (Exception e) {
log.error("定時任務(wù)執(zhí)行出錯");
}
}, 0, 3, TimeUnit.SECONDS);
log.info("初始化成功 {}", format.format(new Date()));
}
}可以看到任務(wù)以上一次任務(wù)的開始時間,按3秒一次的方式執(zhí)行

3. 嚴(yán)格按照一定時間間隔執(zhí)行``
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
參數(shù)1:任務(wù)
參數(shù)2:初始化完成后延遲多長時間執(zhí)行第一次任務(wù)
參數(shù)3:任務(wù)執(zhí)行時間間隔
參數(shù)4:單位
解釋:以上一次任務(wù)執(zhí)行結(jié)束時間為準(zhǔn),加上任務(wù)時間間隔作為下一次任務(wù)開始時間,由此可以得出,任務(wù)可以嚴(yán)格按照時間間隔執(zhí)行
案例:
@Component
@Slf4j
public class MineExecutors {
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@PostConstruct
public void init() {
scheduler.scheduleWithFixedDelay(() -> {
try {
log.info("開始執(zhí)行...time {}", format.format(new Date()));
Thread.sleep(5000);
log.info("執(zhí)行結(jié)束...time {}", format.format(new Date()));
} catch (Exception e) {
log.error("定時任務(wù)執(zhí)行出錯");
}
}, 0, 3, TimeUnit.SECONDS);
log.info("初始化成功 {}", format.format(new Date()));
}
}由下圖日志可以看出,下次任務(wù)的開始時間是在上一次任務(wù)結(jié)束時間+任務(wù)時間間隔為準(zhǔn)的,嚴(yán)格按照任務(wù)時間間隔,規(guī)律執(zhí)行,如果你的任務(wù)需要保證嚴(yán)格的時間間隔,可以用該方法啟動任務(wù)

其他用法與線程池沒有差異了,例如ThreadFactory作為參數(shù)傳入,自定義線程池內(nèi)線程名稱之類的,不多解釋了。
到此這篇關(guān)于Java ScheduledExecutorService的具體使用的文章就介紹到這了,更多相關(guān)ScheduledExecutorService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springcloud如何獲取網(wǎng)關(guān)封裝的頭部信息
這篇文章主要介紹了springcloud獲取網(wǎng)關(guān)封裝的頭部信息,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
IDEA的Web項目右鍵無法創(chuàng)建Servlet問題解決辦法
這篇文章主要介紹了IDEA的Web項目右鍵無法創(chuàng)建Servlet問題解決辦法的相關(guān)資料,在IDEA中新建Servlet時發(fā)現(xiàn)缺失選項,可以通過在pom.xml文件中添加servlet依賴解決,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-10-10
SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖
這篇文章給大家介紹了SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-03-03

