Skip to main content
PreparationZone Logo
PrepZone

@Async and @Scheduled

Background tasks, cron schedules and thread pool configuration.

Pass checkpoint below

Why this matters

  • Sending order confirmation emails or generating PDF receipts from a REST endpoint blocks the HTTP response until the slow work finishes — async moves that work off the request thread.
  • Scheduled jobs power recurring BookStore tasks: nightly inventory syncs, abandoned-cart reminders, and stale reservation cleanup.
  • Misconfigured thread pools are a common production incident: too few threads create backlogs, too many threads exhaust memory.

@Async

L1 · Fresher

Run work on a thread pool after the caller returns — good for emails and analytics, not for work that must finish before HTTP response.

@EnableAsync configuration

L2 · Intermediate

Define TaskExecutor beans with pool sizes and queue limits; unbounded pools hide overload until the JVM runs out of memory.

@Scheduled cron

L3 · Advanced

@Scheduled(cron = "0 0 2 * * *") runs nightly catalog sync — use ShedLock or leader election if multiple BookStore instances run.

Virtual threads for @Async

L4 · Expert

On Java 21, configure async to use virtual threads for I/O-heavy tasks with simpler scaling than large platform thread pools.

Platform threads (carriers)
VT-1Blocked on I/O
VT-2Running
VT-3Blocked on I/O

Blocked virtual threads release their carrier — other virtual threads continue

Thousands of virtual threads multiplex onto a small pool of platform (carrier) threads.

Core annotations and configuration

  • @EnableAsync — activates Spring's async proxy infrastructure; add it to a @Configuration class or your main application class.
  • @Async — marks a method to run on the task executor instead of the caller's thread; must be on a public method of a Spring-managed bean.
  • @EnableScheduling — turns on the scheduler that evaluates @Scheduled expressions.
  • @Scheduled — runs a method on a fixed rate, fixed delay, or cron expression.
  • TaskExecutor / ThreadPoolTaskExecutor — the bean behind @Async; you define pool size, queue capacity, and thread naming.

Enable async and scheduling

Java
// learn:fragment
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfig {

    @Bean(name = "bookstoreTaskExecutor")
    public Executor bookstoreTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(200);
        executor.setThreadNamePrefix("bookstore-async-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Fire-and-forget email after checkout
  • Cron jobs for housekeeping
  • Interview follow-ups

Test yourself

Answer these before moving on — recall is what makes it stick.