ExecutorService executor = Executors.newFixedThreadPool(50);
It is simple and easy to use. It hides low level details of
I 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, I prefer to use
ThreadPoolExecutor with capacity constraints &
RejectedExecutionHandler to handle rejection of tasks.
CountDownLatch will be initialized with a given count. This count is decremented by calls to the
countDown() method. Threads waiting for this count to reach zero can call one of the
await() methods. Calling
await() blocks the thread until the count reaches zero. This class enables a java thread to wait until other set of threads completes their tasks.
Achieving Maximum Parallelism: Sometimes we want to start a number of threads at the same time to achieve maximum parallelism
Wait N threads to completes before start execution
ThreadPoolExecutor : It provides more control. If application is constrained by number of pending Runnable/Callable tasks, you can use bounded queue by setting the max capacity. Once the queue reaches maximum capacity, you can define RejectionHandler. Java provides four types of
ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
ThreadPoolExecutor.CallerRunsPolicy`, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)
If you want to simulate
CountDownLatch behaviour, you can use
One more mechanism you did not quote is ForkJoinPool
ForkJoinPool was added to Java in Java 7. The
ForkJoinPool is similar to
ExecutorService but with one difference. The
ForkJoinPool makes it
easy for tasks to split their work up into smaller tasks which are then
submitted to the
ForkJoinPool too. Task stealing happens in
ForkJoinPool when free worker threads steal tasks from busy worker thread queue.
Java 8 has introduced one more API in ExecutorService to create work stealing pool. You don't have to create
RecursiveAction but still can use
public static ExecutorService newWorkStealingPool()
Creates a work-stealing thread pool using all available processors as its target parallelism level.
By default, it will take number of CPU cores as parameter.
All these four mechanism are complimentary to each other. Depending on level of granularity you want to control, you have to chose right ones.