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?
When you
submit()a task on theExecutorServiceyou get a Future as a result. When the execution has finished you can callget()on that future. This will return the result if applicable, or it will throw anExecutionExceptionif the original task threw one. If you want the real exception object you can dogetCause().Also note that you would be putting a
Taskback into the service, that task is ran on aThreadwhich has not really terminated (just caught the exception and is waiting for a new one).Here is an example usage (you can use
Runnableif you don’t care for the result).