How to notice an exception from the parent thread?
exception, executorservice, java, multithreading
Solution
When you `submit()` a task on the `ExecutorService` you get a Future as a result. When the execution has finished you can call `get()` on that future. This will return the result if applicable, or it will throw an `ExecutionException` if the original task threw one. If you want the real exception object you can do `getCause()`.
Also note that you would be putting a `Task` back into the service, that task is ran on a `Thread` which has not really terminated (just caught the exception and is waiting for a new one).
Here is an example usage (you can use `Runnable` if you don't care for the result).
Callable<String> myCallable = ...;
Future<String> future = myExector.submit(myCallable);
// Do something else until myCallable.isDone() returns true.
try {
String result = future.get();
}catch(ExecutionException e){
// Handle error, perhaps create new Callable to submit.
}
Problem
I'm feeding threads into an `ExecutorService`. These threads are manipulating some data, and if there's a conflict, the data object throws an exception, which is caught by the conflicting thread, which in turn aborts and does not complete execution. When this happens, the aborting thread needs to be put back in the queue and fed back into the executor. How can I tell if an exception was thrown, from the parent thread?