Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
public void doSomething() {
  final Lock lock = new ReentrantLock();
  lock.lock();
  try {
    // Do something with the protected resource
    // This may cause an exception such as FileNotFoundException
  } catch(FileNotFoundException fnf) {
    // Handle the exception
  } finally {
    lock.unlock();
  }
}

Exceptions

EX1 : Locks associated with the use of the synchronized keyword are automatically released on exceptional conditions such as abnormal thread termination.

Risk Assessment

Failing to release a lock on an exceptional condition may lead to thread starvation and deadlock.

...