Java Thread Pools
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 method | Core size | Max size | Queue | Behaviour |
|---|---|---|---|---|
newFixedThreadPool(n) | n | n | Unbounded LinkedBlockingQueue | Always n threads; extra tasks wait |
newSingleThreadExecutor() | 1 | 1 | Unbounded LinkedBlockingQueue | Tasks run one by one, in order |
newCachedThreadPool() | 0 | Unlimited | SynchronousQueue (no storage) | New thread whenever none is free; idle threads die after 60 s |
newScheduledThreadPool(n) | n | Unlimited | Delay queue | Runs 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 aThreadPoolExecutorwith 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
| Parameter | Meaning |
|---|---|
corePoolSize | Number of threads the pool keeps, even when they have nothing to do |
maximumPoolSize | Highest number of threads the pool may create |
keepAliveTime + unit | Extra threads above the core size end after being idle this long |
workQueue | A BlockingQueue<Runnable>; bounded queues such as ArrayBlockingQueue set a limit |
threadFactory | Creates each worker thread; default names are pool-N-thread-M |
handler | A 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.
- If fewer than
corePoolSizethreads exist, start a new thread for the task. - Otherwise, put the task in the queue.
- If the queue is full and fewer than
maximumPoolSizethreads exist, start a new thread. - 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 aRejectedExecutionException. - 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.
| Policy | What happens to the rejected task |
|---|---|
AbortPolicy (default) | Throws RejectedExecutionException |
CallerRunsPolicy | The thread that called execute() runs the task itself |
DiscardPolicy | The task is silently dropped |
DiscardOldestPolicy | The 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
CallerRunsPolicyruns it on themainthread.maincannot 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 anAtomicInteger 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
ScheduledFutureor 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 type | Rule of thumb |
|---|---|
| CPU-bound | threads ≈ number of cores (or cores + 1) |
| I/O-bound | threads ≈ 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:
- 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.
- Back-pressure. When all three workers are busy and the queue is full,
CallerRunsPolicymakesmainsend the email itself. While it does, it cannot submit more, so the queue never grows past 5. - Nothing lost. All 20 emails are sent.
getCompletedTaskCount()counts only tasks finished by pool workers, so it equals 20 minus the emails sent bymain. - Readable names. The
mailer-Nthread names come from theThreadFactoryand 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.