Inspectopedia Help

While loop spins on field

Reports while loops, which 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 likely have different semantics than intended. The Java Memory Model allows that loop to never complete even if another thread changes the field's value.

Example:

class SpinsOnField { boolean ready = false; void run() { // the loop may never complete even after // markAsReady call from the other thread while (!ready) { } // do some work } void markAsReady() { ready = true; } }

Additionally since Java 9, calling Thread.onSpinWait() inside spin loop on a volatile field is recommended, which may significantly improve performance on some hardware.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

GroovyWhileLoopSpinsOnField
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Groovy | Threading issues

Use the checkbox below to have this inspection report only empty while loops.

Inspection options

Here you can find the description of settings available for the While loop spins on field inspection, and the reference of their default values.

Only warn if loop is empty

Not selected

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Groovy, 241.18072

Last modified: 18 June 2024