Some questions about Virtual Thread for JDK21
Ron Pressler
ron.pressler at oracle.com
Fri Nov 22 23:14:37 UTC 2024
> On 22 Nov 2024, at 16:51, Weibing Xiao <weibing.xiao at oracle.com> wrote:
>
> Hello Everyone,
> I am researching virtual thread implementation in JDK21. I have some questions list below. Hope to get some answers and hints from our community.
>
Hi.
I’ve answered most of your questions below.
> • VT will be mounted to PT. It is M:N model. Do we have recommended M (number of VT) comparing with N (number of PT, typically it is equal to the number of cores of CPU)?
No. The number of virtual threads will always be equal to the number of concurrent tasks, regardless of the number of cores, because that’s what a virtual thread represents.
> • If VT is created, but never mounted to PT (never run), how do we find this situation?
Not running a virtual thread is similar to creating a Runnable and never submitting it. There is no way to discover that short of analysing a heap dump.
> • The default value for the parallelism is the number of CPU cores. Setting the parallelism to 256 (max allowed by JDK), is there any difference for the performance if number of CPU cores is small, ex, 10?
There may be some differences, but what they would be depends on too many factors. The default is probably a good starting point. Change it if you find there’s good reason to.
> • The default scheduler is set for VT. How can a developer to create their own scheduler?
There is no way to do this at the moment (that doesn’t require hacking into JDK internals). However, this is a feature that may be added someday.
> • The code can use ExecutorService or Thread builder to start VT. Is there any subtle difference?
There will be a difference in how the threads are presented in the new thread dump.
> • Beside current existing debug tool and skill , is there any other tool/way to debug the failure of VT?
> • The VT is saved as stack chunk objects. Per the document, if VT reaches half the region size, it might throw StackOverflowError. Typically, what will cause the size of VT growing?
> • When GC starts to work on chunk objects, how will it work? Any performance issue?
References are found the same way they are for platform threads — by inspecting the stack oopMap. Some GCs may cache this information in a way that’s more efficient. But the overall process is similar to all other Java objects, which is quite efficient.
> • If new feature is implemented by VT and required backporting to lower version of JDK, What are the recommendation and suggestion for sustaining engineer to help this work done correctly and error free?
Old releases are maintained primarily for the sake of legacy applications that aren’t heavily maintained anymore. As such, the goal is to minimise any risk for disruption, so, as a rule, we only backport security patches and fixes to the most severe, catastrophic bugs. Sometimes we also backport very local fixes that are judged to be risk-free. As a rule, we don’t backport new features or performance enhancements (there are exceptions in special circumstances).
Developers of applications that are under active maintenance and development — and can therefore benefit from enhancements — will find that their experience will be, overall, more pleasant if they use the most current JDK release.
> • Mount vs Unmount: what does JVM do? If this action fails, what will happen?
I gave a talk explaining the process here: https://youtu.be/6nRS6UiN7X0
An unmount can fail if the thread is pinned. In that situation it won’t be unmounted but will block the underlying OS thread. A mount can fail with a stack overflow, and will throw that error.
> • notifyJvmMount vs notifyJvmUnmount, what does JVM react to this call?
> • Is there any full stack trace from starting to the end when running VT?
Not sure what you’re asking here. Any thread stack trace, for any thread is from the start to the end. The old heap dump will show the stack of both a virtual thread and its carrier for mounted virtual threads.
> • How to duplicate “VirtualThreadSubmitFailedEvent”? if failed, what does it look like?
> • When the deadlock happens, is it possible to see how many PTs are available?
> Appreciate your help and time greatly.
> Weibing
— Ron
More information about the loom-dev
mailing list