To configure a thread pool for asynchronous tasks in Spring, enable async support with @EnableAsync, define a ThreadPoolTaskExecutor bean with explicit core size, maximum size and queue capacity, and reference it from @Async("beanName"). In Spring Boot you can instead tune the auto-configured executor through spring.task.execution.pool.* properties.
Spring runs @Async methods on a separate thread through a proxy, so the caller returns immediately. Without a configured pool, a plain Spring application falls back to a SimpleAsyncTaskExecutor, which creates a new thread for every task and has no upper bound. A properly sized pool prevents that.
Add @EnableAsync to a configuration class. Without it, @Async annotations are silently ignored.
ThreadPoolTaskExecutor wraps a java.util.concurrent.ThreadPoolExecutor and exposes its settings as bean properties:
| Setting | Meaning |
|---|---|
corePoolSize | Threads kept alive even when idle |
maxPoolSize | Upper limit of threads, used only when the queue is full |
queueCapacity | Tasks waiting before the pool grows past core size |
keepAliveSeconds | How long extra threads above core size stay idle |
rejectedExecutionHandler | What to do when pool and queue are both full |
threadNamePrefix | Makes thread dumps and logs readable |
Spring calls initialize() for you when the executor is a bean.
The caller gets a CompletableFuture immediately and can compose it with thenApply or thenAccept. A void method also works, but exceptions are then reported only to an AsyncUncaughtExceptionHandler.
@Async relies on a Spring proxy. It does not apply when a method is called from another method in the same class, on private methods, or on objects you create with new.
Size by workload type rather than copying numbers:
Runtime.getRuntime().availableProcessors()).queueCapacity. The default is Integer.MAX_VALUE, which means the pool never grows beyond the core size and memory can fill with queued tasks.Thread creation order matters: the pool grows to core size, then fills the queue, and only then creates threads up to the maximum.
Define a handler for exceptions thrown from void async methods:
When both pool and queue are full, the default policy throws TaskRejectedException. CallerRunsPolicy slows the producer by running the task on its own thread, which gives back pressure instead of data loss.
Spring Boot auto-configures applicationTaskExecutor and lets you set it declaratively:
On Java 21 or later with Spring Boot 3.2 or newer, spring.threads.virtual.enabled=true makes async tasks run on virtual threads, which removes pool sizing for blocking I/O work. Note that security context, MDC and transaction context are thread-bound; propagate them with a TaskDecorator set through executor.setTaskDecorator(...).
@Async method running on the same thread?Usually @EnableAsync is missing, or the method is called from the same class, which bypasses the proxy.
Core threads always exist. Threads up to the maximum are created only when the queue is full.
Set setWaitForTasksToCompleteOnShutdown(true) and setAwaitTerminationSeconds(...) so running tasks can finish.
Yes. Define several executor beans and select one per method with @Async("beanName").
Enable async with @EnableAsync, define a bounded ThreadPoolTaskExecutor, select it by name in @Async, choose a sensible rejection policy, handle exceptions, and shut down gracefully. In Spring Boot, use spring.task.execution.pool.*, or virtual threads for blocking I/O on Java 21 and later.