All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.squid.S2445.html Maven / Gradle / Ivy

There is a newer version: 8.6.0.37351
Show newest version

Synchronizing on a class field synchronizes not on the field itself, but on the object assigned to it. So reassigning a field in a block synchronized on that field's contents immediately opens the block up to access by another thread.

Noncompliant Code Example

private String color = "red";

private void doSomething(){
  synchronized(color) {  // lock is actually on object instance "red" referred to by the color variable
    //...
    color = "green"; // Noncompliant; other threads now allowed into this block
    // ...
  }
}

Compliant Solution

private String color = "red";
private Object lockObj = new Object();

private void doSomething(){
  synchronized(lockObj) {
    //...
    color = "green";
    // ...
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy