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






