Wiki Markup |
---|
An exceptional condition may circumvent the release of a lock. This can result in thread starvation and deadlock. According to the Java API, class {{ReentrantLock}} documentation \[[API 06|AA. Java References#API 06]\], class {{ReentrantLock}} documentation: |
A
ReentrantLock
is owned by the thread last successfully locking, but not yet unlocking it. A thread invokinglock
will return, successfully acquiring the lock, when the lock is not owned by another thread.
...
Code Block | ||
---|---|---|
| ||
public void doSomething() { final Lock lock = new ReentrantLock(); try { lock.lock(); // doDo something with the protected resource // This may cause an exception such as FileNotFoundException lock.unlock(); } catch(FileNotFoundException fnf) { // handleHandle the exception } } |
Note that the lock is not released even when the doSomething()
method returns.
...
Code Block | ||
---|---|---|
| ||
public void doSomething() { final Lock lock = new ReentrantLock(); lock.lock(); try { // doDo something with the protected resource // This may cause an exception such as FileNotFoundException } catch(FileNotFoundException fnf) { // handleHandle the exception } finally { lock.unlock(); } } |
Risk Assessment
...