Best Practice for Busy Waiting in Java
Chen Liang
chen.l.liang at oracle.com
Sun Jun 15 20:42:31 UTC 2025
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/20250615/cb96a8c8/attachment-0001.htm>
More information about the core-libs-dev
mailing list