ThreadPoolExecutor
Javaのクラスを使用して、固定数のスレッドで多数の重いタスクを実行しようとしています。各タスクには、例外が原因で失敗する可能性のある多くの場所があります。
私はサブクラス化し、タスクの実行中に発生したキャッチされない例外を提供することになっているメソッドをThreadPoolExecutor
オーバーライドしました。afterExecute
しかし、私はそれを機能させることができないようです。
例えば:
public class ThreadPoolErrors extends ThreadPoolExecutor {
public ThreadPoolErrors() {
super( 1, // core threads
1, // max threads
1, // timeout
TimeUnit.MINUTES, // timeout units
new LinkedBlockingQueue<Runnable>() // work queue
);
}
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(t != null) {
System.out.println("Got an error: " + t);
} else {
System.out.println("Everything's fine--situation normal!");
}
}
public static void main( String [] args) {
ThreadPoolErrors threadPool = new ThreadPoolErrors();
threadPool.submit(
new Runnable() {
public void run() {
throw new RuntimeException("Ouch! Got an error.");
}
}
);
threadPool.shutdown();
}
}
このプログラムからの出力は、「すべてが順調です-状況は正常です!」です。スレッドプールに送信された唯一のRunnableが例外をスローしたとしても。ここで何が起こっているのかについての手がかりはありますか?
ありがとう!