What are the types of thread pools in java. I need to implement a robust multi-threaded application that uses heavy computing, which thread pool should I use?
There are various thread pools in java:
Single Thread Artist: a pool of threads with only one thread. Thus, all assigned tasks will be performed sequentially. Method: Executors.newSingleThreadExecutor()
Executors.newSingleThreadExecutor()
Cached thread pool: A thread pool that creates so many threads that it needs to complete a task in parrallel. Old available threads will be reused for new tasks. If a thread is not used for 60 seconds, it will be terminated and removed from the pool. Method: Executors.newCachedThreadPool()
Executors.newCachedThreadPool()
Fixed thread pool: A thread pool with a fixed number of threads. If the thread is not available for the task, the task is queued, waiting for the end of another task. Method: Executors.newFixedThreadPool()
Executors.newFixedThreadPool()
Scheduled thread pool: A thread pool for planning a future task. Method: Executors.newScheduledThreadPool()
Executors.newScheduledThreadPool()
Single Thread Scheduled Pool: a thread pool with a single thread for scheduling a future task. Method: Executors.newSingleThreadScheduledExecutor()
Executors.newSingleThreadScheduledExecutor()
There are many types;)
There is, for example, ExecutorService . This is a "basic" implementation that allows you to submit tasks, etc. You probably want to use Executors to get a new one, since it has static factory methods for the most common scenarios.
ExecutorService
Executors
With Java 7, you also have ForkJoinPool .
ForkJoinPool
Also check out FutureTask , as it is a very convenient class for creating separate threads.
FutureTask
See Executors .
Each generic ExecutorService explained, and you are likely to find one that suits your needs.
You can read more about ThreadPoolExecutors here: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
However, it would be nice to take a look at the ForkJoinTask API: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinTask.html
It shows good animations in different concurrency constructs, maybe this will help you choose
http://sourceforge.net/projects/javaconcurrenta/