ThreadPoolExecutor – Java Thread Pool Example

Filed Under: Java
Thread Pool In Java

Java thread pool manages the pool of worker threads. It contains a queue that keeps tasks waiting to get executed. We can use ThreadPoolExecutor to create thread pool in Java.

Java thread pool manages the collection of Runnable threads. The worker threads execute Runnable threads from the queue. java.util.concurrent.Executors provide factory and support methods for java.util.concurrent.Executor interface to create the thread pool in java.

Executors is a utility class that also provides useful methods to work with ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes through various factory methods.

Let’s write a simple program to explain it’s working.

First, we need to have a Runnable class, named WorkerThread.java


package com.journaldev.threadpool;

public class WorkerThread implements Runnable {
  
    private String command;
    
    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }
}

ExecutorService Example

Here is the test program class SimpleThreadPool.java, where we are creating fixed thread pool from Executors framework.


package com.journaldev.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SimpleThreadPool {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    }
}

In the above program, we are creating a 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-2 Start. Command = 1
pool-1-thread-4 Start. Command = 3
pool-1-thread-1 Start. Command = 0
pool-1-thread-3 Start. Command = 2
pool-1-thread-5 Start. Command = 4
pool-1-thread-4 End.
pool-1-thread-5 End.
pool-1-thread-1 End.
pool-1-thread-3 End.
pool-1-thread-3 Start. Command = 8
pool-1-thread-2 End.
pool-1-thread-2 Start. Command = 9
pool-1-thread-1 Start. Command = 7
pool-1-thread-5 Start. Command = 6
pool-1-thread-4 Start. Command = 5
pool-1-thread-2 End.
pool-1-thread-4 End.
pool-1-thread-3 End.
pool-1-thread-5 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.

ThreadPoolExecutor Example

Executors class provide simple implementation of ExecutorService using ThreadPoolExecutor but ThreadPoolExecutor provides much more feature than that. We can specify the number of threads that will be alive when we create ThreadPoolExecutor instance and we can limit the size of thread pool and create our own RejectedExecutionHandler implementation to handle the jobs that can’t fit in the worker queue.

Here is our custom implementation of RejectedExecutionHandler interface.


package com.journaldev.threadpool;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        System.out.println(r.toString() + " is rejected");
    }

}

ThreadPoolExecutor provides several methods using which we can find out the current state of the executor, pool size, active thread count and task count. So I have a monitor thread that will print the executor information at a certain time interval.


package com.journaldev.threadpool;

import java.util.concurrent.ThreadPoolExecutor;

public class MyMonitorThread implements Runnable
{
    private ThreadPoolExecutor executor;
    private int seconds;
    private boolean run=true;

