← Lessons

quiz vs the machine

Platinum1780

Concurrency

Virtual Threads And Green Threads

Cheap user space threads that block without pinning OS threads.

6 min read · advanced · beat Platinum to climb

Virtual Threads And Green Threads

Virtual threads, a modern form of green threads, give you the simple blocking style of thread per request without its scaling ceiling. They are lightweight threads scheduled by the runtime onto a small set of operating system carrier threads.

The trick is mounting and unmounting. A virtual thread runs on a carrier thread. When it hits a blocking call such as a network read, the runtime unmounts it, parking its tiny stack and freeing the carrier to run another virtual thread. When the IO completes, the virtual thread remounts on some carrier and continues. Blocking code thus costs almost nothing, since a parked virtual thread holds only a small heap stack, not an OS thread.

This collapses the old dilemma:

  • Old Thread per request was simple but capped at a few thousand threads.
  • Old Async callbacks scaled but fragmented logic and hurt stack traces.
  • New Virtual threads scale to millions while keeping sequential, debuggable code.

Caveats remain. CPU bound work gains nothing, since it does not yield. Some operations pin a virtual thread to its carrier, such as holding certain native locks, which can starve carriers if widespread. Green threads in earlier runtimes lacked true parallelism, but modern virtual threads run across all cores.

Key idea

Virtual threads unmount on blocking calls to free carrier threads, scaling simple blocking code to millions of concurrent tasks.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens when a virtual thread hits a blocking IO call?

2. Which workload gains little from virtual threads?