Backend Development 5 min read

Implementing Asynchronous Calls in SpringBoot with @Async

This article explains why asynchronous programming improves concurrency in SpringBoot applications, describes how to enable @Async support, configure a custom ThreadPoolTaskExecutor, annotate methods for async execution, and outlines the underlying proxy mechanism that powers @Async.

Java Captain
Java Captain
Java Captain
Implementing Asynchronous Calls in SpringBoot with @Async

When a system needs higher concurrency, time‑consuming operations can be executed asynchronously to avoid blocking the main thread, reducing response time under heavy load.

In SpringBoot, asynchronous execution is enabled by annotating methods with @Async and activating the feature with @EnableAsync in a configuration class.

Step 1 – Enable async support : Create a separate configuration class (e.g., SyncConfiguration ) and place @EnableAsync on it (or on the main application class).

Step 2 – Define a custom thread pool to replace the default SimpleAsyncTaskExecutor , which does not reuse threads and can cause OutOfMemoryError . Example configuration:

@Configuration
@EnableAsync
public class SyncConfiguration {
    @Bean(name = "asyncPoolTaskExecutor")
    public ThreadPoolTaskExecutor executor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setQueueCapacity(50);
        taskExecutor.setKeepAliveSeconds(200);
        taskExecutor.setThreadNamePrefix("async-");
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }
}

Step 3 – Annotate the target method with @Async so it runs in the background:

public class AsyncTask {
    @Async
    public void task() {
        long t1 = System.currentTimeMillis();
        Thread.sleep(5000);
        long t2 = System.currentTimeMillis();
        log.info("task cost {} ms", t2 - t1);
    }
}

Step 4 – Invoke the async method where needed:

asyncTask.task();

How @Async works : When an @Async method is called, Spring creates an asynchronous proxy that wraps the call as a separate task, submits it to the configured executor, immediately returns to the caller, and runs the task in a thread from the pool until completion.

JavaThreadPoolSpringBootAsyncAsync
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.