A note from this newsletter's sponsor

The best way to consider spin-waits is to think of an ideal scenario
where you'd want to use them. So here's the scenario:

You have at least two threads that need to coordinate with a
stateful resource (required, otherwise spin-waits are wasteful)

One of the threads that needs coordination will wait for a state
change that another thread/process will update (required, otherwise
spin-waits are wasteful)

The processing in the waiting thread, after the condition changes,
needs to proceed AS SOON AS POSSIBLE; and you're willing to use extra
CPU to proceed faster (required, otherwise spin-waits are wasteful)

The thread waiting (spin-waiting) is isolated to a specific core
which has nothing else running on it (optional, but if you want that
"AS SOON AS POSSIBLE" requirement to apply, then not doing this means
it's not going to be as soon as possible)

The scenario is waiting for some state to change; and we want to
respond to that change as fast as we possibly can, so we just
repeatedly check the condition doing nothing else - looping on the
condition check (the "spin" part of the spin-wait). Note this
is an anti-pattern - if you had too much in the way of spin-wait, your
CPU could be fully utilised without doing very much at all. But to
achieve that "as soon as possible" hard requirement, it can be worth it.

Okay, that's spin-wait but about the JEP? So if the JVM knew it was
doing a spin-wait loop, on some hardware it is possible to optimize
the inter-thread comms a little. But it's difficult for the JVM to
know that the application is spin-waiting rather than just looping
doing something. The JEP provides a way for the application to tell the
JVM it's spin-waiting by calling the Thread.onSpinWait() method in
the body of the loop. On x86, this gives a 20 nanosecond improvement
to the latency in responding to the state change in the spin-wait.

When 20 nanoseconds per request matter to you, you need to know about Thread.onSpinWait().

Now on to our usual links to tools, articles, news, talks, blogs. And
if you need the tips from this month's articles and talks, as ever
they are extracted into
this month's tips page.