1. Concept library
  2. Java & Spring Boot
  3. Module 4: Concurrency

Concept

Virtual threads

Lightweight threads from Project Loom, and when they replace a thread pool.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how a virtual thread differs from a platform thread
  2. Run blocking I/O on virtual threads and explain why a pool is unnecessary
  3. Spot the synchronized block that pins the carrier thread

In practice

A few lines that show Virtual threads doing its job.

Java
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    List<Future<String>> pages = urls.stream()
        .map(url -> executor.submit(() -> fetch(url)))   // blocking is fine here
        .toList();
    for (var page : pages) System.out.println(page.get().length());
}

// Spring Boot: spring.threads.virtual.enabled=true

The common mistake

Watch for

Pooling virtual threads or holding a synchronized block around blocking I/O. Both pin the virtual thread to its carrier and lose the benefit; create them freely and prefer ReentrantLock around blocking calls.

What to know first

Your roadmap puts these before Virtual threads, unless you already know them.

How it stays learned

Once a session shows you understand Virtual threads, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Java memory model