<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Thank you, everybody, for your kind input!</p>
<p><br>
</p>
<p>Having said that, I fully agree with all your proposals, but need
to say: I already knew them, and it was **not** what I actually
asked for. To repeat: "Is there a *best practice*". If the
question is unclear, let me rephrase it using an example:</p>
<p><br>
</p>
<p>"Is there a document that contains a short and simple rule set
that we (the core-libs contributors) apply in busy-wait cases BY
DEFAULT when we do *not* decide to explicitly optimize for any
special case?"</p>
<p><br>
</p>
<p>It could look like this:</p>
<p><br>
</p>
<p>BUSY-WAIT BEST PRACICE</p>
<p><br>
</p>
<p>In case you MUST use busy-wait, apply the following rules:<br>
</p>
<p><br>
</p>
<p>* NEVER have EMPTY busy-wait loops, but ALWAYS put
Thread.onSpinWait() into it. The performance drop is negligible
but the CO2 footprint is considerably smaller.<br>
</p>
<p>* IF it is acceptable for the waiting thread to not have the
*absolute* maximum throughput, put Thread.yield() before
Thread.onSpinWait() in the busy-wait loop, so CPU cores are more
efficiently used.<br>
</p>
<p>* Never use Thread.sleep() in busy-wait loops.</p>
<p>* If possible, pin current thread to current CPU core to prevent
inefficient context switches.<br>
</p>
<p>* ...more rules...</p>
<p><br>
</p>
<p>THAT is what my question is targeting! :-)</p>
<p><br>
</p>
<p>Thanks!</p>
<p>-Markus<br>
</p>
<p><br>
</p>
<div class="moz-cite-prefix">Am 16.06.2025 um 12:22 schrieb Per-Ake
Minborg:<br>
</div>
<blockquote type="cite"
cite="mid:SA1PR10MB7832C9FE08B822E090C61743DA70A@SA1PR10MB7832.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);">
Hi Markus, and thank you for your question, which I think is a
very relevant one.</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);">
There is no silver bullet here. It all depends on the problem at
hand. One of the major questions to ask is what the aim of the
solution is: are you optimizing latency, throughput, power
efficiency, fairness, a certain SLA etc.</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);">
If you have considered asynchronous programming, I would
encourage you to instead take a look at virtual threads, which
provide a much more robust programming model. I think this is,
indeed, a best practice approach in this type of choices.</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; color: rgb(0, 0, 0);">
<span style="font-size: 12pt;">If you are dealing with latency
sensitive stuff, sometimes starting out with a busy wait
(calling
</span><span style="font-size: 11pt;">Thread.onSpinWait()) and
then progressively backing off to yield() and wait() could be
a good strategy. This can be combined with thread pinning and
CPU isolation on the OS level to get improved latency metrics.
Sometimes, event loops are a good choice in this domain.</span></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);">
Best, Per Minborg</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 Chen Liang <a class="moz-txt-link-rfc2396E" href="mailto:chen.l.liang@oracle.com"><chen.l.liang@oracle.com></a><br>
<b>Sent:</b> Sunday, June 15, 2025 10:42 PM<br>
<b>To:</b> Markus KARG <a class="moz-txt-link-rfc2396E" href="mailto:markus@headcrashing.eu"><markus@headcrashing.eu></a>; Archie
Cobbs <a class="moz-txt-link-rfc2396E" href="mailto:archie.cobbs@gmail.com"><archie.cobbs@gmail.com></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>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
Not a concurrency professional, but my first impression is
whether to yield or spin depends on how costly the task you
are waiting is - I know yield involves a context switch and
can be problematic for small waits.</div>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
In addition, in Java, many blocking happens in a way users
cannot control - for example, class initialization. Those
might be bigger bottlenecks compared to the explicit busy
waits.</div>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
Chen</div>
<div dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div id="x_ms-outlook-mobile-body-separator-line" dir="auto"><br>
</div>
<div id="x_ms-outlook-mobile-signature" dir="auto"
style="font-family:Aptos,Aptos_MSFontService,-apple-system,Roboto,Arial,Helvetica,sans-serif; font-size:12pt">
Get <a href="https://aka.ms/AAb9ysg" moz-do-not-send="true">Outlook
for Android</a></div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font
face="Calibri, sans-serif" color="#000000"
style="font-size:11pt"><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 Markus
KARG <a class="moz-txt-link-rfc2396E" href="mailto:markus@headcrashing.eu"><markus@headcrashing.eu></a><br>
<b>Sent:</b> Sunday, June 15, 2025 11:49:52 AM<br>
<b>To:</b> Archie Cobbs <a class="moz-txt-link-rfc2396E" href="mailto:archie.cobbs@gmail.com"><archie.cobbs@gmail.com></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>
<p>Seems you misunderstood my question. It was *not* what is
best to do. It was: "Does the core-libs team have a
common-sense / best practice for busy-wait.". The latter is
a clear and concise question, and the answer could be as
simple as "yes" or "no".<br>
</p>
<div class="x_x_moz-cite-prefix">Am 15.06.2025 um 18:40
schrieb Archie Cobbs:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div>Just MHO... </div>
<div><br>
</div>
<div>This is kind of like asking "What's the best way to
waste electricity?"</div>
<div><br>
</div>
<div>It's a nebulous question until you specify what
"best" means in this odd scenario.... </div>
<div><br>
</div>
<div>-Archie</div>
</div>
<br>
<div class="x_x_gmail_quote x_x_gmail_quote_container">
<div dir="ltr" class="x_x_gmail_attr">On Sun, Jun 15, 2025
at 11:09 AM Markus KARG <<a
href="mailto:markus@headcrashing.eu"
class="x_x_moz-txt-link-freetext moz-txt-link-freetext"
moz-do-not-send="true">markus@headcrashing.eu</a>>
wrote:<br>
</div>
<blockquote class="x_x_gmail_quote"
style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
Recently I was asked by a programmer, what to do if
there simply is no <br>
other way than actually busy-wait.<br>
<br>
I see several options:<br>
<br>
* Do nothing: Costs valuable CPU time and increases
carbon footprint.<br>
<br>
* Do a power-nap: Thread.sleep(1)<br>
<br>
* Be fair: Thread.yield() gives other threads a chance
to execute in <br>
this time slot.<br>
<br>
* Be eco-friendly: Thread.onSpinWait() could reduce
carbon footprint.<br>
<br>
* A combination of that, like "Thread.yield();
Thread.onSpinWait();"<br>
<br>
As all of that has its pros and cons, and as all of that
works <br>
differently on each hardware platform, I do wonder if
there is some <br>
common sense / best practice for busy wait (other than
"Replace it using <br>
async") in the core-libs team?<br>
<br>
Thanks!<br>
<br>
-Markus<br>
<br>
<br>
<br>
</blockquote>
</div>
<div><br clear="all">
</div>
<br>
<span class="x_x_gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="x_x_gmail_signature">Archie L. Cobbs<br>
</div>
</blockquote>
</div>
</div>
</blockquote>
</body>
</html>