Virtual Threads

  • Officially part of Java 21
  • Part of Project Loom https://wiki.openjdk.org/display/loom/Main
  • Maybe they are inspired by Go coroutines?
  • They were originally called fibers
  • We can now categorize threads in java:
    • Platform Threads
    • Virtual Threads
  • Platform Threads is implemented as a thin wrapper around an OS thread, and hence are precious resource
    • captures its OS thread for the platform thread’s entire lifetime
    • number of available platform threads is limited to the number of OS threads
  • Virtual Threads are lightweight threads that reduce the effort of writing, maintaining, and debugging high-throughput concurrent applications.
    • it isn’t tied to a specific OS thread, although the code runs on an OS thread.
    • when code running in a virtual thread calls a blocking I/O operation, the Java runtime suspends the virtual thread until it can be resumed.
    • OS thread associated with the suspended virtual thread is now free to perform operations for other virtual threads.
    • similar to how virtual memory simulates RAM, the Java runtime maps a large number of virtual threads to a small number of OS threads.
    • a single JVM might support millions of virtual threads.
  • When to use Virtual Threads:
    • Use virtual threads in high-throughput concurrent applications, especially those that consist of a great number of concurrent tasks that spend much of their time waiting.
    • They exist to provide scale (higher throughput), not speed (lower latency).