JavaTM Platform
Standard Ed. 6

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 - delay パラメータの時間単位
戻り値:
タスクの保留状態の完了を表す ScheduledFuture。その get() メソッドは完了時に null を返す
例外:
RejectedExecutionException - タスクの実行をスケジュールできない場合
NullPointerException - コマンドが null の場合

schedule

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

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

scheduleAtFixedRate

ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                       long initialDelay,
                                       long period,
                                       TimeUnit unit)
指定された初期遅延の経過後にはじめて有効になり、その後は指定された期間ごとに有効になる定期的なアクションを作成して実行します。つまり実行は initialDelay 後に開始され、その後は initialDelay+periodinitialDelay + 2 * period というようになります。タスクを実行して例外が発生すると、以降の実行は抑止されます。そうでない場合は、executor の取り消しまたは終了によってのみタスクは終了します。このタスクを実行するのに指定の期間 (period) より長い時間がかかる場合、以降の実行は遅れて開始されることがありますが、並行して実行はされません。

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

scheduleWithFixedDelay

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                          long initialDelay,
                                          long delay,
                                          TimeUnit unit)
指定された初期遅延の経過後にはじめて有効になり、その後は実行の終了後から次の開始までの指定の遅延ごとに有効になる定期的なアクションを作成して実行します。タスクを実行して例外が発生すると、以降の実行は抑止されます。そうでない場合は、executor の取り消しまたは終了によってのみタスクは終了します。

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

JavaTM Platform
Standard Ed. 6

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

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