Virtual thread
A lightweight Java thread managed by the JVM, cheap enough to create one per task and to block.
Definition
Virtual threads (Java 21+) run on a small pool of operating-system carrier threads. When a virtual thread blocks on I/O or sleeps, the JVM unmounts it and runs another one on the same carrier, so ordinary blocking code can serve tens of thousands of concurrent tasks. They don't make code faster or prevent data races, and they move the bottleneck to limited resources like connection pools.
In practice
Executors.newVirtualThreadPerTaskExecutor() can run 10,000 tasks that each sleep one second in about one second.
Learn it in context
Lessons that use this term.
Related terms
- CoroutineA function that can pause and resume later without blocking a thread, used for async code in Kotlin.
- async/awaitWriting asynchronous code that reads top to bottom like normal code, with await marking where it may pause.
- Structured concurrencyOrganizing concurrent work so every task has a parent that waits for it, cancels it and receives its errors.