Server applications are one of the most common use cases in Java. One aspect of server applications is to handle incoming requests and process them. This seems simple to implement but it has its own disadvantages.
Since we are creating a new thread for each incoming request, the system would consume more time and resources in creating and destroying threads. This may also lead to the infamous OutOfMemoryError.
To deal with such problems, Java provides the concept of Thread Pools. Let us understand this in detail in the below section.
Since we are creating a new thread for each incoming request, the system would consume more time and resources in creating and destroying threads. This may also lead to the infamous OutOfMemoryError.
To deal with such problems, Java provides the concept of Thread Pools. Let us understand this in detail in the below section.
Thread Pools
When we need to limit the number of threads running in our application, the thread pool proves helpful. There is a performance overhead in starting a new thread and each thread is allocation some stack memory.
Instead of starting a new thread for executing a task concurrently, we can pass the task to a thread pool. As soon as the thread pool has any idle thread, the task is assigned to it and the thread executes the desired task.
Thread Pool implements a Blocking Queue internally in which the tasks are inserted initially. The idle threads in the pool deque these tasks one by one to execute. The rest of the idle threads in the queue will be blocked waiting to deque tasks. When any thread completes its execution, it can pick up a new task to execute. When all tasks are completed, the threads remain active and wait for more tasks in the queue.
Thread Pool Executor
Since Java 5, we have the Executor framework. This revolves around the Executor interface, its subinterface ExecutorService and ThreadPoolExecutor class which implements both interfaces.
ThreadPoolExecutor separates the task creation and its execution. Using this, we only have to implement Runnable objects and send them to the executor. The executor is responsible for their creation, execution, instantiation etc.
How to create ThreadPoolExecutor
We can create it in five ways -
- Fixed thread pool executor - This creates a thread pool executor with a fixed number of threads. These threads can execute any number of tasks. If all threads are active and additional tasks are submitted, then they have to wait in the queue until a thread is available.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
- Cached thread pool executor - This can create new threads as needed but will reuse previously created threads when they are idle. We should use this very carefully as in long-running tasks it can bring the system down.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
- Scheduled thread pool executor - This can schedule the tasks to run periodically.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(10);
- Single thread pool executor - Creates only one thread to execute tasks.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newSingleThreadExecutor();
- Work stealing thread pool executor - Creates a thread pool that maintains enough threads to support the given parallelism level. Here parallelism level means the maximum number of threads which will be used to execute a given task, at a single point of time, in multi-processor machines.
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newWorkStealingPool(4);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(10);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newSingleThreadExecutor();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newWorkStealingPool(4);
Code Examples
Below are code examples to see concepts in action -
- ExecutorService example
The output of this program will be - Finished all threads
pool-1-thread-1 - Start. Command - 0
Command is processing...
pool-1-thread-2 - Start. Command - 1
Command is processing...
pool-1-thread-3 - Start. Command - 2
Command is processing...
pool-1-thread-3 End.
pool-1-thread-3 - Start. Command - 3
Command is processing...
pool-1-thread-1 End.
pool-1-thread-2 End.
pool-1-thread-1 - Start. Command - 4
Command is processing...
pool-1-thread-3 End.
pool-1-thread-1 End.
- ThreadPoolExecutor example
The output of this program will be - Created: Task - 0
Created: Task - 1
Created: Task - 2
Created: Task - 3
Created: Task - 4
Executing: Task - 0
Executing: Task - 1
Executing: Task - 2
Executing: Task - 3
Executing: Task - 4
Conclusion
Congratulations!! 🙋 today we discussed the concepts of Thread Pools, Executor Framework with their implementations. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub.Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like have suggestions from you to make it better.
Happy Coding 😊
Finished all threads pool-1-thread-1 - Start. Command - 0 Command is processing... pool-1-thread-2 - Start. Command - 1 Command is processing... pool-1-thread-3 - Start. Command - 2 Command is processing... pool-1-thread-3 End. pool-1-thread-3 - Start. Command - 3 Command is processing... pool-1-thread-1 End. pool-1-thread-2 End. pool-1-thread-1 - Start. Command - 4 Command is processing... pool-1-thread-3 End. pool-1-thread-1 End.
Created: Task - 0 Created: Task - 1 Created: Task - 2 Created: Task - 3 Created: Task - 4 Executing: Task - 0 Executing: Task - 1 Executing: Task - 2 Executing: Task - 3 Executing: Task - 4
Conclusion
Congratulations!! 🙋 today we discussed the concepts of Thread Pools, Executor Framework with their implementations. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub.Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like have suggestions from you to make it better.
Happy Coding 😊
Comments
Post a Comment