Inspectopedia Help

Synchronization on 'this'

Reports synchronization on this or class expressions. The reported constructs include synchronized blocks and calls to wait(), notify() or notifyAll().

There are several reasons synchronization on this or class expressions may be a bad idea:

  1. it makes synchronization a part of the external interface of the class, which makes a future change to a different locking mechanism difficult,

  2. it becomes hard to track just who is locking on a given object,

  3. it makes a denial-of-service attack possible, either on purpose or it can happen easily by accident when subclassing.

As an alternative, consider synchronizing on a private final lock object, access to which can be completely controlled.

Example:

public void print() { synchronized(this) { // warning: Lock operations on 'this' may have unforeseen side-effects System.out.println("synchronized"); } }

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.

SynchronizeOnThis
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 | Java | Threading issues

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Java, 242.22892

Last modified: 11 September 2024