<html>
<head>
<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>
</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);">
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>
<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 Chen Liang <chen.l.liang@oracle.com><br>
<b>Sent:</b> Sunday, June 15, 2025 10:42 PM<br>
<b>To:</b> Markus KARG <markus@headcrashing.eu>; 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>
<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">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 <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_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">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>
</body>
</html>