JD.LOCK.WAIT

JD.LOCK.WAIT occurs when an Object.wait() method is called while the method is holding two or more locks. This checker checks only local locks.

Vulnerability and risk

Waiting on a monitor while two locks are held may cause deadlock. Performing a wait releases the lock only on the object being waited on, not on any other locks. Not necessarily a problem.

Mitigation and prevention

Preferably, call wait() method without lock held, or with a lock on the same object for which the wait is called.

Example 1

9      String name;
10     synchronized void waitForCondition(Object lock) {
11         try {
12             synchronized(lock) {
13                 name = "aa";
14                 lock.wait();
15             }
16         } catch (InterruptedException e) {
17             return;
18         }
19     }

JD.LOCK.WAIT is reported for line 14: Calling 'java.lang.Object.wait()' with two or more locks held '[lock, this]' could cause a deadlock.

Related checkers