I have some code that makes heavy use of a thread pool, which I use by creating Collection<Callable<T>> tasks
, and calling ExecutorService.invokeAll(tasks)
.
for (Future<Object> future : threadPool.invokeAll(tasks)) {
Object object = future.get();
// Calling thread effectively blocks on invokeAll().
}
In my app, the size of tasks
varies a lot. In fact, most of the time the ExecutorService.invokeAll()
is called with a single task. The implementation of invokeAll()
that I'm using calls ThreadPoolExecutor.execute()
, whose implementation appears to always run the task in the thread pool (never in the calling thread).
In the case of a single task, would it be more efficient to call the task in the current thread rather than send it off to another thread?