Best Practice for Busy Waiting in Java
Markus KARG
markus at headcrashing.eu
Sun Jun 15 16:08:38 UTC 2025
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
More information about the core-libs-dev
mailing list