Я создал подкласс ThreadPoolExecutor и переопределил afterExecute метод, который должен обеспечивать любые неперехваченные исключения, возникающие во время выполнения задачи. Однако, похоже, у меня ничего не получается.
Например:
Код: Выделить всё
public class ThreadPoolErrors extends ThreadPoolExecutor {
public ThreadPoolErrors() {
super( 1, // core threads
1, // max threads
1, // timeout
TimeUnit.MINUTES, // timeout units
new LinkedBlockingQueue() // 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();
}
}
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/224 ... vice-tasks