|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||
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 ArrayDeque<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 クラスは、拡張可能なスレッドプール実装を提供します。Executor クラスは、これらの Executor 用の利便性の高いファクトリメソッドを提供します。
メモリー整合性効果:Runnable オブジェクトを Executor に送信する前のスレッド内のアクションは、別のスレッドで行われる可能性のある実行の開始よりも happen-before です。
| メソッドの概要 | |
|---|---|
void |
execute(Runnable command)
将来のどの時点かで、指定されたコマンドを実行します。 |
| メソッドの詳細 |
|---|
void execute(Runnable command)
command - 実行可能なタスク
RejectedExecutionException - タスクの実行を受け入れることができない場合
NullPointerException - コマンドが null の場合
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||
Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。