亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

JAVA.util.concurrent.ScheduledExecutorService是一個(gè)可以安排任務(wù)延遲執(zhí)行的 ExecutorService , 或者以固定的時(shí)間間隔重復(fù)執(zhí)行。任務(wù)通過一個(gè)工作線程異步執(zhí)行,而不是提交任務(wù)到ScheduledExecutorService的線程。

ScheduledExecutorService例子

下面是ScheduledExecutorService例子:

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(5);
 
ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    5,
    TimeUnit.SECONDS);

首先,創(chuàng)建一個(gè)容納5個(gè)線程的.然后,創(chuàng)建了Callable 接口的一個(gè)匿名類作為參數(shù)提交到Callable。

ScheduledExecutorService實(shí)現(xiàn)

既然ScheduledExecutorService是個(gè)接口, ava.util.concurrent包中的ScheduledExecutorService 的類實(shí)現(xiàn)了該接口:

  • ScheduledThreadPoolExecutor

創(chuàng)建ScheduledExecutorService

創(chuàng)建 ScheduledExecutorService 取決于你用哪種實(shí)現(xiàn),當(dāng)然也可以Executors 的工廠方法創(chuàng)建ScheduledExecutorService 實(shí)例,下面是代碼:

ScheduledExecutorService scheduledExecutorService =
 
        Executors.newScheduledThreadPool(5);

ScheduledExecutorService用法

一旦創(chuàng)建了 ScheduledExecutorService,可以用下面方法 :

  • schedule (Callable task, long delay, TimeUnit timeunit)
  • schedule (Runnable task, long delay, TimeUnit timeunit)
  • scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)
  • scheduleWithFixedDelay (Runnable, long initialDelay, long period, TimeUnit timeunit)

下面一一講解這些方法:

schedule (Callable task, long delay, TimeUnit timeunit)

這個(gè)方法安排給定得 Callable 延遲執(zhí)行,這方法返回ScheduledFuture ,可以用于在任務(wù)未執(zhí)行前取消任務(wù)或者當(dāng)執(zhí)行完了獲取返回結(jié)果,下面是代碼:

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(5);
 
ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    5,
    TimeUnit.SECONDS);
 
System.out.println("result = " + scheduledFuture.get());
 
scheduledExecutorService.shutdown();

輸出結(jié)果:

Executed!
result = Called!

schedule (Runnable task, long delay, TimeUnit timeunit)

這個(gè)方法類似于上面得方法,但是沒有返回結(jié)果,所以任務(wù)完成 ScheduledFuture.get()將返回null。

scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)

這個(gè)方法安排任務(wù)間隔執(zhí)行,任務(wù)首次在initialDelay以后執(zhí)行,然后每次間隔initialDelay執(zhí)行。如果任何一次拋異常,那么任務(wù)不再執(zhí)行,如果沒有異常,任務(wù)一直執(zhí)行直到ScheduledExecutorService 關(guān)閉,如果當(dāng)前線程執(zhí)行時(shí)間很長(zhǎng),那么下一個(gè)任務(wù)要等到這個(gè)任務(wù)執(zhí)行完成,在同一時(shí)間只執(zhí)行一個(gè)任務(wù)。

scheduleWithFixedDelay (Runnable, long initialDelay, long period, TimeUnit timeunit)

這個(gè)方法和 scheduleAtFixedRate()非常相似,只是時(shí)間段有不同的解釋。

在scheduleAtFixedRate()方法中,周期被解釋為從上一次執(zhí)行開始到下一次執(zhí)行開始之間的延遲。

然而,在這種方法中,周期被解釋為上一次執(zhí)行結(jié)束到下一次執(zhí)行開始之間的延遲。因此,延遲是在完成執(zhí)行之間,而不是在執(zhí)行開始之間。

ScheduledExecutorService Shutdown

和 ExecutorService一樣, 當(dāng)任務(wù)執(zhí)行完畢 ScheduledExecutorService需要關(guān)閉,如果不關(guān)閉,一直在JVM中運(yùn)行,盡管其他線程已經(jīng)關(guān)閉。

關(guān)閉ScheduledExecutorService用shutdown() 或者 shutdownNow() 方法,這兩個(gè)方法是從ExecutorService接口繼承得, 可以查看前面文章 ExecutorService 中得Shutdown 。

參考:https://blog.csdn.net/cgsyck/article/details/107692471

http://tutorials.jenkov.com/java-util-concurrent/scheduledexecutorservice.html

https://blog.csdn.net/cgsyck/article/details/107769550

分享到:
標(biāo)簽:并發(fā) JAVA
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定