Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: updated automated analysis (flashlight)

Wiki Markup
Code that uses synchronization can sometimes be enigmatic and tricky to debug. Misuse of synchronization primitives is a common source of implementation errors. An analysis of the JDK 1.6.0 source code unveiled at least 31 bugs that fell into this category. \[[Pugh 08|AA. Java References#Pugh 08]\]

...

Code Block
bgColor#ccccff
final Lock lock = new ReentrantLock();

public void doSomething() {
  lock.lock();
  try {
    // ...
  } finally {
    lock.unlock();
  }
}

It is recommended to not use the intrinsic locks of objects of classes that implement Lock or Condition interfaces. If there is no real need of using the advanced functionality of the dynamic locking utilities of package java.util.concurrent, prefer using the Executor framework or other concurrency primitives such as synchronization and atomic classes.

...

Noncompliant Code Example

Message

Boolean lock object

No obvious issues

Boxed primitive

No obvious issues

interned String object

No obvious issues

String literal

No data available about field accesses

getClass() lock object

No data available about field accesses

ReentrantLock lock object

No obvious issues

Collection view

No obvious issues

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...