How to catch and handle exceptions in Java ExecutorService

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(); // let assume that task implements Runnable try { executor.execute(task); } catch(Exception ex) { // record the failed task, so that it can be re-added to the queue } 

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?

+5
source share
1 answer

you can run all your tasks in one loop and check / wait / repeat in another:

 Map<Future<?>, Task> futures = new HashMap<Future<?>, Task>() while(!taskQueue.isEmpty()){ Task task = taskQueue.poll(); Future<?> future = executor.submit(task); futures.put(future, task); } for(Map.Entry<Future<?>, Task> entry : futures.entrySet()){ try { entry.getKey().get(); } catch(ExecutionException ex) { // record the failed task, so that it can be re-added to the queue // you should add a retry counter because you want to prevent endless loops taskQueue.add(entry.getValue()); } catch(InterrupredException ex){ // thread interrupted, exit Thread.interrupt(); return; } } 

HTH, Mark

+2
source

All Articles