JavaTM 2 Platform
Standard Ed. 5.0

java.util.concurrent
インタフェース ScheduledExecutorService

すべてのスーパーインタフェース:
Executor, ExecutorService
既知の実装クラスの一覧:
ScheduledThreadPoolExecutor

public interface ScheduledExecutorService
extends ExecutorService

指定された遅延時間後、または周期的にコマンドの実行をスケジュール可能な ExecutorService

schedule メソッドは、さまざまな遅延を持つタスクを作成し、実行の取り消しやチェックに使用可能なタスクオブジェクトを返します。scheduleAtFixedRate および scheduleWithFixedDelay メソッドは、取り消されるまで周期的に実行されるタスクを作成および実行します。

Executor.execute(java.lang.Runnable) および ExecutorService submit メソッドを使用して送信されるコマンドは、要求された遅延ゼロでスケジューリングされます。値がゼロおよび負の遅延 (ピリオドはなし) も schedule メソッドで許可および処理され、ただちに実行されます。

すべての schedule メソッドは、引数として、絶対時間や日付ではなく「相対的な」遅延および期間を受け入れます。Date で表される絶対時間を要求された形式に変換するのは簡単です。たとえば、将来の特定の date にスケジュールを設定する場合は、schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS) を使用できます。ただし、相対遅延の有効期限が現在の Date (この時点で、ネットワーク時刻同期プロトコル、クロックずれ、または他の要因のためにタスクが有効になる) に一致する必要はないことに留意してください。 Executors クラスは、このパッケージで提供される ScheduledExecutorService 実装用の利便性の高いファクトリメソッドを提供します。

使用例

次に示すクラスには、1 時間ビープ音を 10 秒ごとに鳴らすように 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);
    }
 }
 

導入されたバージョン:
1.5

メソッドの概要
<V> ScheduledFuture<V>
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)
          指定された初期遅延後に最初に有効になり、以降は指定された期間後に有効になる周期的なアクションを作成および実行します。
 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
 

メソッドの詳細

schedule

ScheduledFuture<?> schedule(Runnable command,
                            long delay,
                            TimeUnit unit)
指定された遅延後に有効になる単発的なアクションを作成および実行します。

パラメータ:
command - 実行するタスク
delay - 現在からの実行遅延時間
unit - 遅延パラメータの時間単位
戻り値:
保留状態のタスク完了を表す Future。その get() メソッドは、完了時に null を返す
例外:
RejectedExecutionException - タスクの実行をスケジュールできない場合
NullPointerException - コマンドが null の場合

schedule

<V> ScheduledFuture<V> schedule(Callable<V> callable,
                                long delay,
                                TimeUnit unit)
指定された遅延後に有効になる ScheduledFuture を作成および実行します。

パラメータ:
callable - 実行する関数
delay - 現在からの実行遅延時間
unit - 遅延パラメータの時間単位
戻り値:
結果の抽出または取り消しに使用可能な ScheduledFuture
例外:
RejectedExecutionException - タスクの実行をスケジュールできない場合
NullPointerException - callable が null の場合

scheduleAtFixedRate

ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                       long initialDelay,
                                       long period,
                                       TimeUnit unit)
指定された初期遅延後に最初に有効になり、以降は指定された期間後に有効になる周期的なアクションを作成および実行します。つまり、initialDelay 後に実行が開始され、initialDelay+period 後、initialDelay + 2 * period 後、という具合に実行されてゆきます。いずれかのタスク実行が例外に遭遇すると、後続の実行は抑制されます。それ以外でタスクが終了するのは、executor が取り消されるか終了する場合だけです。

パラメータ:
command - 実行するタスク
initialDelay - 最初の実行の遅延時間
period - 後続の実行間隔
unit - initialDelay および period パラメータの時間単位
戻り値:
タスクの保留完了を表す Future。その get() メソッドは、取り消し時に例外をスローする
例外:
RejectedExecutionException - タスクの実行をスケジュールできない場合
NullPointerException - コマンドが null の場合
IllegalArgumentException - 期間がゼロ以下の場合

scheduleWithFixedDelay

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                          long initialDelay,
                                          long delay,
                                          TimeUnit unit)
指定された初期遅延後に最初に有効になり、以降は実行終了から次の実行開始までの指定遅延後に有効になる周期的なアクションを作成および実行します。いずれかのタスク実行が例外に遭遇すると、後続の実行は抑制されます。それ以外でタスクが終了するのは、executor が取り消されるか終了する場合だけです。

パラメータ:
command - 実行するタスク
initialDelay - 最初の実行の遅延時間
delay - 実行終了から次の実行開始までの遅延
unit - initialDelay および delay パラメータの時間単位
戻り値:
タスクの保留完了を表す Future。その get() メソッドは、取り消し時に例外をスローする
例外:
RejectedExecutionException - タスクの実行をスケジュールできない場合
NullPointerException - コマンドが null の場合
IllegalArgumentException - 遅延がゼロ以下の場合

JavaTM 2 Platform
Standard Ed. 5.0

バグの報告と機能のリクエスト
さらに詳しい API リファレンスおよび開発者ドキュメントについては、Java 2 SDK SE 開発者用ドキュメントを参照してください。開発者向けの詳細な解説、概念の概要、用語の定義、バグの回避策、およびコード実例が含まれています。

Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。