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?
Exceptions are caught by handlers(here catch block). Exceptions are caught by handlers positioned along with the thread's method invocation stack. If the calling method is not prepared to catch the exception, it throws the exception up to its calling method and so on.
Short answer, it doesn't. If the exception propagates all the way out of the thread, it'll simply die (possible generating some error print on the console).
An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.
By default, child thread value is exactly the same as parent thread value.
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With