<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
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 style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
<br>
</div>
<div style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
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 style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
<br>
</div>
<div style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
Chen</div>
<div style="font-family: Aptos, Aptos_MSFontService, -apple-system, Roboto, Arial, Helvetica, sans-serif; font-size: 12pt;" dir="auto">
<br>
</div>
<div id="ms-outlook-mobile-body-separator-line" dir="auto"><br>
</div>
<div id="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">Outlook for Android</a></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 Markus KARG <markus@headcrashing.eu><br>
<b>Sent:</b> Sunday, June 15, 2025 11:49:52 AM<br>
<b>To:</b> Archie Cobbs <archie.cobbs@gmail.com><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>
<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_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_gmail_quote x_gmail_quote_container">
<div dir="ltr" class="x_gmail_attr">On Sun, Jun 15, 2025 at 11:09 AM Markus KARG <<a href="mailto:markus@headcrashing.eu" class="x_moz-txt-link-freetext">markus@headcrashing.eu</a>> wrote:<br>
</div>
<blockquote class="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_gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="x_gmail_signature">Archie L. Cobbs<br>
</div>
</blockquote>
</div>
</body>
</html>