Best Practice for Busy Waiting in Java
Markus KARG
markus at headcrashing.eu
Mon Jun 16 11:09:45 UTC 2025
Thank you, everybody, for your kind input!
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:
"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?"
It could look like this:
BUSY-WAIT BEST PRACICE
In case you MUST use busy-wait, apply the following rules:
* 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.
* 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.
* Never use Thread.sleep() in busy-wait loops.
* If possible, pin current thread to current CPU core to prevent
inefficient context switches.
* ...more rules...
THAT is what my question is targeting! :-)
Thanks!
-Markus
Am 16.06.2025 um 12:22 schrieb Per-Ake Minborg:
> 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-retn at openjdk.org> on behalf of
> Chen Liang <chen.l.liang at oracle.com>
> *Sent:* Sunday, June 15, 2025 10:42 PM
> *To:* Markus KARG <markus at headcrashing.eu>; Archie Cobbs
> <archie.cobbs at gmail.com>
> *Cc:* core-libs-dev <core-libs-dev at 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-retn at openjdk.org> on behalf of
> Markus KARG <markus at headcrashing.eu>
> *Sent:* Sunday, June 15, 2025 11:49:52 AM
> *To:* Archie Cobbs <archie.cobbs at gmail.com>
> *Cc:* core-libs-dev <core-libs-dev at 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 <markus at headcrashing.eu
>> <mailto:markus at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250616/de4870ea/attachment-0001.htm>
More information about the core-libs-dev
mailing list