Hi Markus, and thank you for your question, which I think is a very relevant 
one.

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.

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.

If you are dealing with latency sensitive stuff, sometimes starting out with a 
busy wait (calling 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.

Best, Per Minborg
________________________________
From: core-libs-dev <core-libs-dev-r...@openjdk.org> on behalf of Chen Liang 
<chen.l.li...@oracle.com>
Sent: Sunday, June 15, 2025 10:42 PM
To: Markus KARG <mar...@headcrashing.eu>; Archie Cobbs <archie.co...@gmail.com>
Cc: core-libs-dev <core-libs-dev@openjdk.org>
Subject: Re: Best Practice for Busy Waiting in Java

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.

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.

Chen


Get Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
From: core-libs-dev <core-libs-dev-r...@openjdk.org> on behalf of Markus KARG 
<mar...@headcrashing.eu>
Sent: Sunday, June 15, 2025 11:49:52 AM
To: Archie Cobbs <archie.co...@gmail.com>
Cc: core-libs-dev <core-libs-dev@openjdk.org>
Subject: Re: Best Practice for Busy Waiting in Java


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".

Am 15.06.2025 um 18:40 schrieb Archie Cobbs:
Just MHO...

This is kind of like asking "What's the best way to waste electricity?"

It's a nebulous question until you specify what "best" means in this odd 
scenario....

-Archie

On Sun, Jun 15, 2025 at 11:09 AM Markus KARG 
<mar...@headcrashing.eu<mailto:mar...@headcrashing.eu>> wrote:
Recently I was asked by a programmer, what to do if there simply is no
other way than actually busy-wait.

I see several options:

* Do nothing: Costs valuable CPU time and increases carbon footprint.

* Do a power-nap: Thread.sleep(1)

* Be fair: Thread.yield() gives other threads a chance to execute in
this time slot.

* Be eco-friendly: Thread.onSpinWait() could reduce carbon footprint.

* A combination of that, like "Thread.yield(); Thread.onSpinWait();"

As all of that has its pros and cons, and as all of that works
differently on each hardware platform, I do wonder if there is some
common sense / best practice for busy wait (other than "Replace it using
async") in the core-libs team?

Thanks!

-Markus





--
Archie L. Cobbs

Reply via email to