Thread pools in Java
A thread pool manages the pool of worker threads, it contains a queue that keeps tasks waiting to get executed.The basic idea is as follows:
- whenever we want a job performing, instead of creating a thread for it directly, we put it on to a queue;
- a (fairly) fixed set of long-running threads each sit in a loop pulling jobs off the queue and running them.
- a thread pool implementation is provided in the form of the ThreadPoolExecutor class, part of the Java concurrency framework introduced in Java 5;
- you can plug in different implementations of BlockingQueue in order to specifiy different queue behaviour such as queue bounds or priority ordering
- Single Thread Executor : A thread pool with only one thread. So all the submitted tasks will be executed sequentially. Method : Executors.newSingleThreadExecutor()
- Cached Thread Pool : A thread pool that creates as many threads it needs to execute the task in parrallel. The old available threads will be reused for the new tasks. If a thread is not used during 60 seconds, it will be terminated and removed from the pool. Method : Executors.newCachedThreadPool()
- Fixed Thread Pool : A thread pool with a fixed number of threads. If a thread is not available for the task, the task is put in queue waiting for an other task to ends. Method : Executors.newFixedThreadPool()
- Scheduled Thread Pool : A thread pool made to schedule future task. Method : Executors.newScheduledThreadPool()
- Single Thread Scheduled Pool : A thread pool with only one thread to schedule future task. Method : Executors.newSingleThreadScheduledExecutor()
Let’s write a simple program to explain it’s working.
First we need to have a Runnable class.
package com.tutorialsdesk.threads; public class MyRunnable implements Runnable { private String command; public MyRunnable(String s){ this.command=s; } @Override public void run() { System.out.println(Thread.currentThread().getName()+" Start. Thread from Pool = "+command); processCommand(); System.out.println(Thread.currentThread().getName()+" End."); } private void processCommand() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString(){ return this.command; } }
Now you run your runnables with the executor framework.
package com.tutorialsdesk.threads; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SimpleThreadPool { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { Runnable myRunnable = new MyRunnable("" + i); executor.execute(myRunnable); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); } }
In above program, we are creating fixed size thread pool of 5 worker threads. Then we are submitting 10 jobs to this pool, since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state, as soon as one of the job is finished, another job from the wait queue will be picked up by worker thread and get’s executed. Here is the output of the above program.
pool-1-thread-1 Start. Thread from Pool = 0 pool-1-thread-3 Start. Thread from Pool = 2 pool-1-thread-2 Start. Thread from Pool = 1 pool-1-thread-1 End. pool-1-thread-1 Start. Thread from Pool = 3 pool-1-thread-3 End. pool-1-thread-3 Start. Thread from Pool = 4 pool-1-thread-2 End. pool-1-thread-2 Start. Thread from Pool = 5 pool-1-thread-1 End. pool-1-thread-1 Start. Thread from Pool = 6 pool-1-thread-3 End. pool-1-thread-3 Start. Thread from Pool = 7 pool-1-thread-2 End. pool-1-thread-2 Start. Thread from Pool = 8 pool-1-thread-1 End. pool-1-thread-1 Start. Thread from Pool = 9 pool-1-thread-3 End. pool-1-thread-2 End. pool-1-thread-1 End. Finished all threads
The output confirms that there are five threads in the pool named from “pool-1-thread-1? to “pool-1-thread-5? and they are responsible to execute the submitted tasks to the pool.
NEXT READ RejectedExecutionHandler Thread Pool Executor in Java.
Hope we are able to explain you Importance of Thread Pool Executor in Java, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).
Please share us on social media if you like the tutorial.
Blogger Comment
Facebook Comment