Skip to content
Ameen Abozaid
Planned lessonMID-007-L02

Virtual threads explained

  • Lesson 2 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

describe how virtual threads work and when they help.

Planned topics

  • A virtual thread is a lightweight thread managed by the JVM, mounted on a small pool of carrier (platform) threads.
  • When it blocks on supported I/O, it unmounts and the carrier runs another virtual thread.
  • Cheap to create: one per task; do not pool them.
  • Create with Thread.ofVirtual().start(r) or Executors.newVirtualThreadPerTaskExecutor().
  • They help blocking I/O workloads; they do not make CPU work faster.
Draft outline details

Demonstration

java
try (var exec = Executors.newVirtualThreadPerTaskExecutor()) {
    suppliers.forEach(s -> exec.submit(() -> importPrices(s)));
}

Check your understanding

Should you pool virtual threads?

Show answer

No; create one per task.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.