    public MyMonitorThread(ThreadPoolExecutor executor, int delay)
    {
        this.executor = executor;
        this.seconds=delay;
    }
    public void shutdown(){
        this.run=false;
    }
    @Override
    public void run()
    {
        while(run){
                System.out.println(
                    String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                        this.executor.getPoolSize(),
                        this.executor.getCorePoolSize(),
                        this.executor.getActiveCount(),
                        this.executor.getCompletedTaskCount(),
                        this.executor.getTaskCount(),
                        this.executor.isShutdown(),
                        this.executor.isTerminated()));
                try {
                    Thread.sleep(seconds*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
            
    }
}

Here is the thread pool implementation example using ThreadPoolExecutor.


package com.journaldev.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WorkerPool {

    public static void main(String args[]) throws InterruptedException{
        //RejectedExecutionHandler implementation
        RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
        //Get the ThreadFactory implementation to use
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //creating the ThreadPoolExecutor
        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory, rejectionHandler);
        //start the monitoring thread
        MyMonitorThread monitor = new MyMonitorThread(executorPool, 3);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        //submit work to the thread pool
        for(int i=0; i<10; i++){
            executorPool.execute(new WorkerThread("cmd"+i));
        }
        
        Thread.sleep(30000);
        //shut down the pool
        executorPool.shutdown();
        //shut down the monitor thread
        Thread.sleep(5000);
        monitor.shutdown();
        
    }
}

Notice that while initializing the ThreadPoolExecutor, we are keeping initial pool size as 2, maximum pool size to 4 and work queue size as 2. So if there are 4 running tasks and more tasks are submitted, the work queue will hold only 2 of them and the rest of them will be handled by RejectedExecutionHandlerImpl.

Here is the output of the above program that confirms the above statement.


pool-1-thread-1 Start. Command = cmd0
pool-1-thread-4 Start. Command = cmd5
cmd6 is rejected
pool-1-thread-3 Start. Command = cmd4
pool-1-thread-2 Start. Command = cmd1
cmd7 is rejected
cmd8 is rejected
cmd9 is rejected
[monitor] [0/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-4 End.
pool-1-thread-1 End.
pool-1-thread-2 End.
pool-1-thread-3 End.
pool-1-thread-1 Start. Command = cmd3
pool-1-thread-4 Start. Command = cmd2
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-4 End.
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true

Notice the change in active, completed and total completed task count of the executor. We can invoke shutdown() method to finish execution of all the submitted tasks and terminate the thread pool.

If you want to schedule a task to run with delay or periodically then you can use ScheduledThreadPoolExecutor class. Read more about them at Java Schedule Thread Pool Executor.

Comments

  1. Rakesh says:

    Hi, I have created ExecutorService with 10 worker threads, and there are N number of tasks, wherein each task connects to DB and does some processing. My problem is, I am unable to understand why thread names keep changing. For example : pool-9-thread-1, pool-9-thread-2,…pool-9-thread-10 after few executions the thread name changes to pool-10-thread-1,pool-10-thread-2,….pool-10-thread-10 .
    Can you please explain, me why is it happening?

    1. Kiran says:

      Hi Rakesh,
      I think you are initializing the ExecutorService instance every time in the loop.

      Instead, you should create the ExecutorService instance only once.
      To this single ExecutorService instance you should give N tasks one by one in a loop.

      Without the reference to the code you wrote, this much can be concluded.
      If possible, post your code to get Exact reason.

  2. linjiejun says:

    ”Java thread pool manages the collection of Runnable threads and worker threads execute Runnable from the queue“

    I think this should be —>
    Java thread pool manages the collection of Runnable threads and worker threads executING
    Runnable from the queue

    Please answer me . Am I right?

    1. Pankaj says:

      I admit the wordings were confusing. I have corrected it by splitting into two sentences.

  3. Florin says:

    can you help me wihts this?
    Implement a thread pool executor, similar to the one present in Java platform: java.util.concurrent.ThreadPoolExecutor
    WITHOUT using any class from java.util.concurrent package.
    The component will accept for execution tasks of type java.lang.Runnable.
    The input parameters are:
    – int corePoolSize
    – int maximumPoolSize
    – int keepAliveTime (resolution = 1 second)
    – int queueSize

  4. Akshay says:

    I am not able to see Executors is implementing Executor interface

    1. Pankaj says:

      Yes, you are correct. Executors class doesn’t implement the Executor interface. It provides useful methods to work with the Executor interface and create a thread pool. I have updated the article with correct details.

  5. Christophe says:

    thanks a lot, it’s really do me a favor

  6. Ashish Mishra says:

    Nice example. i have some requirement. I have to read number of files and push the data to the table. I think executor service can used here to do it in a optimum way. Can you please help me how to do it using executor service

  7. Prakash says:

    Very helpful.

    Thanking you

  8. krishna chaitanya says:

    Neat and clean explanation

  9. Amarreddy says:

    How to fetch large set of data from database using ThreadPoolExecutor (or) any other way to fetch large data at once?

  10. Giulio Cesare says:

    Excellent explanation with sample program to ThreadPoolExecutor.
    I could easily modify the WorkerPool to manage task submission only if queue has room for new tasks avoiding tasks to be rejected. If it may be of any use to anyone here is the code

    for(int i=0; i<10; i++){
    // Can we submit another task without being rejected?
    while (executorPool.getQueue().remainingCapacity() == 0) // is the task queue full?
    { // wait till we have a free slot for a new task
    try {
    Thread.sleep(1*1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }
    executorPool.execute(new WorkerThread("cmd"+i));
    }

  11. Dan says:

    Nice and Simple post.

  12. hitchu says:

    Nice article

  13. Anurag Singh says:

    thank you so much for this very unique post.
    it is very helpful article.
    thanks

  14. Priyan says:

    very helpful example. Thank you.

  15. Kiran Kanaparthi says:

    r.toString() is NOT needed in
    System.out.println(r.toString() + ” is rejected”);

    As the toString() call is implicit with the +(String Concat) operation.

  16. Kenshin says:

    Thank you for your example.

    I have understood and learned a lot from you tutorial.

  17. A.Javadi says:

    really useful,,,,,Thanks so much …..

  18. Ankit Jain says:

    Hi Pankaj,

    Thanks for the Tutorial.
    Can you please tell , how to decide the size of Fixed Thread pool. ?

    Thanks

  19. shiming zheng says:

    Thanks for the Tutorial!
    in class SimpleThreadPool
    @Override
    public String toString(){
    return this.command;
    }
    }
    Any use?

  20. Sriharsha says:

    Suppose an executor is associated with 10 tasks,where each time take some unknown time.In that case,How long should i wait before calling executor.shutdown();

    Is the only solution to such thing is associating a new excecutor for every task so that,executor.shutdown(),will not shutsdown until its running task completes the execution?

    1. Banjamin says:

      Hi Sriharsha,

      You just need add a if loop in monitor’s run method to check if active Count change to 0 and then shutdown the executor .
      hope that helps

  21. Rymz says:

    Thank you very much

  22. Alabi Razaq says:

    I so much appreciate your solutions, Pankaj. You call it tutorial but I call it solution because it might have helped someone.
    Can I ask a question please.

    I have a program for computer based test and is almost ready but I want to include a mechanism that will make the app to close at the end of a specified duration even if user have not finished the test.
    During research I realized I needed thread() and
    duration () but I have not seen any suitable method
    I also want the duration to be displacement as the program runs.
    Also the thread should be one that can be disabled and enabled after examiner completes setting questions
    Thanks in advance.

  23. Nishant says:

    Hi Thanks, really gr8

  24. Claude Quézel says:

    Is it just me that thinks that WorkerThread is a realy bad name for a task?

    1. steam says:

      this is not point!

  25. Yuval says:

    The busy loop at the end troubles me:

    while (!executor.isTerminated()) {
    }

    While I am new to Java, it seems to me that that would create a busy loop, which may be avoided by using executor.awaitTermination() instead.

    1. Ole Villumsen says:

      @Yuval, I agree about the busy wait. It’s bad. I believe a good solution is to use this method in the ExecutorService:
      boolean awaitTermination(long timeout,
      TimeUnit unit)
      throws InterruptedException
      Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

  26. manoj says:

    I have a servlet which does very long running process. Now I want to share a pool of thread with maximum single thread in it. If any further request comes to the servlet, it should add new task to queue and servlet method should return from there. How can I achieve this.
    I have created fixedthreadpool in servlet constructor, since there will be only single servlet per application. If executorservice execute method has already one thread running, then it should add it to queue. I am also using asynccontext to use request and response objects. But clients are getting response at same time.

    1. ma says:

      I have figured out the reason. The new request is added to queue. On client side.. I need to listen for onprogress event.

  27. Gopinath says:

    Simple but profound tutorial found for thread concepts till now!!! Thanks a ton Pankaj for ur great work it really helps me a lot…keep learning,Keep writing and keep us learning…

  28. Rajnish says:

    thanks a lot,really it helped me. keep on 🙂

  29. marqo says:

    Thanks a lot,
    it help me to know different use between only Executor and ThreadPoolExecutor..

  30. Nicola Thon says:

    Thank you so much for your sharing, it is really helpful to me.
    😀

    1. chandra shaker says:

      Thanks to share valuable information. Great efforts and its help full for us….

  31. Jeston says:

    Thanks for the tutorial. It really helped me to clear things up!

  32. Tanu says:

    Thank u soo much. Helped me a lot.

  33. Correia e Silva says:

    Awesome tutorial! Thanks a lot

  34. Adarsha says:

    Really nice !

  35. J says:

    Nice move Pankaj… Clear, useful, didactic, quite poetry…
    Thank you very much for enlightening our minds with so beautiful code…

  36. Manish Mannan says:

    Hi Pankaj,

    Its very nice to read your article but i am looking for answers of my question. it would be great if you could help me out

    1. is thread pool taking some memory location in JVM if yes then which part of JVM does it if no then please explain
    2. How JVM is responsible for execution of threads.
    3. by calling t.start we are creating the new thread in thread pool which internally call run method and now my question is run method is responsible for running state of thread or which method we can call to running method in java

    t.Start >> t.Run >> Running

    i would appreciate your response !!

    1. Lakshyaveer Chaudhary says:

      by t.start() we are not creating any thread, instead of this by using Runnable interface we create threads.

  37. Jyoti says:

    Hello pankaj,

    Thanks a lot for such a great work.

    Could you please cover CountDownLatch & Semaphores topics as well!

    Thanks & regards
    Jyoti

  38. Varun says:

    Thanks for the great article Pankaj. I had a simple doubt and the first line of the cleared it. You and Jacob Jenkov provide best material for clearing concurrency concepts

  39. Shanmugam says:

    Tq….Pankaj…

  40. HIMANSU NAYAK says:

    Hi Pankaj,
    I have few doubts on this WorkerPool.java example
    1. In WorkerPool.java example you have use ThreadFactory interface, but ThreadPoolExecutor uses the defaultThreadFactory() internally in case if it is not passed during creation, then what is the purpose of explicitly doing it in the example?

    2. “So if there are 4 running tasks and more tasks are submitted, the work queue will hold only 2 of them and rest of them will be handled by RejectedExecutionHandlerImpl” – if we haven’t implemented RejectionExecutionHandler interface then all the task would be in the queue for the execution without getting rejected?

  41. HIMANSU NAYAK says:

    Hi Pankaj,
    Thank you for such a beautiful article on one of the most complex topic in java.

    Can you please elaborate the exact diffrence between shutdown() and shutdownow() api, as the javadoc is not very clear on the shutdownnow()

  42. Brindha says:

    Hi,

    Your artcile is very good, but I struggle whenever I am asked to develop new Programming codes like if something is given in Socket or Thread…(i.e)different from the usual ones………can you please tell me which books to refer, Can you please give a list of them

    1. Pankaj says:

      The more you practice, the easier it will become. There are some good books for Java but usually I read the Java Docs for different classes and API docs. Because the books are mostly not updated to latest version.

  43. Tumi Le says:

    Thanks a lot for your great article.

  44. thanksmahesh says:

    thanks a lot

  45. Rahul says:

    Thanks for the nice example

    I have a question. What is the use of passing 3 as an argument while creating an object of MyMonitorThread ?

    1. Rahul says:

      I got that . But still thanks for sharing really good implementations.

  46. Roshan says:

    Thanks for the Tutorial. Really Good.

  47. Lin says:

    Thank you for your posts.

    I have learned a lot from your tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages