Skip to content
javahacks.dev
Concurrency

Fix RejectedExecutionException From a Full Thread Pool

Fix RejectedExecutionException by telling a full queue from a shut down executor and choosing a pool size, queue bound and rejection policy on purpose.

7 min read
Summarize in AI
Flat illustration of threads sharing work across lanes, a lock and a pool, with the OpenJDK, Linux and IntelliJ IDEA logos.

A service works for weeks and then, under a burst of traffic, starts throwing this:

Text
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@69930714[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@a4102b8[Wrapped task = Rej$$Lambda$216/0x0000000800d4c208@11dc3715]] rejected from java.util.concurrent.ThreadPoolExecutor@7a52f2a2[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]

The message looks like noise and is actually a complete diagnosis. It names the pool and prints its state: Running, pool size 1, one active thread, one queued task. The pool had no capacity left and was told not to wait. RejectedExecutionException is how a ThreadPoolExecutor says no, and it says no in exactly two situations: it is saturated (all threads busy and the queue full) or it has been shut down. This post reproduces both with real output, reads the state fields in the message, explains why an unbounded queue is not the fix, picks the right rejection policy and shows how the same error appears in Spring. The output below was produced by running the code on JDK 17.

Cause 1: the pool is saturated

A ThreadPoolExecutor has a core size, a maximum size and a queue. A new task goes to a free thread; if none is free it goes into the queue; if the queue is full it creates threads up to the maximum; if even that is exhausted, the rejection policy runs, and the default policy throws. Reproduce it with the smallest pool that can show it, one thread and a queue of one:

src/main/java/dev/javahacks/pool/Rej.javajava
import java.util.concurrent.*;

public class Rej {

    public static void main(String[] args) throws Exception {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1));
        Runnable slow = () -> {
            try { Thread.sleep(300); } catch (InterruptedException e) { }
        };

        pool.submit(slow);          // runs on the only thread
        pool.submit(slow);          // waits in the queue
        try {
            pool.submit(slow);      // no thread, no room
        } catch (RejectedExecutionException e) {
            System.out.println(e.getMessage());
        }
        pool.shutdown();
    }
}
Text
Task java.util.concurrent.FutureTask@69930714[Not completed, ...] rejected from java.util.concurrent.ThreadPoolExecutor@7a52f2a2[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]

Read the bracket at the end: [Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]. State Running means it is open for business; one thread, busy; one task queued; and nothing finished yet. With a limit of one thread and one queue slot, the third task has nowhere to go. In production the numbers are larger (pool size = 200, active threads = 200, queued tasks = 1000) and the meaning is the same: the pool is full and work arrives faster than it completes.

Find out why it is full before changing a number. Two possibilities. The load really exceeds capacity, so the pool is correctly sized for a smaller world. Or tasks are stuck: a call to a slow dependency with no timeout holds every thread, and the queue fills behind them. A thread dump separates the two. If most threads of the pool are WAITING on the same lock, a socket read or a database connection, the pool is blocked, not overloaded; reading that dump is covered in finding deadlocks and blocked threads in a dump. The fix for blocked threads is a timeout on the wait.

Cause 2: the executor was shut down

The same exception is thrown for a different reason: the pool is no longer accepting tasks. The state in the message changes:

java
pool.shutdown();
try {
    pool.submit(slow);
} catch (RejectedExecutionException e) {
    System.out.println(e.getMessage());
}
Text
Task java.util.concurrent.FutureTask@8909f18[Not completed, ...] rejected from java.util.concurrent.ThreadPoolExecutor@7a52f2a2[Shutting down, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]

Shutting down (or Terminated) in the bracket is the tell. This one is not about capacity, and no change of size fixes it. Something called shutdown() or shutdownNow(), and then something else still submitted work. Typical sources:

  • Application shutdown. During a Spring context close or a SIGTERM, the executor stops while a request thread or a scheduled task still submits work. It is usually harmless noise at the end of a graceful shutdown, and a sign that the shutdown order is wrong if it happens earlier.
  • A shared executor closed by one user. A utility class calls executor.shutdown() in a finally on a pool that other code also uses. After that, every other user gets rejections. Never shut down a pool you did not create.
  • **A try-with-resources on an ExecutorService.** On Java 19 and later, ExecutorService is AutoCloseable, and leaving the block closes it. Reusing the variable after the block fails with this error.
  • A static pool and a redeploy. In an application server, a pool held in a static field survives into a new deployment in a stale state.

Fix by finding who calls shutdown (search for shutdown, shutdownNow and close on the executor, or set a breakpoint on ThreadPoolExecutor.shutdown) and by making ownership explicit: whoever creates the pool closes it, once, at the end of its life. The proper shutdown sequence, without leaks, is in using ExecutorService correctly.

