Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 5 OF 5 · 2 MIN READ

Virtual Threads (Project Loom)

Virtual threads, final in Java 21, are lightweight threads managed by the JVM rather than the OS. They let simple blocking code handle huge numbers of concurrent requests. Interviewers ask how they work, when they help, and what pinning means.

What you will learn

  • Compare platform and virtual threads
  • Create virtual threads and enable them in Spring Boot
  • Know when virtual threads help and when they do not
  • Explain pinning and what Java 24 changed

Key concepts

Platform thread

A thin wrapper over an OS thread, about 1 MB of stack, expensive to create. Servers keep them in pools of a few hundred.

Virtual thread

A JVM-managed thread that is mounted on a small pool of carrier (platform) threads. When it blocks on I/O it unmounts, freeing the carrier. You can create millions.

When it helps

I/O-bound, thread-per-request code: REST calls, JDBC, file I/O. It does not make CPU-bound work faster.

Pinning

Cases where a blocked virtual thread cannot unmount. In Java 21, blocking inside synchronized pinned the carrier; Java 24 (JEP 491) removed that limitation, and native calls can still pin.

Step by step

Using them

// one virtual thread per task
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    List<Future<Price>> prices = suppliers.stream()
            .map(s -> executor.submit(() -> s.fetchPrice(sku)))   // blocking HTTP calls are fine
            .toList();
}

Thread.ofVirtual().name("worker-", 0).start(task);

# Spring Boot 3.2+: run request handling on virtual threads
spring.threads.virtual.enabled=true

Examples

Watch out

// Do NOT pool virtual threads; create one per task.
// Limit access to scarce resources with a Semaphore, not a small thread pool:
private final Semaphore dbPermits = new Semaphore(20);

// ThreadLocal caches (e.g. big formatters) multiply with millions of threads;
// prefer ScopedValue or local variables.

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Start 100,000 threads that each sleep for one second with platform and with virtual threads; compare.
  • Explain why virtual threads do not remove the need for a database connection pool.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Virtual threads or reactive (WebFlux)?

Both handle many concurrent I/O requests. Virtual threads keep simple blocking code, normal stack traces and easy debugging. Reactive offers backpressure and streaming operators but harder code. For most new CRUD services, virtual threads with Spring MVC are simpler.

Are virtual threads faster?

Not per request. They increase throughput when many threads are waiting on I/O, because waiting no longer ties up an OS thread. Latency of one request and CPU-bound work stay the same.

Summary

Virtual threads are cheap JVM threads that unmount while blocked, giving thread-per-request code massive concurrency for I/O-bound work. Do not pool them, guard scarce resources with semaphores, and know that synchronized no longer pins since Java 24.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus