ExecutorService.invokeAll does NOT support task collection

You want to run the Runnable task collection using the invokeAll (..) ExecutorService method. But this is not supported at the moment ( only collects only tasks to be set )

Any specific reason for this? What an alternative to do something like this.

+6
source share
2 answers

Just convert runnables to callables:

List<Callable<Void>> callables = new ArrayList<>(); for (Runnable r : runnables) { callables.add(toCallable(r)); } executor.invokeAll(callables); private Callable<Void> toCallable(final Runnable runnable) { return new Callable<Void>() { @Override public Void call() { runnable.run(); return null; } }; } 
+8
source
 Runnable task = new Runnable() { public void run() { } }; Callable<Object> c = Executors.callable(task); 

It just turned out that Performers provides a utility method for converting Runnable into a Callable task. This explains why we do not have an overloaded invokeAll that also performs the Runnable task.

+15
source

Source: https://habr.com/ru/post/970447/


All Articles