Virtual threads created more platform threads

Jianbin Chen jianbin at apache.org
Thu Jul 3 06:47:53 UTC 2025


Hello everyone

Has anyone taken a look at my example? I'm really curious to know why JDK
21 cannot detect pinned platform thread behavior in this particular
example. If someone could help explain this, I would be extremely grateful!

Best Regards.
Jianbin Chen, github-id: funky-eyes

Jianbin Chen <jianbin at apache.org> 于 2025年7月3日周四 09:14写道:

> Sorry to bother you all again. When using virtual threads on JDK 21, I've
> always been concerned about potential pinning situations, so during testing
> in our offline environment, we always add the -Djdk.tracePinnedThreads=full
> parameter. However, we have never seen any pinned-related information
> output. Yesterday I conducted a test and found that when using synchronized
> + object.wait(), even though the maximum number of platform threads used by
> virtual threads has already reached the limit, it still cannot output
> pinned-related logs. But if I switch to synchronized + Thread.sleep(), it
> can output the logs. I'm providing my example and JVM parameters below,
> hoping someone can help explain this issue. Thank you very much.
>
> JVM parameters: -Djdk.tracePinnedThreads=full
> -Djava.util.concurrent.ForkJoinPool.common.parallelism=2
> -Djdk.virtualThreadScheduler.parallelism=2
> -Djava.util.concurrent.ForkJoinPool.common.maximumSpares=2
> -Djdk.virtualThreadScheduler.maxPoolSize=2
>
> ```
> public static void main(String[] args) throws InterruptedException {
>     Executor executor =
> ThreadPoolFactory.newVirtualThreadPerTaskExecutor();
>     List<String> list = new ArrayList<>();
>     for (int i = 0; i < 20; i++) {
>         list.add(String.valueOf(i));
>     }
>     for (int i = 0; i < list.size(); i++) {
>         String value = list.get(i);
>         executor.execute(() -> {
>             synchronized (value) {
>                 try {
>                     value.wait();
>                 } catch (InterruptedException e) {
>                     throw new RuntimeException(e);
>                 }
>             }
>         });
>     }
>     Thread.sleep(120000);
> }
> ```
>
> Best Regards.
> Jianbin Chen, github-id: funky-eyes
>
> Alan Bateman <alan.bateman at oracle.com> 于 2025年7月2日周三 17:19写道:
>
>>
>>
>> On 02/07/2025 09:27, Andrew Haley wrote:
>> > On 02/07/2025 05:15, Peter Eastham wrote:
>> >> As Java 21 is still the current LTS, it isn't completely unreasonable
>> to
>> >> forward concerns to the mailing list.
>> >
>> > Sure, we'e happy to talk about it, but as far as this group is
>> > concerned the issue has been resolved.
>>
>> Right, and as a reminder: the changes were to the lightweight locking
>> mode only (not legacy) and that mode only became the default in JDK 22.
>> There is also a prerequisite spec change to JVMTI GetObjectMonitorUsage
>> that happened in JDK 23. It also seems pointless given that JDK 24 is
>> already released and JDK 25 is very close.
>>
>> -Alan
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250703/d01b63fb/attachment.htm>


More information about the loom-dev mailing list