Java Thread Pools

Su Suriya Ravichandran Updated 18 Nov 2026
11 min read ·Lesson 194 of 306

A Java thread pool is a group of worker threads that are created once and then reused to run many tasks. Instead of starting a new thread for every job, you put jobs into a queue, and the pool's workers take them out and run them one after another.

Thread pools make programs faster, because creating threads is expensive, and safer, because the pool puts a limit on how many threads exist at once. The executors you used in Java ExecutorService are thread pools; this lesson looks at how they work inside and how to configure one yourself with ThreadPoolExecutor.


How a Java Thread Pool Works

Every thread pool has two main parts: a work queue that holds tasks waiting to run, and a set of worker threads that repeatedly take a task from the queue and run it.

 submit(task) ──►  [ work queue: task5 task4 task3 ]  ──►  worker-1 (running task1)
                                                      ──►  worker-2 (running task2)

When a worker finishes a task, it does not die. It goes back to the queue and takes the next task. The single-thread executor below shows this clearly: one worker runs every task, in the order they were submitted.

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

public class SingleWorker {
    public static void main(String[] args) {
        try (ExecutorService printerQueue = Executors.newSingleThreadExecutor()) {
            for (String document : new String[] {"invoice.pdf", "report.docx", "photo.png"}) {
                printerQueue.execute(() ->
                    System.out.println("Printing " + document + " on "
                            + Thread.currentThread().getName()));
            }
        } // waits for the queue to be empty
        System.out.println("Print queue empty");
    }
}

Output:

Printing invoice.pdf on pool-1-thread-1
Printing report.docx on pool-1-thread-1
Printing photo.png on pool-1-thread-1
Print queue empty

How it works:

  • newSingleThreadExecutor() is a pool with exactly one worker, pool-1-thread-1.
  • All three tasks go into the queue. The worker runs them one at a time, so the order is always the order of submission.
  • The same thread name appears three times, which shows the thread is reused rather than recreated.

Types of Thread Pools in Java

The Executors factory methods are shortcuts that create a ThreadPoolExecutor (or ScheduledThreadPoolExecutor) with preset settings.

Factory methodCore sizeMax sizeQueueBehaviour
newFixedThreadPool(n)nnUnbounded LinkedBlockingQueueAlways n threads; extra tasks wait
newSingleThreadExecutor()11Unbounded LinkedBlockingQueueTasks run one by one, in order
newCachedThreadPool()0UnlimitedSynchronousQueue (no storage)New thread whenever none is free; idle threads die after 60 s
newScheduledThreadPool(n)nUnlimitedDelay queueRuns tasks after a delay or repeatedly

Note: The fixed and single pools use an unbounded queue. If tasks arrive faster than they finish, the queue grows without limit and can cause an OutOfMemoryError. The cached pool has the opposite risk: it can create thousands of threads. For servers, build a ThreadPoolExecutor with explicit limits.


ThreadPoolExecutor Constructor Parameters

ThreadPoolExecutor lets you control every part of the pool.

new ThreadPoolExecutor(
        corePoolSize,      // threads kept alive even when idle
        maximumPoolSize,   // upper limit on threads
        keepAliveTime,     // how long extra (non-core) threads may stay idle
        unit,              // TimeUnit for keepAliveTime
        workQueue,         // where waiting tasks are stored
        threadFactory,     // optional: how threads are created (names, daemon flag)
        handler);          // optional: what to do when the pool is full
ParameterMeaning
corePoolSizeNumber of threads the pool keeps, even when they have nothing to do
maximumPoolSizeHighest number of threads the pool may create
keepAliveTime + unitExtra threads above the core size end after being idle this long
workQueueA BlockingQueue<Runnable>; bounded queues such as ArrayBlockingQueue set a limit
threadFactoryCreates each worker thread; default names are pool-N-thread-M
handlerA RejectedExecutionHandler, called when both the queue and the pool are full

How Core Size, Queue and Max Size Work Together

