Ten thousand blocking tasks
With 10,000 platform threads this would use gigabytes of stack; with virtual threads it finishes in well under a second on a normal laptop.
Virtual threads (Java 21, JEP 444) are threads managed by the JVM instead of the operating system. They're so cheap that you can start one per task, even a million of them, and write simple blocking code that scales like asynchronous code.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a program that starts 1,000 virtual threads with Thread.ofVirtual(), each adding 1 to an AtomicInteger, joins them all and prints the total.
Explain it without notes
What is a virtual thread and how is it different from a platform thread?
Why don't virtual threads speed up CPU-bound work?
How should you limit concurrency when using virtual threads?
Expected output
10,000 tasks finished, sum = 49995000