package java.util.concurrent; public interface ScheduledExecutorService extends ExecutorService { //建立並執行在給定延遲後啓用的一次性操做 public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit); //建立並執行在給定延遲後啓用的 ScheduledFuture public <V> ScheduledFuture<V> schedule(Callable<V> callable,long delay, TimeUnit unit); //建立並執行一個在給定初始延遲後首次啓用的按期操做,後續操做具備給定的週期 //在initialDelay後開始執行,而後以後每隔period執行一次 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit); //建立並執行一個在給定初始延遲後首次啓用的按期操做,隨後在每一次執行終止和下一次執行開始之間都存在給定的延遲 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit); }
全部超級接口:html
Executor, ExecutorServicejava
全部已知實現類:api
schedule 方法使用各類延遲建立任務,並返回一個可用於取消或檢查執行的任務對象。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法建立並執行某些在取消前一直按期運行的任務。spa
用 Executor.execute(java.lang.Runnable)
和 ExecutorService
的 submit 方法所提交的命令,經過所請求的 0 延遲進行安排。.net
schedule 方法中容許出現 0 和負數延遲(但不是週期),並將這些視爲一種當即執行的請求。線程
全部的 schedule 方法都接受相對 延遲和週期做爲參數,而不是絕對的時間或日期。將以 Date
所表示的絕對時間轉換成要求的形式很容易。code
例如,要安排在某個之後的 Date 運行,可使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。htm
注意,因爲網絡時間同步協議、時鐘漂移或其餘因素的存在,所以相對延遲的期滿日期沒必要與啓用任務的當前 Date 相符。 對象
Executors
類爲此包中所提供的 ScheduledExecutorService 實現提供了便捷的工廠方法。
如下是一個帶方法的類,它設置了 ScheduledExecutorService ,在 1 小時內每 10 秒鐘蜂鳴一次:
package com.thread; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import static java.util.concurrent.TimeUnit.SECONDS; class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);//建立一個ScheduledFuture 對象實例 scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true);//試圖取消對此任務的執行。若是任務已完成、或已取消,或者因爲某些其餘緣由而沒法取消,則此嘗試將失敗 } }, 60 * 60, SECONDS); } }
從接口 java.util.concurrent.ExecutorService 繼承的方法
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
從接口 java.util.concurrent.Executor 繼承的方法
execute
ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit)
建立並執行在給定延遲後啓用的一次性操做。
參數:
command
- 要執行的任務
delay
- 從如今開始延遲執行的時間
unit
- 延遲參數的時間單位
返回:
表示掛起任務完成的 ScheduledFuture,而且其 get() 方法在完成後將返回 null
拋出:
RejectedExecutionException
- 若是沒法安排執行該任務
NullPointerException
- 若是 command 爲 null
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
建立並執行在給定延遲後啓用的 ScheduledFuture。
參數:
callable
- 要執行的功能
delay
- 從如今開始延遲執行的時間
unit
- 延遲參數的時間單位
返回:
可用於提取結果或取消的 ScheduledFuture
拋出:
RejectedExecutionException
- 若是沒法安排執行該任務
NullPointerException
- 若是 callable 爲 null
ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit)
建立並執行一個在給定初始延遲後首次啓用的按期操做,後續操做具備給定的週期;也就是將在 initialDelay 後開始執行,而後在 initialDelay+period 後執行,接着在 initialDelay + 2 * period 後執行,依此類推。若是任務的任何一個執行遇到異常,則後續執行都會被取消。不然,只能經過執行程序的取消或終止方法來終止該任務。若是此任務的任何一個執行要花費比其週期更長的時間,則將推遲後續執行,但不會同時執行。
參數:
command
- 要執行的任務
initialDelay
- 首次執行的延遲時間
period
- 連續執行之間的週期
unit
- initialDelay 和 period 參數的時間單位
返回:
表示掛起任務完成的 ScheduledFuture,而且其 get() 方法在取消後將拋出異常
拋出:
RejectedExecutionException
- 若是沒法安排執行該任務
NullPointerException
- 若是 command 爲 null
IllegalArgumentException
- 若是 period 小於等於 0
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit)
建立並執行一個在給定初始延遲後首次啓用的按期操做,隨後,在每一次執行終止和下一次執行開始之間都存在給定的延遲。若是任務的任一執行遇到異常,就會取消後續執行。不然,只能經過執行程序的取消或終止方法來終止該任務。
參數:
command
- 要執行的任務
initialDelay
- 首次執行的延遲時間
delay
- 一次執行終止和下一次執行開始之間的延遲
unit
- initialDelay 和 delay 參數的時間單位
返回:
表示掛起任務完成的 ScheduledFuture,而且其 get() 方法在取消後將拋出異常
拋出:
RejectedExecutionException
- 若是沒法安排執行該任務
NullPointerException
- 若是 command 爲 null。
IllegalArgumentException
- 若是 delay 小於等於 0