JavaTM 2 Platform
Standard Ed. 5.0

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

既知のサブインタフェースの一覧:
ExecutorService, ScheduledExecutorService
既知の実装クラスの一覧:
AbstractExecutorService, ScheduledThreadPoolExecutor, ThreadPoolExecutor

public interface Executor

送信された Runnable タスクを実行するオブジェクト。このインタフェースは、タスク送信を各タスクの実行方式 (スレッドの使用やスケジューリングの詳細などを含む) から分離する方法を提供します。通常、Executor は、明示的にスレッドを作成する代わりに使用されます。たとえば、各タスクセットごとに new Thread(new(RunnableTask())).start() を呼び出す代わりに、以下を使用できます。


Executor executor = anExecutor;
executor.execute(new RunnableTask1());
executor.execute(new RunnableTask2());
...
ただし、Executor インタフェースでは、実行が非同期であることが厳密に求められるわけではありません。もっとも単純なケースでは、executor は、送信されたタスクを呼び出し側のスレッド内でただちに実行できます。

class DirectExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
}
より一般的には、タスクは呼び出し側のスレッド以外のスレッドで実行されます。以下に示す executor は、各タスク用の新規スレッドを生成します。

class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
new Thread(r).start();
}
}
多数の Executor 実装は、タスクをスケジュールする方法および時期に関して何らかの制限を課します。以下に、executor がタスクの送信を直列化して 2 番目の executor に渡す、複合 executor を示します。

class SerialExecutor implements Executor {
final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
final Executor executor;
Runnable active;

SerialExecutor(Executor executor) {
this.executor = executor;
}

public synchronized void execute(final Runnable r) {
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (active == null) {
scheduleNext();
}
}

protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
executor.execute(active);
}
}
}
このパッケージで提供される Executor 実装は、より拡張性の高い ExecutorService を実装します。ThreadPoolExecutor クラスは、拡張可能なスレッドプール実装を提供します。Executors クラスは、これらの Executor 用の利便性の高いファクトリメソッドを提供します。

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

メソッドの概要
 void execute(Runnable command)
          将来のある時点で指定されたコマンドを実行します。
 

メソッドの詳細

execute

void execute(Runnable command)
将来のある時点で指定されたコマンドを実行します。コマンドは、新規スレッド内でも、プールされたスレッド内でも、呼び出し側のスレッド内でも、Executor 実装により随意に実行できます。

パラメータ:
command - 実行可能なタスク
例外:
RejectedExecutionException - タスクの実行を受け入れることができない場合
NullPointerException - コマンドが null の場合

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 も参照してください。