[Draft] Programming with Virtual Threads - vinhtbkit/bkit-kb GitHub Wiki

Virtual Threads

  • JDK 21
  • From Project Loom
  • Is also a Thread: use as Platform Thread, have ThreadLocal

Virtual Threads vs Platform Thread

- Virtual Thread Platform Thread
Allocation On Platform Thread On OS Thread
Size 200 ~ 300 bytes 2~4 Mb
  • We can allocate much more virtual threads than platform threads
  • Virtual threads DON'T make programs run faster, but it makes scaling much better

Context Switching

TODO: more details

Virtual Threads mechanism

  • JVM maintains a pool of platform threads, created and maintained by a dedicated ForkJoinPool
  • size = min (256, CPU cores)
  • Each virtual thread is created, then the JVM schedules its execution on a platform thread.
    • The Platform thread is called the Carrier Thread A lil Random Carrier Rush on Starcraft: Brood War
  • One platform thread can only execute a virtual thread at a time
  • When the virtual thread blocks on a blocking operation, the carrier thread select the next virtual thread in queue
  • When virtual thread finish its blocking task, it is scheduled again for next execution

Utilizing Virtual Threads for highly concurrency apps

What Platform Threads stuck

  • Size
  • Resources Limitation: Number of cores
  • Context Switching

For Highly concurrency applications, using Threads is not possible solution

Future?

  • Also uses a Thread Pool, which uses Platform Thread => Same issues

Virtual Threads proposal

  • For any time-consuming tasks, run it using a Virtual Thread
    • E.g: API calls, database queries, files I/O...