V6126. Native synchronization used on high-level concurrency class.
The analyzer has detected that a subclass of java.util.concurrent.locks.Lock
, java.util.concurrent.locks.Condition
, java.util.concurrent.locks.ReadWriteLock
, and/or java.util.concurrent.locks.StampedLock
is passed to the synchronized
statement.
These classes belong to the category of high-level concurrency classes, and their use with synchronized
is incorrect. Instead, it is recommended to use the locking mechanisms provided directly by these classes.
When synchronized
is used with such objects, it creates two independent synchronization systems. One system is based on the object monitor (via synchronized
), and the other is based on the internal locking mechanism of the class. This can lead to the following issues:
- broken synchronization integrity;
- race conditions;
- deadlocks;
- unexpected program behavior.
The incorrect code:
Lock lock = new ReentrantLock();
synchronized(lock) {
....
}
The fixed code:
Lock lock = new ReentrantLock();
lock.lock();
try {
....
} finally {
lock.unlock();
}
This diagnostic is classified as:
|