After research on various multi threading frameworks for past 3 months , I have found answer to question.
ExecutorService
It is simple and easy to use with limited control. You can use it
- To start parallel independent tasks with out Waiting
- Wait for completion of all your tasks
I prefer this one when number of Callable/Runnable
tasks are small in number and piling of tasks in unbounded queue does not cause pile-up in memory & degrade the performance of the system.
It hides low level details of ThreadPoolExecutor
. It does not allow playing with other parameters ( Bounded Queue, Rejection Handler
etc. to fine tune the performance) as in ThreadPoolExectuor
.
ThreadPoolExecutor
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)
It provides more control to you. Apart from setting minimum and maximum threads, you can set queue size and make BlockingQueue
is bounded.
You can come up with your own thread factory if you need below features
- To set a more descriptive thread name
- To set thread daemon status
- To set thread priority
If your application is constrained by number of pending Runnable/Callable tasks, you will use bounded queue by setting the max capacity. Once the queue reaches maximum capacity, you can define RejectionHandler. Java provides four types of Rejection Handler policies.
In the default ThreadPoolExecutor.AbortPolicy
, the handler throws a runtime RejectedExecutionException upon rejection.
In 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.
In ThreadPoolExecutor.DiscardPolicy
, a task that cannot be executed is simply dropped.
In 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.)
CountDownLatch
CountDownLatch
: This framework allows a java thread to wait until other set of threads completes their tasks.
Use cases:
Achieving Maximum Parallelism: Sometimes we want to start a number of threads at the same time to achieve maximum parallelism
Wait for N threads to complete before start of executing other code block
Deadlock detection.
More details are listed in this article
ForkJoinPool
The ForkJoinPool
is similar to the Java 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.
public ForkJoinPool(int parallelism,
ForkJoinPool.ForkJoinWorkerThreadFactory factory,
Thread.UncaughtExceptionHandler handler,
boolean asyncMode)
Creates a ForkJoinPool with the given parameters.
Parameters:
parallelism - the parallelism level. For default value, use Runtime.availableProcessors()
.
factory - the factory for creating new threads. For default value, use
defaultForkJoinWorkerThreadFactory.
handler - the handler for internal worker threads that terminate due to unrecoverable errors
asyncMode - if true, establishes local first-in-first-out scheduling mode
for forked tasks that are never joined.
Regarding main query:
You can use ExecutorService.invokeAll()
or CountDownLatch
framework or ForkJoinPool
. All these frameworks are complimentary to each other varying of granularity to control the execution of tasks from high level to low level.
EDIT:
Have a look at related SE questions:
What are the advantages of using an ExecutorService?
Java's Fork/Join vs ExecutorService - when to use which?