Reports while loops that spin on the value of a non-volatile field, waiting for it to be changed by another thread.

In addition to being potentially extremely CPU intensive when little work is done inside the loop, such loops are likely to have different semantics from what was intended. The Java Memory Model allows such loop to never complete even if another thread changes the field's value.

Example:


  class SpinsOnField {
    boolean ready = false;

    void run() {
      while (!ready) { // the loop may never complete even after markAsReady call
                       // from the other thread
      }
      // do some work
    }

    void markAsReady() {
      ready = true;
    }
  }

Additionally, since Java 9 it's recommended to call Thread.onSpinWait() inside a spin loop on a volatile field, which may significantly improve performance on some hardware.

Use the inspection options to only report empty while loops.