I am trying to figure out a way to handle exceptions in multi-threaded setup. I would like to perform certain tasks in parallel, each of which can throw an exception from which I need to respond (basically, returning an unsuccessful task to the execution queue). However, it seems that the only way to get an exception from the stream is to create the Future and call its get () method. However, this substantially turns calls into synchronous calls.
Maybe some code will illustrate the point:
ExecutorService executor = Executors.newFixedThreadPool(nThreads); Task task = taskQueue.poll();
However, in this case, all tasks are started, but the exceptions do not seem to fall into this catch block.
An alternative could be to use the Future instead of the stream and get its result:
try { Future<?> future = executor.submit(task); future.get(); } ...
In this case, the exceptions are blocked in the catch block, but at the price awaiting the completion of this operation. Thus, tasks are performed sequentially, and not in parallel, as desired.
What am I missing? How can each Exception task be captured and responded to?
source share