Java中ScheduledExecutorService的使用方法詳解
ScheduledExecutorService 簡介
ScheduledExecutorService是ExecutorService的一個子接口。它主要用于在給定的延遲之后或周期性地執(zhí)行任務。這個接口提供了一種方便的方式來處理異步任務的調(diào)度,相比于傳統(tǒng)的Timer和TimerTask,它具有更好的靈活性和可靠性,特別是在處理多線程環(huán)境下的任務調(diào)度時。
我們可以通過ScheduledExecutorService,更有效地管理和調(diào)度多個異步任務,這些任務可以是一次性執(zhí)行的,也可以是周期性重復執(zhí)行的。
常用方法
schedule
schedule(Runnable command, long delay, TimeUnit unit)
功能描述:該方法用于安排一個任務(Runnable)在指定的延遲(delay)之后執(zhí)行一次。TimeUnit是一個枚舉類型,用于指定延遲的時間單位,例如TimeUnit.SECONDS表示秒,TimeUnit.MILLISECONDS表示毫秒等。
示例代碼:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Runnable task = () -> System.out.println("任務在延遲后執(zhí)行"); scheduler.schedule(task, 5, TimeUnit.SECONDS); scheduler.shutdown();
解釋:在這個示例中,首先通過Executors.newScheduledThreadPool(1)創(chuàng)建了一個ScheduledExecutorService對象,線程池大小為 1。然后定義了一個Runnable任務,該任務只是簡單地打印一條消息。接著使用schedule方法安排這個任務在 5 秒(TimeUnit.SECONDS)后執(zhí)行。最后調(diào)用shutdown方法來優(yōu)雅地關(guān)閉ScheduledExecutorService,防止資源泄漏。
scheduleAtFixedRate
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
功能描述:用于安排一個任務在初始延遲(initialDelay)之后開始執(zhí)行,然后以固定的頻率(period)重復執(zhí)行。如果任務的執(zhí)行時間超過了指定的周期(period),下一次執(zhí)行會在當前任務執(zhí)行完成后立即開始,不會等待一個完整的周期。
示例代碼:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Runnable task = () -> System.out.println("周期性任務執(zhí)行"); scheduler.scheduleAtFixedRate(task, 2, 3, TimeUnit.SECONDS); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown();
解釋:在這個例子中,任務會在 2 秒(initialDelay)后首次執(zhí)行,然后每隔 3 秒(period)執(zhí)行一次。通過Thread.sleep(10000)讓主線程等待一段時間,以便觀察任務的周期性執(zhí)行。最后關(guān)閉ScheduledExecutorService。
scheduleWithFixedDelay
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
功能描述:安排一個任務在初始延遲(initialDelay)之后開始執(zhí)行,并且在每次執(zhí)行完成后,等待固定的延遲(delay)時間后再執(zhí)行下一次任務。這與scheduleAtFixedRate不同,scheduleAtFixedRate是按照固定的頻率執(zhí)行,而這個方法是在上一次任務執(zhí)行完成后等待固定的延遲時間再執(zhí)行下一次。
示例代碼:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Runnable task = () -> { System.out.println("任務開始執(zhí)行"); try { Thread.sleep(2000); // 模擬任務執(zhí)行時間 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("任務執(zhí)行完成"); }; scheduler.scheduleWithFixedDelay(task, 2, 3, TimeUnit.SECONDS); try { Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown();
解釋:在這個示例中,任務首先在 2 秒(initialDelay)后開始執(zhí)行。任務本身會模擬執(zhí)行 2 秒(通過Thread.sleep(2000)),然后在任務執(zhí)行完成后,等待 3 秒(delay)再執(zhí)行下一次任務。通過Thread.sleep(15000)讓主線程等待足夠長的時間來觀察任務的執(zhí)行情況,最后關(guān)閉ScheduledExecutorService。
關(guān)于shutdown
關(guān)閉釋放資源: 雖然JVM會自動回收資源,但是為防止內(nèi)存泄漏,還是建議每次執(zhí)行完成后都添加shutdown()方法,我們可以根據(jù)不同場景選擇不同的關(guān)閉方法
// 等待所有任務完成才去執(zhí)行關(guān)閉 executor.shutdown(); // 如果需要立即關(guān)閉并嘗試中斷任務 executor.shutdownNow(); // 指定等待時間來等待執(zhí)行完成 executor.awaitTermination(long timeout, TimeUnit unit) // 如果在等待時間內(nèi)執(zhí)行器成功關(guān)閉,該方法將返回true;否則返回false
到此這篇關(guān)于Java中ScheduledExecutorService的使用方法詳解的文章就介紹到這了,更多相關(guān)Java ScheduledExecutorService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中l(wèi)ogback?自動刷新不生效的問題解決
本文主要介紹了Java中l(wèi)ogback?自動刷新不生效的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05使用Java動態(tài)創(chuàng)建Flowable會簽模型的示例代碼
動態(tài)創(chuàng)建流程模型,尤其是會簽(Parallel Gateway)模型,是提升系統(tǒng)靈活性和響應速度的關(guān)鍵技術(shù)之一,本文將通過Java編程語言,深入探討如何在運行時動態(tài)地創(chuàng)建包含會簽環(huán)節(jié)的Flowable流程模型,需要的朋友可以參考下2024-05-05