Virtual Thread

Virtual thread was added in Project Loom which meant to extends the support for concurrency in Java 19.

Definition

Virtual thread is a thread that controlled by the JVM instead of the OS. JVM uses the OS thread as the carrier for the virtual thread (we explore more on this later).

Therefore, 1 OS thread can run multiple virtual thread synchronously. However if there is multiple OS thread, we can run multiple virtual thread concurrently

How it works

Virtual thread memory is stored in a Stack instead of a Heap whereas the OS thread memory is stored in the heap.

Each virtual thread will be executed from 1 OS thread. As a result, we call this OS thread as the carrier of the virtual thread.

Pasted image 20230815160423.png

In this case, T1 (thread 1) could be handling V1 (Virtual thread 1) and V2.

To find the number of threads in MacOS, we can do

sysctl hw.physicalcpu hw.logicalcpu

And look at hw.logicalcpu

Thread pinning

By default, the virtual thread is not pinned to OS thread. So T1 could be handling V1 and V2 but later on could be handling some Vn.

However, if we're running synchronized block the virtual thread will be pinned to an OS thread .

Memory

Each virtual thread has its own memory in a heap. When an OS Thread take it, it will transfer the heap memory to OS Thread stack memory and execute there.

Blocking operation

When a virtual thread is being blocked (by using Sleep for example), the OS thread will execute other eligble virtual thread. When the virutal thread finished the blocking operation, the Processor Thread Scheduler will schedule it again for execution (could go to the same OS thread or different OS thread).

Read more: The Ultimate Guide to Java Virtual Threads - Rock the JVM Blog

[!danger]
Since virtual thread is a daemon thread. A JVM will exit if there is no daemon thread running. Therefore, if you're doing something like @Scheduled, the JVM will shutdown and led to unexpected behaviour.

If using springboot, this can be fixed by using spring.main.keep-alive=true