In what cases does Future.get() throw ExecutionException or InterruptedException

ExecutionException and InterruptedException are two very different things. ExecutionException wraps whatever exception the thread being executed threw, so if your thread was, for instance, doing some kind of IO that caused an IOException to get thrown, that would get wrapped in an ExecutionException and rethrown. An InterruptedException is not a sign of anything having gone … Read more

Elegantly implementing queue length indicators to ExecutorServices

There is a more direct way: ThreadPoolExecutor executor = new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>() ); // add jobs // … int size = executor.getQueue().size(); This is directly copied from Executors.newSingleThreadExecutor in JDK 1.6. The LinkedBlockingQueue that is passed to the constructor is actually the very object that you will get back from … Read more

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

Work stealing is a technique used by modern thread-pools in order to decrease contention on the work queue. A classical threadpool has one queue, and each thread-pool-thread locks the queue, dequeue a task and then unlocks the queue. If the tasks are short and there are many of them, there is a lot of contention … Read more

ExecutorCompletionService? Why do need one if we have invokeAll?

Using a ExecutorCompletionService.poll/take, you are receiving the Futures as they finish, in completion order (more or less). Using ExecutorService.invokeAll, you do not have this power; you either block until are all completed, or you specify a timeout after which the incomplete are cancelled. static class SleepingCallable implements Callable<String> { final String name; final long period; … Read more

How to shutdown an ExecutorService?

The typical pattern is: executorService.shutdownNow(); executorService.awaitTermination(); When calling shutdownNow, the executor will (generally) try to interrupt the threads that it manages. To make the shutdown graceful, you need to catch the interrupted exception in the threads or check the interrupted status. If you don’t your threads will run forever and your executor will never be … Read more

What are the advantages of using an ExecutorService?

ExecutorService abstracts away many of the complexities associated with the lower-level abstractions like raw Thread. It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed as Runnable or Callable). From JCiP, Section 6.2, straight from the horse’s mouth: Executor may be a simple interface, … Read more

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

Here is the source of Executors.newFixedThreadPool: public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } It internally uses ThreadPoolExecutor class with default configuration as you can see above. Now there are scenarios where default configuration is not suitable say instead of LinkedBlockingQueue a priority queue needs to be used … Read more

tech