<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=koi8-r">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I'll second what Alan said.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
What I've found to be true, over and over, is that every API needs its own (broad) performance testing, since memory access patterns, instructions-under-critical-section, and differences in scheduling across different platforms all interact.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
It's worth having a look at how busy-waiting is implemented in different java.util.concurrent constructs.</div>
<div id="Signature" class="elementToProof">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
–</div>
<div class="elementToProof" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div class="elementToProof" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div class="elementToProof" style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Alan Bateman <alan.bateman@oracle.com><br>
<b>Sent:</b> Monday, 16 June 2025 17:23<br>
<b>To:</b> Markus KARG <markus@headcrashing.eu><br>
<b>Cc:</b> core-libs-dev <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Re: Best Practice for Busy Waiting in Java</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">On 16/06/2025 12:09, Markus KARG wrote:<br>
><br>
><br>
> In case you MUST use busy-wait, apply the following rules:<br>
><br>
><br>
> * NEVER have EMPTY busy-wait loops, but ALWAYS put Thread.onSpinWait() <br>
> into it. The performance drop is negligible but the CO2 footprint is <br>
> considerably smaller.<br>
><br>
> * IF it is acceptable for the waiting thread to not have the <br>
> *absolute* maximum throughput, put Thread.yield() before <br>
> Thread.onSpinWait() in the busy-wait loop, so CPU cores are more <br>
> efficiently used.<br>
><br>
> * Never use Thread.sleep() in busy-wait loops.<br>
><br>
> * If possible, pin current thread to current CPU core to prevent <br>
> inefficient context switches.<br>
><br>
> * ...more rules...<br>
><br>
><br>
> THAT is what my question is targeting! :-)<br>
><br>
<br>
It may be possible to provide some guidelines but I don't think they can <br>
be turned into rules, e.g. the NEVER example is challenged by methods <br>
that do atomic increment/add/etc. as this read+CAS in a tight loop. <br>
There are also examples that of tight loops to CAS an object to the add <br>
it to a list. The examples of onSpinWait in the JDK might give you some <br>
ideas, e.g. using it in conjunction with a max spin count before timed <br>
back off or parking. One thing to add to your list is virtual threads <br>
where it may be better to park rather than Thread.yield.<br>
<br>
-Alan<br>
<br>
<br>
<br>
</div>
</span></font></div>
</body>
</html>