Creating new virtual threads
As pointed out at the beginning of the previous section, native Java threads are managed by the JVM by working directly with the OS’s threading library. There is a one-to-one relationship between a native thread and an OS thread. This new approach is called virtual threads. While native threads are managed by the JVM in collaboration with the OS, virtual threads are managed exclusively by the JVM. OS threads are still used, but what makes this approach significant is that virtual threads can share OS threads and are no longer one-to-one.
Virtual threads do not run faster and can suffer from race and deadlock conditions. What is special with virtual threads is that the number of threads that you can start up could be in the millions. How we use virtual threads is not much different from native threads. The following code snippet shows the perform
method that we saw in the previous examples creating virtual threads. The thread class is unchanged...