New tasks are handled in a fixed order that surprises many developers. The pool only grows beyond its core size after the queue is full.

  1. If fewer than corePoolSize threads exist, start a new thread for the task.
  2. Otherwise, put the task in the queue.
  3. If the queue is full and fewer than maximumPoolSize threads exist, start a new thread.
  4. Otherwise, reject the task.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class PoolGrowth {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2,                               // core threads
                4,                               // max threads
                30, TimeUnit.SECONDS,            // idle time for extra threads
                new ArrayBlockingQueue<>(2));    // queue holds at most 2 tasks

        for (int task = 1; task <= 7; task++) {
            int taskNo = task;
            try {
                pool.execute(() -> sleepQuietly(500)); // each task is busy for 500 ms
                System.out.printf("task %d accepted -> pool size %d, queued %d%n",
                        taskNo, pool.getPoolSize(), pool.getQueue().size());
            } catch (RejectedExecutionException e) {
                System.out.printf("task %d REJECTED (pool size %d, queue full)%n",
                        taskNo, pool.getPoolSize());
            }
        }

        pool.shutdown();
        pool.awaitTermination(5, TimeUnit.SECONDS);
        System.out.println("completed tasks: " + pool.getCompletedTaskCount());
    }

    static void sleepQuietly(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Output:

task 1 accepted -> pool size 1, queued 0
task 2 accepted -> pool size 2, queued 0
task 3 accepted -> pool size 2, queued 1
task 4 accepted -> pool size 2, queued 2
task 5 accepted -> pool size 3, queued 2
task 6 accepted -> pool size 4, queued 2
task 7 REJECTED (pool size 4, queue full)
completed tasks: 6

How it works:

  • Tasks 1 and 2 each start a core thread.
  • Tasks 3 and 4 go into the queue, even though the pool could still grow to 4 threads.
  • Tasks 5 and 6 arrive when the queue is full, so the pool creates extra threads, up to the maximum of 4.
  • Task 7 finds 4 busy threads and a full queue, so the default handler (AbortPolicy) throws a RejectedExecutionException.
  • After 30 idle seconds the two extra threads would end, and the pool would shrink back to its 2 core threads.

Thread Pool Rejection Policies

When the pool and the queue are both full, the pool calls its RejectedExecutionHandler. Java includes four ready-made policies as nested classes of ThreadPoolExecutor.

PolicyWhat happens to the rejected task
AbortPolicy (default)Throws RejectedExecutionException
CallerRunsPolicyThe thread that called execute() runs the task itself
DiscardPolicyThe task is silently dropped
DiscardOldestPolicyThe oldest queued task is dropped, and the new one is queued

CallerRunsPolicy is useful for back-pressure: when the pool is overloaded, the submitting thread is kept busy running a task, so it automatically slows down.

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

public class CallerRunsDemo {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1, 1, 0, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(1),
                new ThreadPoolExecutor.CallerRunsPolicy()); // overflow runs on the caller

        for (int i = 1; i <= 4; i++) {
            int jobNo = i;
            pool.execute(() -> {
                sleepQuietly(200);
                System.out.println("job " + jobNo + " ran on " + Thread.currentThread().getName());
            });
        }

        pool.shutdown();
        pool.awaitTermination(5, TimeUnit.SECONDS);
    }

    static void sleepQuietly(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Output (the first two lines can swap):

job 1 ran on pool-1-thread-1
job 3 ran on main
job 2 ran on pool-1-thread-1
job 4 ran on pool-1-thread-1

How it works:

  • Job 1 takes the only worker, and job 2 fills the one-slot queue.
  • Job 3 is rejected, so CallerRunsPolicy runs it on the main thread. main cannot submit job 4 until job 3 is done.
  • By then the worker has finished job 1 and taken job 2 from the queue, so job 4 fits in the queue. No task is lost.

Naming Pool Threads with a ThreadFactory

Default names like pool-3-thread-7 make logs and thread dumps hard to read. A ThreadFactory controls how the pool creates its threads. Since Java 21, Thread.ofPlatform() builds one in a single line.

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

public class NamedThreads {
    public static void main(String[] args) {
        // Names: email-sender-1, email-sender-2, ...
        ThreadFactory emailThreads = Thread.ofPlatform().name("email-sender-", 1).factory();

        try (ExecutorService emailPool = Executors.newFixedThreadPool(2, emailThreads)) {
            for (String customer : new String[] {"asha@example.com", "ravi@example.com"}) {
                emailPool.execute(() ->
                    System.out.println(Thread.currentThread().getName() + " -> " + customer));
            }
        }
    }
}

Output (the order can change):

email-sender-1 -> asha@example.com
email-sender-2 -> ravi@example.com

How it works:

  • name("email-sender-", 1) gives each new thread the prefix plus a counter that starts at 1.
  • newFixedThreadPool(2, emailThreads) uses the factory for both workers.
  • On Java 17 or older, write the factory as a lambda: r -> new Thread(r, "email-sender-" + counter.incrementAndGet()) with an AtomicInteger counter.

Scheduled Thread Pools

A ScheduledExecutorService runs tasks after a delay or repeatedly at a fixed rate. It replaces the old java.util.Timer class.

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ScheduledPool {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        CountDownLatch threeBeats = new CountDownLatch(3);
        AtomicInteger beatNumber = new AtomicInteger();

        // run once, after a delay
        scheduler.schedule(() -> System.out.println("cache warmed (after 100 ms delay)"),
                100, TimeUnit.MILLISECONDS);

        // run repeatedly: first at 0 ms, then every 200 ms
        ScheduledFuture<?> heartbeat = scheduler.scheduleAtFixedRate(() -> {
            System.out.println("heartbeat " + beatNumber.incrementAndGet());
            threeBeats.countDown();
        }, 0, 200, TimeUnit.MILLISECONDS);

        threeBeats.await();      // wait for three heartbeats
        heartbeat.cancel(false); // stop the repeating task
        scheduler.shutdown();
    }
}

