Is ExecutorService (specifically ThreadPoolExecutor) thread safe?

(Contrary to other answers) the thread-safety contract is documented: look in the interface javadocs (as opposed to javadoc of methods). For example, at the bottom of the ExecutorService javadoc you find: Memory consistency effects: Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken … 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

How to properly use Java Executor?

ExecutorService ExecutorService executor=Executors.newFixedThreadPool(50); It is simple and easy to use. It hides low level details of ThreadPoolExecutor. Prefer this one when number of Callable/Runnable tasks are small in number and piling of tasks in unbounded queue does not increase memory & degrade the performance of the system. If you have CPU/Memory constraints, use ThreadPoolExecutor with … Read more

Choose between ExecutorService’s submit and ExecutorService’s execute

There is a difference concerning exception/error handling. A task queued with execute() that generates some Throwable will cause the UncaughtExceptionHandler for the Thread running the task to be invoked. The default UncaughtExceptionHandler, which typically prints the Throwable stack trace to System.err, will be invoked if no custom handler has been installed. On the other hand, … Read more

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, … Read more

How to properly shutdown java ExecutorService

Recommended way from Oracle API documentation page of ExecutorService: void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, … Read more

Handling exceptions from Java ExecutorService tasks

WARNING: It should be noted that this solution will block the calling thread. If you want to process exceptions thrown by the task, then it is generally better to use Callable rather than Runnable. Callable.call() is permitted to throw checked exceptions, and these get propagated back to the calling thread: Callable task = … Future … Read more