java.util.concurrent.locks.Lock
offers far more powerful and flexible locking operations than are available with
synchronized
blocks. So synchronizing on a Lock
instance throws away the power of the object, as it overrides its better
locking mechanisms. Instead, such objects should be locked and unlocked using one of their lock
and unlock
method
variants.
Lock lock = new MyLockImpl(); synchronized(lock) { // Noncompliant // ... }
Lock lock = new MyLockImpl(); if (lock.tryLock()) { try { // ... } finally { lock.unlock(); } }