Java / Java 21 Virtual Threads Interview questions
How does virtual thread creation cost compare to platform threads?
Creating a platform thread involves the OS allocating and page-mapping a full native stack (often ~1MB) plus kernel-level bookkeeping, which typically costs on the order of tens of microseconds to low milliseconds and real memory.
Creating a virtual thread instead allocates a small continuation object on the Java heap - no OS thread, no reserved native stack. The cost is closer to allocating a regular object, orders of magnitude cheaper.
That gap is exactly why a JVM comfortably hosting a few thousand platform threads can host millions of virtual threads at once without exhausting memory or OS resources.
More Related questions...