<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>In general, I second do that. The point why I came up with a best
practices document is that in many companies the average
programmer is not an engineer, and would be happy to have some
rules at hand. It must not be perfect, it must be just better than
"empty" busy-wait.<br>
</p>
<p><br>
</p>
<div class="moz-cite-prefix">Am 17.06.2025 um 11:33 schrieb Viktor
Klang:<br>
</div>
<blockquote type="cite"
cite="mid:MW4PR10MB5774E5EA72C88D075F10E624FF70A@MW4PR10MB5774.namprd10.prod.outlook.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" style="display:none;">P {margin-top:0;margin-bottom:0;}</style>
<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>
<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 <a class="moz-txt-link-rfc2396E" href="mailto:core-libs-dev-retn@openjdk.org"><core-libs-dev-retn@openjdk.org></a> on behalf
of Alan Bateman <a class="moz-txt-link-rfc2396E" href="mailto:alan.bateman@oracle.com"><alan.bateman@oracle.com></a><br>
<b>Sent:</b> Monday, 16 June 2025 17:23<br>
<b>To:</b> Markus KARG <a class="moz-txt-link-rfc2396E" href="mailto:markus@headcrashing.eu"><markus@headcrashing.eu></a><br>
<b>Cc:</b> core-libs-dev <a class="moz-txt-link-rfc2396E" href="mailto:core-libs-dev@openjdk.org"><core-libs-dev@openjdk.org></a><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>
</blockquote>
</body>
</html>