Java併發包:ScheduledExecutorService

文章譯自:http://tutorials.jenkov.com/java-util-concurrent/index.html 
抽空翻譯了一下這個教程的文章,後面會陸續放出,若有不妥,請批評指正。 
轉自請註明出處。html

ScheduledExecutorService

java.util.concurrent.ScheduleExecutorService是一種安排任務執行的ExecutorService,任務能夠延遲執行,或者在一個固定的時間間隔內重複執行。任務經過工做線程而且不能被正在處理任務的線程異步執行,。java

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

首先建立一個帶有5個線程的SechuleExecutorService。而後將Callbale接口的匿名實例類被傳遞給schedule()方法。最後兩個參數指定了Callable將在5秒後執行。jvm

ScheduledExecutorService的具體實現

ScheduledExecutorService是一個接口類,java.util.concurrent包中有如下關於此接口的實現類:spa

  • ScheduledThreadPoolExecutor

ScheduledExecutorService使用說明

一旦你建立了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)

這個方法將在給定的延遲時間後執行Callable。code

這個方法返回ScheduledFuture,你能夠在任務執行以前使用它來取消任務,若是任務已經執行也能夠經過它來獲得執行結果。htm

下面是一個例子:繼承

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

例子輸出結果:

Executed!
result = Called!
  • 1
  • 2
  • schedule (Runnable task, long delay, TimeUnit timeunit)

這個方法相似於用Callable做爲參數的版本(上面的方法),ScheduledFuture.get()方法在任務執行完畢時返回null。

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

這個方法能夠週期性的執行任務。任務在initialDelay時間後第一次執行,而後每次週期循環執行。

若是執行的任務拋出了異常,任務不會再執行了,若是沒有拋出異常,任務將繼續執行直到 ScheduledExecutorService 關閉。

若是任務執行時間超過了任務之間間隔的時間,下個任務將會在當前任務完成後再執行。執行任務的線程每次不會超過一個。

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

這個方法與scheduleAtFixedRate()方法相似,只是對period理解是不一樣的。

scheduleAtFixedRate()方法的period指的是上一個任務開始執行到下一個任務開始執行的時間間隔。

然而,這個方法的period指的是上一個任務執行完到下一個任務開始執行之間的時間間隔。

ScheduledExecutorService Shutdown

就像ExecutorService,ScheduleExecutorService在使用完以後須要關閉同樣。若是不這樣作,jvm將會一直在運行。儘管全部其餘的線程都被關閉了。

關閉 ScheduleExecutorService使用shutdown()和shutdownNow()方法,這個兩個方法繼承自ExecutorService接口。 查看ExecutorService Shutdown部分了解更多。

相關文章
相關標籤/搜索