JEP 285: Spin-Wait Hints in Java

December 04, 2018

Spin loop performance can be improved in some processors using a special instruction inside the spin loop code.

Spin loop, or busy waiting technique, is a loop where a certain condition is repeatedly checked, such as a flag. For example (this example is from the Java API doc):

volatile boolean eventNotificationNotReceived;

void waitForEventAndHandleIt() {
  // a spin loop
  while (eventNotificationNotReceived) {
    // code-sequence inside the spin loop
  }

  readAndProcessEvent();
}

Java 9 introduces Thread::onSpinWait method to give JVM a hint that the following code is in a spin loop. This has no side-effect and only provides a hint to optimize spin loops in a processor specific manner.

From Java 9 source code of java.lang.Thread:

@HotSpotIntrinsicCandidate
public static void onSpinWait() {}

Intel SSE2 PAUSE instruction is exactly provided for this reason and quoting Intel Software Developer Manual:

11.4.4.4 Pause

The PAUSE instruction is provided to improve the performance of “spin-wait loops” executed on a Pentium 4 or Intel Xeon processor. On a Pentium 4 processor, it also provides the added benefit of reducing processor power consumption while executing a spin-wait loop. It is recommended that a PAUSE instruction always be included in the code sequence for a spin-wait loop.

If PAUSE is seen on a pre-SSE2 architecture, it is interpreted as a no-op.

So a spin loop like above has to be hinted like this:

while (eventNotificationNotReceived) {
  java.lang.Thread.onSpinWait();
}

On Java 9, Thread::onSpinWait is already used in a few places in the platform classes.