'What's the resizable stack of JVM virtual threads?
The open jdk project "Loom" supports virtual thread which allows the classic IO-blocking API to enjoy the great benefits of async, reactor, coroutine development styles.
I searched it on youtube, I learned that the most important magic of virtual thread is the "resizable stack" in this video.
That's why millions of virtual threads can be created and blocked.
I am very interested in how the "resizable stack" is implemented.
- Is it a contiguous memory segment or a discontinuous linked list structure?
- Is its performance high enough?
- Function can be called by both platform threads and virtual threads. Will there be a performance penalty due to the need to determine what situation the current execution is in?
Solution 1:[1]
Virtual thread must run base on real thread.
The implementation of all blocking API must be changed, that's called virtual-thread friendly.
When virtual method call blocking API, virtual thread will be unmounted from the real thread so that real thread can continue to run other virtual threads that is not blocking.
When the virtual thread is unmounted, the information of current stack will be copied to heap, they are called stack chunk objects.
When the blocking API is finished, virtual thread will be mounted again to real thread, the information will be copy from heap to the stack so that the real thread can continue to run.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tao Chen |