| 
 | JavaTM 2 Platform Standard Ed. 6 | |||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
public interface ScheduledExecutorService
一個 ExecutorService,可安排在給定的延遲後運行或定期執行的命令。 
schedule 方法使用各種延遲創建任務,並返回一個可用於取消或檢查執行的任務物件。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法創建並執行某些在取消前一直定期運行的任務。
 用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,通過所請求的 0 延遲進行安排。schedule 方法中允許出現 0 和負數延遲(但不是週期),並將這些視為一種立即執行的請求。
所有的 schedule 方法都接受相對 延遲和週期作為參數,而不是絕對的時間或日期。將以 Date 所表示的絕對時間轉換成要求的形式很容易。例如,要安排在某個以後的 Date 運行,可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。但是要注意,由於網路時間同步協議、時鐘漂移或其他因素的存在,因此相對延遲的期滿日期不必與啟用任務的當前 Date 相符。
Executors 類別為此套件中所提供的 ScheduledExecutorService 實作提供了便捷的處理器方法。
 import static java.util.concurrent.TimeUnit.*;
 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);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }
 
| 方法摘要 | ||
|---|---|---|
| 
 | schedule(Callable<V> callable,
         long delay,
         TimeUnit unit)創建並執行在給定延遲後啟用的 ScheduledFuture。 | |
|  ScheduledFuture<?> | schedule(Runnable command,
         long delay,
         TimeUnit unit)創建並執行在給定延遲後啟用的一次性操作。 | |
|  ScheduledFuture<?> | scheduleAtFixedRate(Runnable command,
                    long initialDelay,
                    long period,
                    TimeUnit unit)創建並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的週期;也就是將在 initialDelay 後開始執行,然後在 initialDelay+period 後執行,接著在 initialDelay + 2 * period 後執行,依此類別推。 | |
|  ScheduledFuture<?> | scheduleWithFixedDelay(Runnable command,
                       long initialDelay,
                       long delay,
                       TimeUnit unit)創建並執行一個在給定初始延遲後首次啟用的定期操作,隨後,在每一次執行終止和下一次執行開始之間都存在給定的延遲。 | |
| 從介面 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 - 延遲參數的時間單位
RejectedExecutionException - 如果無法安排執行該任務
NullPointerException - 如果 command 為 null
<V> ScheduledFuture<V> schedule(Callable<V> callable,
                                long delay,
                                TimeUnit unit)
callable - 要執行的功能delay - 從現在開始延遲執行的時間unit - 延遲參數的時間單位
RejectedExecutionException - 如果無法安排執行該任務
NullPointerException - 如果 callable 為 null
ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                       long initialDelay,
                                       long period,
                                       TimeUnit unit)
command - 要執行的任務initialDelay - 首次執行的延遲時間period - 連續執行之間的週期unit - initialDelay 和 period 參數的時間單位
RejectedExecutionException - 如果無法安排執行該任務
NullPointerException - 如果 command 為 null
IllegalArgumentException - 如果 period 小於等於 0
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                          long initialDelay,
                                          long delay,
                                          TimeUnit unit)
command - 要執行的任務initialDelay - 首次執行的延遲時間delay - 一次執行終止和下一次執行開始之間的延遲unit - initialDelay 和 delay 參數的時間單位
RejectedExecutionException - 如果無法安排執行該任務
NullPointerException - 如果 command 為 null。
IllegalArgumentException - 如果 delay 小於等於 0| 
 | JavaTM 2 Platform Standard Ed. 6 | |||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。