Virtual threads explained
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)orExecutors.newVirtualThreadPerTaskExecutor(). - They help blocking I/O workloads; they do not make CPU work faster.
Draft outline details
Demonstration
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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.