BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Spring and Boot

Spring

@Async

@Async makes a call return immediately while the method body runs on another thread from a pool.

Learn these first

At a glance

Mental model

Dropping a letter in the outbox: you carry on with your day while someone else delivers it.

What happens, in order
  1. 1@EnableAsync activates the featureCalling an @Async method from another bean passes through the proxy, which submits the call to a TaskExecutor and returns right away.
  2. 2Return types: void (fire and forget), Future or CompletableFuture<T> (the caller can wait or chain)Anything else would just return null to the caller.
  3. 3The body runs on a pool thread, so thread-bound things such as the current transaction, the security context and request-scoped beans are not carried over automatically
Remember
Watch out
Self-invocation, so it runs synchronouslyUnhandled exceptions in void methodsExpecting the caller's transaction or login to follow into the new threadUnbounded queues that hide overload
Example (highlighted lines explain it)
@Servicepublic class Mailer {    @Async    public CompletableFuture<Void> send(String to, String body) {        smtp.deliver(to, body);               // runs on a pool thread        return CompletableFuture.completedFuture(null);    }}
  1. line 3 uses Async, the core of @Async
  2. line 5 runs on a pool thread
Read the full explanation (about 1 min)

@Async makes a call return immediately while the method body runs on another thread from a pool. It can return void, a Future or a CompletableFuture.

A mental model

Dropping a letter in the outbox: you carry on with your day while someone else delivers it. You do not know when it is delivered unless you ask for a receipt (the Future).

How it works

@EnableAsync activates the feature. Calling an @Async method from another bean passes through the proxy, which submits the call to a TaskExecutor and returns right away.

Return types: void (fire and forget), Future or CompletableFuture<T> (the caller can wait or chain). Anything else would just return null to the caller.

The body runs on a pool thread, so thread-bound things such as the current transaction, the security context and request-scoped beans are not carried over automatically.

Going deeper

Exceptions from a void async method disappear into the default handler (logged only). Provide an AsyncUncaughtExceptionHandler, or return a CompletableFuture and handle errors there.

Define your own executor with a bounded queue. The Spring Boot default has an effectively unbounded queue, so a traffic spike turns into memory growth instead of back-pressure.

Calling an @Async method from the same class runs it synchronously (self-invocation).

Under the hood

An AsyncAnnotationBeanPostProcessor adds an advisor whose interceptor wraps the invocation in a Callable and submits it to the executor. Spring Boot auto-configures applicationTaskExecutor (a ThreadPoolTaskExecutor) when no other executor is defined.

Common mistakes

  • Self-invocation, so it runs synchronously.
  • Unhandled exceptions in void methods.
  • Expecting the caller's transaction or login to follow into the new thread.
  • Unbounded queues that hide overload.

How Unravel recognises it

When you paste code, Unravel looks for these signs of @async. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is @Async?

@Async makes a call return immediately while the method body runs on another thread from a pool. It can return void, a Future or a CompletableFuture.

What are the most common mistakes with @Async?

Self-invocation, so it runs synchronously. Unhandled exceptions in void methods. Expecting the caller's transaction or login to follow into the new thread. Unbounded queues that hide overload.

How can I see @Async in real code?

Paste your own Java into Unravel and it will point out where @async appears, explain how it works at the detail level you choose, and draw the flow of the code around it.