<div dir="ltr">Does it have to be Thread.sleep(1) or is even Thread.yield() slow?<div><br></div><div>Attila</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Fredriksson, Christian <<a href="mailto:christian.fredriksson.2@volvocars.com">christian.fredriksson.2@volvocars.com</a>> ezt írta (időpont: 2025. nov. 21., P, 18:52):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-1625126602077230519">
<div lang="en-SE" style="overflow-wrap: break-word;">
<div class="m_-1625126602077230519WordSection1">
<p class="MsoNormal">Hey,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I believe I have found a bug with JDK 25 and virtual threads, but will need some guidance in what information I need to provide in order to file a bug.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Short version: after changing from JDK 21 to JDK 25, doing Thread.sleep(1) in a virtual thread now sometimes takes up to 60 seconds (!).<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Long version:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I recently updated an application from Java 21 to 25 and started seeing a very negative performance impact.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The application in question runs ca 20 long running virtual threads which basically does the following:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">while (true) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> doTrivialWork();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> Thread.sleep(1);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> semaphore.acquire();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">}<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I.e. do trivial work, sleep 1 ms, wait on a semaphore to be <span lang="en-SE">
woken</span> by another thread, and then <span lang="en-SE">loop</span> again. After long investigation I finally realized that it was the Thread.sleep that sometimes took significantly longer with Java 25 than Java 21.<u></u><u></u></p>
<p class="MsoNormal">So I timed it, basically just this:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">while (true) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> doTrivialWork();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> var start = System.nanoTime();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> Thread.sleep(1);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> var end = System.nanoTime();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> System.out.println("delay=" + (end - start));<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> semaphore.acquire();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">}<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">And let it run >1 million times.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">With JDK 21 and either virtual threads or platform threads, or with latest JDK 25 and platform threads, it works fine and the Thread.sleep(1) finishes according to the following:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">99,5% finish within 2 ms<u></u><u></u></p>
<p class="MsoNormal">0,4% finish within 4 ms<u></u><u></u></p>
<p class="MsoNormal">Worst case 80 ms<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">But with latest JDK 25 and virtual threads, it’s significantly worse:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">98,0% finish within 2 ms<u></u><u></u></p>
<p class="MsoNormal">0,3% finish within 4 ms<u></u><u></u></p>
<p class="MsoNormal">0,5% taking over 1000 ms<u></u><u></u></p>
<p class="MsoNormal">A few hundred occurrences over 60 seconds (!)<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The only change between these runs is JDK version and platform vs virtual threads.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Increasing <span lang="en-SE">jdk.virtualThreadScheduler.parallelism</span> and
<span lang="en-SE">jdk.virtualThreadScheduler.timerQueues</span><span lang="en-SE">
</span>makes no difference.<u></u><u></u></p>
<p class="MsoNormal">There are no jdk.VirtualThreadPinned or jdk.VirtualThreadSubmitFailed events in JFR.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Additional information:<u></u><u></u></p>
<p class="MsoNormal">The application is running in a pod on k8s with 1 CPU allocated to it.<u></u><u></u></p>
<p class="MsoNormal">No additional arguments for the JDK.<u></u><u></u></p>
<p class="MsoNormal">CPU utilization is less than 15%.<u></u><u></u></p>
<p class="MsoNormal">GC happens very infrequently (many minutes apart).<u></u><u></u></p>
<p class="MsoNormal"><span lang="en-SE"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif"> <u></u><u></u></span></p>
<table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse">
<tbody>
<tr style="height:217.75pt">
<td width="74" valign="top" style="width:55.65pt;padding:2.4pt 3.75pt 2.4pt 2.4pt;height:217.75pt">
<p class="MsoNormal"><span lang="en-SE"><a href="http://volvocars.com/" target="_blank"><span lang="en-SE" style="font-family:Calibri,sans-serif;color:blue"><img border="0" width="66" height="66" style="width: 0.6875in; height: 0.6875in;" id="m_-1625126602077230519Picture_x0020_1" src="cid:ii_19aaae194994cdccc1" alt="VOLVO CAR CORPORATION"></span></a></span><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
</td>
<td width="187" valign="top" style="width:140.3pt;padding:2.4pt 2.4pt 2.4pt 12.75pt;height:217.75pt">
<p class="MsoNormal" style="margin-bottom:10pt"><span style="font-size:14pt;font-family:Calibri,sans-serif;color:black">Christian<br>
Fredriksson</span><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8pt;font-family:Calibri,sans-serif;color:black"><br>
T +46 734 63 71 06<br>
<u><a href="mailto:christian.fredriksson.2@volvocars.com" target="_blank"><span style="color:black">christian.fredriksson.2@volvocars.com</span></a></u><br>
<br>
VOLVO CAR CORPORATION<br>
Connected Car Cloud<br>
<u><a href="http://volvocars.com" target="_blank"><span style="color:black">volvocars.com</span></a></u></span><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal" style="margin-bottom:10pt"><span style="font-size:8pt;font-family:Calibri,sans-serif;color:rgb(127,127,127)">Registered Office Göteborg, Sweden<br>
Registration No. 556074-3089</span><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-family:"Times New Roman",serif"> <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Times New Roman",serif"> </span><span lang="en-SE"><u></u><u></u></span></p>
</div>
</div>
</div></blockquote></div>