Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed Executor reference

...

The doSomething() method in this noncompliant code example synchronizes on the intrinsic lock of an instance of ReentrantLock rather than on the reentrant mutual exclusion Lock encapsulated by ReentrantLock.

Code Block
bgColor#FFcccc

private final Lock lock = new ReentrantLock();

public void doSomething() {
  synchronized(lock) {
    // ...
  }
}

...

This compliant solution uses the lock() and unlock() methods provided by the Lock interface.

Code Block
bgColor#ccccff

private final Lock lock = new ReentrantLock();

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

In the absence of a requirement for the advanced functionality of the java.util.concurrent package's dynamic-locking utilities, it is better to use the Executor framework or other use other concurrency primitives such as synchronization and atomic classes.

...

 

      08. Locking (LCK)