Skip to main content

Threads in Java - Thread Pools (Part 10)

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.

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);

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. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊

Comments

Popular posts from this blog

Threads in Java - Masterclass (Part 0)

Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading.
One should exploit this feature, especially with all these multiple core machines nowadays.

Below are a few reasons why we should use multithreading -
1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a processor busy can be a tough task e…

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects -
making connectionscachingretrying failed requeststhreadingresponse parsingerror handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from https://timesofindia.indiatimes.com/rssfeedstopstories.cms using the Retrofit library.

To work with Retrofit, we need three classes -  Model class to map the JSON dataInterfaces which defines the possible HTTP operationsRetrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The response is returned as a Call object…

Material design profile page in Android

Hey everyone, some days back I was working on one my personal Android project. In that project, I was supposed to create a simple profile page for a user. This profile page was supposed to show some basic details of a user.

The output of this UI will be like this -
I created the profile page using material design and in this post, I am going to discuss a step by step tutorial to create a simple yet elegant profile page. Without further ado, let's get started.
Creating a new project Click on File ➤ New Project ➤ Empty Activity and fill the necessary details. Change styles.xml fileNavigate to app\src\main\res\values\styles.xmlChange the style value from DarkActionBar to NoActionBar as below<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark&qu…