Java

Java

Made by DeepSource

Synchronization performed on a concurrency primitive object JAVA-E0321

Anti-pattern
Major

This method performs synchronization on an object that implements java.util.concurrent.locks.Lock.

Such an object is locked/unlocked using acquire()/release() rather than using the synchronized (...) construct.

Refactor the code to use the correct methods and constructs to achieve synchronization.

Bad Practice

Consider a reentrant lock created somewhere:

Lock someLock = new ReentrantLock();

// ...

Synchronizing on this lock is a wasteful operation, since the lock needn't have ever been created for this purpose.

synchronized (someLock) {
    // ...
}

Recommended

Use the lock's methods to synchronize your code instead:

someLock.lock();

// ...

someLock.unlock();

If you'd like to preserve synchronized-style scoping, as well as automatic locking/unlocking of the lock, you could use one of the solutions provided here.

References