Do Not Use synchronized on a Lock Object
ID |
java.no_synchronize_on_lock_object |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
concurrency, reliability |
Description
Reports synchronized blocks that lock on a java.util.concurrent.locks.Lock instance. Lock objects have their own lock()/unlock() protocol and should not be used with intrinsic synchronization.
Rationale
Lock objects from java.util.concurrent.locks provide a richer locking API (try-lock, timed lock, interruptible lock, read-write locks) than intrinsic monitors. Using synchronized on a Lock object only acquires the object’s intrinsic monitor, not the Lock itself. This means other code calling lock.lock() will not see the synchronization, creating a subtle concurrency bug.
// Bad -- acquires the intrinsic monitor, not the Lock
Lock lock = new ReentrantLock();
synchronized (lock) {
doWork();
}
Remediation
Use the Lock API directly:
// Good -- uses the Lock protocol
Lock lock = new ReentrantLock();
lock.lock();
try {
doWork();
} finally {
lock.unlock();
}