Tutorial

ThreadPoolExecutor - Java Thread Pool Example

Published on August 3, 2022
ThreadPoolExecutor - Java Thread Pool Example

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
See author profile
Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 27, 2013

Thank you for your posts. I have learned a lot from your tutorials.

- Lin

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 27, 2013

    Thanks for the Tutorial. Really Good.

    - Roshan

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 2, 2013

      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 ?

      - Rahul

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 2, 2013

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

      - Rahul

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        October 22, 2013

        thanks a lot

        - thanksmahesh

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 1, 2013

          Thanks a lot for your great article.

          - Tumi Le

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 4, 2013

            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

            - Brindha

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 4, 2013

            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.

            - Pankaj

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              April 7, 2014

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

              - HIMANSU NAYAK

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                April 7, 2014

                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?

                - HIMANSU NAYAK

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  April 11, 2014

                  Tq…Pankaj…

                  - Shanmugam

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 8, 2014

                    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

                    - Varun

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      July 24, 2014

                      Hello pankaj, Thanks a lot for such a great work. Could you please cover CountDownLatch & Semaphores topics as well! Thanks & regards Jyoti

                      - Jyoti

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        August 3, 2014

                        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 !!

                        - Manish Mannan

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        May 16, 2018

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

                        - Lakshyaveer Chaudhary

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          August 29, 2014

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

                          - J

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            September 23, 2014

                            Really nice !

                            - Adarsha

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              October 13, 2014

                              Awesome tutorial! Thanks a lot

                              - Correia e Silva

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                January 8, 2015

                                Thank u soo much. Helped me a lot.

                                - Tanu

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  January 15, 2015

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

                                  - Jeston

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    February 18, 2015

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

                                    - Nicola Thon

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    February 25, 2015

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

                                    - chandra shaker

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      February 27, 2015

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

                                      - marqo

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        May 11, 2015

                                        thanks a lot,really it helped me. keep on :)

                                        - Rajnish

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          August 23, 2015

                                          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…

                                          - Gopinath

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            August 27, 2015

                                            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.

                                            - manoj

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            August 28, 2015

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

                                            - ma

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              October 12, 2015

                                              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.

                                              - Yuval

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              January 15, 2016

                                              @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.

                                              - Ole Villumsen

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                December 13, 2015

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

                                                - Claude Quézel

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                January 17, 2017

                                                this is not point!

                                                - steam

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  June 9, 2016

                                                  Hi Thanks, really gr8

                                                  - Nishant

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    September 9, 2016

                                                    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.

                                                    - Alabi Razaq

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      September 11, 2016

                                                      Thank you very much

                                                      - Rymz

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        September 14, 2016

                                                        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?

                                                        - Sriharsha

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        August 16, 2017

                                                        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

                                                        - Banjamin

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          September 18, 2016

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

                                                          - shiming zheng

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            December 4, 2016

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

                                                            - Ankit Jain

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 23, 2016

                                                              really useful,Thanks so much …

                                                              - A.Javadi

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                January 12, 2017

                                                                Thank you for your example. I have understood and learned a lot from you tutorial.

                                                                - Kenshin

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  March 26, 2017

                                                                  r.toString() is NOT needed in System.out.println(r.toString() + " is rejected"); As the toString() call is implicit with the +(String Concat) operation.

                                                                  - Kiran Kanaparthi

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    May 16, 2017

                                                                    very helpful example. Thank you.

                                                                    - Priyan

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      July 27, 2017

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

                                                                      - Anurag Singh

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        August 29, 2017

                                                                        Nice article

                                                                        - hitchu

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          October 20, 2017

                                                                          Nice and Simple post.

                                                                          - Dan

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            November 9, 2017

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

                                                                            - Giulio Cesare

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              November 12, 2017

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

                                                                              - Amarreddy

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                December 25, 2017

                                                                                Neat and clean explanation

                                                                                - krishna chaitanya

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  February 12, 2018

                                                                                  Very helpful. Thanking you

                                                                                  - Prakash

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    April 13, 2018

                                                                                    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

                                                                                    - Ashish Mishra

                                                                                      JournalDev
                                                                                      DigitalOcean Employee
                                                                                      DigitalOcean Employee badge
                                                                                      April 25, 2018

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

                                                                                      - Christophe

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        October 12, 2018

                                                                                        I am not able to see Executors is implementing Executor interface

                                                                                        - Akshay

                                                                                          JournalDev
                                                                                          DigitalOcean Employee
                                                                                          DigitalOcean Employee badge
                                                                                          December 17, 2018

                                                                                          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

                                                                                          - Florin

                                                                                            JournalDev
                                                                                            DigitalOcean Employee
                                                                                            DigitalOcean Employee badge
                                                                                            February 27, 2019

                                                                                            ”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?

                                                                                            - linjiejun

                                                                                              JournalDev
                                                                                              DigitalOcean Employee
                                                                                              DigitalOcean Employee badge
                                                                                              October 18, 2019

                                                                                              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?

                                                                                              - Rakesh

                                                                                                JournalDev
                                                                                                DigitalOcean Employee
                                                                                                DigitalOcean Employee badge
                                                                                                December 24, 2019

                                                                                                Thanks, my most of the basic doubts got clarified.

                                                                                                - maha

                                                                                                  JournalDev
                                                                                                  DigitalOcean Employee
                                                                                                  DigitalOcean Employee badge
                                                                                                  April 28, 2020

                                                                                                  Is it work in same way for JAVA 8, Or need to change code

                                                                                                  - Amol

                                                                                                    JournalDev
                                                                                                    DigitalOcean Employee
                                                                                                    DigitalOcean Employee badge
                                                                                                    September 10, 2021

                                                                                                    Hi Pankaj , Thanks a lot for this great tutorial, My lots of concept cleared about executorpoolservice. It would be great if my one doubt clear. I am trying to implement this threadpoolexecutor , As we know Use threadpoolExecutor we creating thead pool, My question how this thread worker is internally connected with http rest call, bcz there also we are creating tomcat thread, Suppose we have 50 tomcat thread , and 10 worker thread, and 10k request, how each request sync with there own response if we calling async call.

                                                                                                    - Ravi kumar

                                                                                                      Join the Tech Talk
                                                                                                      Success! Thank you! Please check your email for further details.

                                                                                                      Please complete your information!

                                                                                                      Become a contributor for community

                                                                                                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                                      DigitalOcean Documentation

                                                                                                      Full documentation for every DigitalOcean product.

                                                                                                      Resources for startups and SMBs

                                                                                                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                                      Get our newsletter

                                                                                                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                                      New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                                      The developer cloud

                                                                                                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                                      Get started for free

                                                                                                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                                      *This promotional offer applies to new accounts only.