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

org.sonar.l10n.java.rules.java.S2442.html Maven / Gradle / Ivy

Why is this an issue?

java.util.concurrent.locks.Lock offers far more powerful and flexible locking operations than are available with synchronized blocks. So synchronizing on a Lock instance throws away the power of the object, as it overrides its better locking mechanisms. Instead, such objects should be locked and unlocked using one of their lock and unlock method variants.

Noncompliant code example

Lock lock = new MyLockImpl();
synchronized(lock) {  // Noncompliant
  // ...
}

Compliant solution

Lock lock = new MyLockImpl();
if (lock.tryLock()) {
  try {
    // ...
  } finally {
    lock.unlock();
  }
}

Resources

  • CERT, LCK03-J. - Do not synchronize on the intrinsic locks of high-level concurrency objects




© 2015 - 2025 Weber Informatics LLC | Privacy Policy