Why do we need Thread pool ?
Thread pools are useful when you need to limit the number of threads running at the same time.
Also there is a performance overhead associated with creation of a thread and if there is frequent creation of threads in your application. It is better to use Thread pool instead of creating a new thread every time.
Before we start implementing the code. Lets think about what we need to implement Thread pool.
1. Pool of threads - list of threads which can execute a task.
2. Queue - a queue which can put the tasks in queue and thread can pick it up to execute the task.
3. Task - a interface to all tasks and have a method execute which will be called by a thread to complete the task.
It just nothing but a typical producer - consumer problem with Tasks (given by application) are producers and threads( in a pool) are consumers and resource being the queue.
Notes:
Queue should be a blocked queue either Java provided or own implemented, which means it should make the thread in wait. So finally Thread should be in wait status if the queue is empty rather than looping forever.
Also thread should be completed/destroyed if ThreadPool is destroyed so that it should not hold any reasources
Sample Code:
Thread in a pool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/PoolThread.java
Task - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/Task.java
ThreadPool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/ThreadPool.java
Thread pools are useful when you need to limit the number of threads running at the same time.
Also there is a performance overhead associated with creation of a thread and if there is frequent creation of threads in your application. It is better to use Thread pool instead of creating a new thread every time.
Before we start implementing the code. Lets think about what we need to implement Thread pool.
1. Pool of threads - list of threads which can execute a task.
2. Queue - a queue which can put the tasks in queue and thread can pick it up to execute the task.
3. Task - a interface to all tasks and have a method execute which will be called by a thread to complete the task.
It just nothing but a typical producer - consumer problem with Tasks (given by application) are producers and threads( in a pool) are consumers and resource being the queue.
Notes:
Queue should be a blocked queue either Java provided or own implemented, which means it should make the thread in wait. So finally Thread should be in wait status if the queue is empty rather than looping forever.
Also thread should be completed/destroyed if ThreadPool is destroyed so that it should not hold any reasources
Sample Code:
Thread in a pool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/PoolThread.java
Task - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/Task.java
ThreadPool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/ThreadPool.java