As the thread-per-request programming style proves limiting for application scalability, developers have embraced thread sharing style programming to fully leverage hardware capabilities. However, this shift necessitates adopting asynchronous programming, which comes at a cost. Creating reactive pipelines, debugging them, and visualizing their execution can be complicated tasks, compounded by the complexities that arise when exceptions occur.
Java Virtual Threads enable applications to scale while preserving the thread-per-request style programming. Just as operating systems give the illusion of plentiful memory by mapping a large virtual address space to a limited amount of physical RAM, a Java runtime can give the illusion of plentiful threads by mapping a large number of virtual threads to a small number of OS threads.
Let's understand the java virtual thread and how it works under-the-hood. We'll explore some use cases for virtual threads and the APIs that the project Loom has introduced. Additionally, we'll compare traditional threads in Java with Virtual Threads through a simple programming example to experience the magical power of virtual threads in action.