Output:

heartbeat 1
cache warmed (after 100 ms delay)
heartbeat 2
heartbeat 3

How it works:

  • schedule() runs a task once after the delay (100 ms).
  • scheduleAtFixedRate() runs the heartbeat at 0, 200 and 400 ms. scheduleWithFixedDelay() would instead wait 200 ms after each run finishes.
  • A repeating task never ends on its own, so it must be cancelled through its ScheduledFuture or by shutting down the scheduler.

How to Choose the Thread Pool Size

The right size depends on what the tasks do. CPU-bound tasks (calculations, compression) keep a core busy all the time, so more threads than cores just adds switching overhead. I/O-bound tasks (database calls, HTTP requests) spend most of their time waiting, so more threads keep the CPU busy.

Task typeRule of thumb
CPU-boundthreads ≈ number of cores (or cores + 1)
I/O-boundthreads ≈ cores × (1 + wait time / compute time)
public class PoolSizing {
    public static void main(String[] args) {
        int cores = Runtime.getRuntime().availableProcessors();

        double waitMs = 90;     // time a task spends waiting for the database
        double computeMs = 10;  // time a task spends using the CPU

        int cpuBoundPool = cores;
        int ioBoundPool = (int) (cores * (1 + waitMs / computeMs));

        System.out.println("CPU cores          : " + cores);
        System.out.println("CPU-bound pool size: " + cpuBoundPool);
        System.out.println("I/O-bound pool size: " + ioBoundPool);
    }
}

Output (on an 8-core machine):

CPU cores          : 8
CPU-bound pool size: 8
I/O-bound pool size: 80

How it works:

  • availableProcessors() returns the number of cores the JVM may use, including container limits.
  • A task that waits 90 ms and computes 10 ms keeps a core busy only 10% of the time, so about ten threads per core keep all cores working.
  • These are starting points. Measure under real load and adjust. For very large numbers of blocking tasks, virtual threads (Java 21+) avoid the sizing problem altogether.

Real-World Example: A Bounded Email Sending Pool

A shop sends 20 order-confirmation emails. The pool has named threads, a bounded queue so memory stays under control, and CallerRunsPolicy so no email is lost when the pool is busy. The program reports which threads did the work.

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

public class EmailDispatcher {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor mailPool = new ThreadPoolExecutor(
                2,                                              // core threads
                3,                                              // max threads
                30, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),                    // at most 5 waiting emails
                Thread.ofPlatform().name("mailer-", 1).factory(),
                new ThreadPoolExecutor.CallerRunsPolicy());     // overflow: caller sends it

        Map<String, LongAdder> sentBy = new ConcurrentHashMap<>();
        LongAdder sent = new LongAdder();

        for (int orderNo = 1; orderNo <= 20; orderNo++) {
            String recipient = "customer" + orderNo + "@example.com";
            mailPool.execute(() -> {
                sleepQuietly(100);                              // simulate the SMTP call
                sent.increment();
                sentBy.computeIfAbsent(Thread.currentThread().getName(), name -> new LongAdder())
                      .increment();
            });
        }

        mailPool.shutdown();
        mailPool.awaitTermination(10, TimeUnit.SECONDS);

        System.out.println("emails sent        : " + sent.sum());
        System.out.println("largest pool size  : " + mailPool.getLargestPoolSize());
        System.out.println("finished by workers: " + mailPool.getCompletedTaskCount());
        new TreeMap<>(sentBy).forEach((thread, count) ->
                System.out.println("  " + thread + " sent " + count.sum()));
    }

    static void sleepQuietly(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Output (the per-thread counts change between runs; the totals do not):

emails sent        : 20
largest pool size  : 3
finished by workers: 18
  mailer-1 sent 6
  mailer-2 sent 6
  mailer-3 sent 6
  main sent 2

How it works:

  1. Growth. Emails 1–2 start the two core threads, emails 3–7 fill the queue, and email 8 creates the third thread. That is why the largest pool size is always 3.
  2. Back-pressure. When all three workers are busy and the queue is full, CallerRunsPolicy makes main send the email itself. While it does, it cannot submit more, so the queue never grows past 5.
  3. Nothing lost. All 20 emails are sent. getCompletedTaskCount() counts only tasks finished by pool workers, so it equals 20 minus the emails sent by main.
  4. Readable names. The mailer-N thread names come from the ThreadFactory and make the report (and any thread dump) easy to understand.

Pool workers share data, so everything they update here is thread-safe (LongAdder, ConcurrentHashMap), as explained in Java Thread Safety. The next lesson, Java Callable Interface, shows how pool tasks can return results and throw checked exceptions.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Java Thread Pools and get answers drawn from it.

Signed-in readers only.