ThreadPoolExecutor#afterExecute()
メソッドでワーカースレッドによってスローされた例外を処理したい。私は、コードを実行した場合、私は出力を得るThreadPoolExecutorのafterExecute()で例外がnullになるのはなぜですか?
public class MyExecutor extends ThreadPoolExecutor {
public static void main(String[] args) {
MyExecutor threadPool = new MyExecutor();
Task<Object> task = new Task<>();
threadPool.submit(task);
}
public MyExecutor() {
super(4, 20, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(4000));
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
System.out.println("in afterExecute()");
if (t != null) {
System.out.println("exception thrown: " + t.getMessage());
} else {
System.out.println("t == null");
}
}
private static class Task<V> implements Callable<V> {
@Override
public V call() throws Exception {
System.out.println("in call()");
throw new SQLException("testing..");
}
}
}
:
in call()
in afterExecute()
t == null
なぜafterExecute()
におけるパラメータThrowable t
null
ですが現在、私はこのコードを持っていますか? SQLException
インスタンスであってはなりませんか?
ありがとうございます。だから、チェックされた例外はすべてThreadPoolExecutorによって飲み込まれるだけですか?すべての例外処理はCallable#call()で行わなければなりませんか? –
@ potato300私の編集を参照してください、特定の例で何か他のことが起こっています(私が最初に気付かなかったもの) – Tunaki