-
Bug
-
Resolution: Not an Issue
-
P5
-
None
-
6
-
x86
-
linux
A DESCRIPTION OF THE PROBLEM :
It is recommended practice to always immediately follow a call to lock with a try block, most typically in a before/after construction such as:
class X {
The following example code is given:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
</quote>
I believe that the call to lock should be inside the try-finally block as if the thread is interrupted after locking but before entering the try block it could leave m() without unlocking the lock. Thus the code would have to be:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
try {
lock.lock(); // block until condition holds
// ... method body
} finally {
lock.unlock()
}
}
}
</quote>
This means that unlock with necessarily be called. However, if the thread is interrupted before lock() is called, unlock() will be called and will trigger an exception, thus I think the example code should be:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
try {
lock.lock(); // block until condition holds
// ... method body
} finally {
try {
lock.unlock()
} catch (IllegalMonitorStateException e) {
// Ignore
}
}
}
}
</quote>
URL OF FAULTY DOCUMENTATION :
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html
It is recommended practice to always immediately follow a call to lock with a try block, most typically in a before/after construction such as:
class X {
The following example code is given:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
</quote>
I believe that the call to lock should be inside the try-finally block as if the thread is interrupted after locking but before entering the try block it could leave m() without unlocking the lock. Thus the code would have to be:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
try {
lock.lock(); // block until condition holds
// ... method body
} finally {
lock.unlock()
}
}
}
</quote>
This means that unlock with necessarily be called. However, if the thread is interrupted before lock() is called, unlock() will be called and will trigger an exception, thus I think the example code should be:
<quote>
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
try {
lock.lock(); // block until condition holds
// ... method body
} finally {
try {
lock.unlock()
} catch (IllegalMonitorStateException e) {
// Ignore
}
}
}
}
</quote>
URL OF FAULTY DOCUMENTATION :
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html