...
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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.
...
[API 2006] |
|
| |
Synchronization | |
Locking | |