...
| Code Block | ||
|---|---|---|
| ||
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.
...