Cause 3: the pool is the wrong size

Sizing mistakes cause saturation by construction. A pool of 4 threads for tasks that wait 200 ms on a database cannot serve 100 requests per second: each thread handles 5 per second, so the pool tops out at 20. The rule is Little's law: the threads needed equal throughput times the time each task holds a thread. For 100 tasks per second at 200 ms, you need at least 100 x 0.2 = 20 threads busy on average, plus headroom for bursts. Work out the number from measured latency rather than guessing, as in sizing thread pools for CPU and I/O work.

The other direction matters too. Raising the maximum to 5,000 hides the symptom and invites memory pressure from thousands of stacks and a stampede on whatever the tasks call. A bounded, measured pool plus a policy for overflow is the stable design.

The unbounded queue trap

The popular "fix" is Executors.newFixedThreadPool(n), which never rejects because its queue is a LinkedBlockingQueue with no limit. It does not solve the problem; it hides it. Under sustained overload the queue grows without bound, each queued task holds memory, latency climbs from milliseconds to minutes as tasks wait their turn, and eventually the JVM runs out of heap with OutOfMemoryError, a far worse failure than a clean rejection. A rejected task at least tells the caller immediately. Prefer a bounded queue and an explicit policy:

java
ThreadPoolExecutor pool = new ThreadPoolExecutor(
        8, 8, 0L, TimeUnit.MILLISECONDS,
        new ArrayBlockingQueue<>(200),
        new ThreadPoolExecutor.CallerRunsPolicy());

The four built-in rejection policies:

PolicyBehaviorUse when
AbortPolicy (default)Throws RejectedExecutionExceptionThe caller can handle failure: return a 503, retry later
CallerRunsPolicyRuns the task on the submitting threadYou want backpressure: the producer slows down because it is doing the work
DiscardPolicySilently drops the taskNever for important work; only for best-effort, with metrics
DiscardOldestPolicyDrops the oldest queued task and retriesOnly when newer work supersedes older, such as latest-value updates

Show CallerRunsPolicy working: with a one-thread pool and a queue of one, the third submission runs on the caller:

Text
ran on main

The submitting thread (main) executed the task, so it could not submit more until it finished. That throttles producers to the rate the pool can sustain, which is the point. It also means the caller's latency now includes the task's duration; if the caller is a request thread, a slow task makes that request slow. Choose by asking what the caller should do when the system is full. Fail fast and tell the client (abort), slow down (caller runs) or lose some work on purpose (discard). Never pick "wait forever" by default. The behavior of each is specified in the ThreadPoolExecutor Javadoc, and the standard tutorial is Oracle's thread pools lesson.

In Spring

Spring's @Async and @Scheduled use ThreadPoolTaskExecutor, a wrapper over the same class, and its defaults are worth knowing. The auto-configured executor in Spring Boot has a core size of 8, an unbounded queue and no maximum growth, so it does not reject, it queues, and the trap above applies. When you define your own, set the bound and the policy:

src/main/java/dev/javahacks/config/AsyncConfig.javajava
@Configuration
@EnableAsync
class AsyncConfig {

    @Bean(name = "mailExecutor")
    ThreadPoolTaskExecutor mailExecutor() {
        var ex = new ThreadPoolTaskExecutor();
        ex.setCorePoolSize(4);
        ex.setMaxPoolSize(8);
        ex.setQueueCapacity(100);
        ex.setThreadNamePrefix("mail-");
        ex.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        ex.setWaitForTasksToCompleteOnShutdown(true);
        ex.setAwaitTerminationSeconds(20);
        return ex;
    }
}

The last two settings address cause 2: with waitForTasksToCompleteOnShutdown, the pool drains during context close instead of rejecting late arrivals. Name the threads (setThreadNamePrefix) so the pool in a stack trace or a thread dump is recognizable. The exception from @Async methods is thrown to the caller as TaskRejectedException, Spring's wrapper for the same event. On Java 21 and later, switching to virtual threads (spring.threads.virtual.enabled=true) removes the pool-size bound for waiting work, which makes saturation of the thread pool a non-issue and moves the limit to the resources the tasks use. The modern language and library features around this are in the modern Java guide.

Where this does not apply

If the exception comes from a ForkJoinPool, a ScheduledThreadPoolExecutor or a framework's own executor (Netty, Tomcat), the same two causes hold but the settings differ; read the state in the message and check the framework's documentation. If the message says Terminated, the pool is gone and the question is lifecycle, not capacity. And if rejections are rare, a single burst at startup, a retry with backoff may be all you need, not a bigger pool.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.