Why this matters
Calling wait() while holding multiple locks can cause deadlocks if no other thread tries to acquire the awaited object. Always release locks before calling wait().
wait() When Holding Multiple LocksEnsure that wait() is not called while holding multiple locks to prevent deadlocks.
Calling wait() while holding multiple locks can cause deadlocks if no other thread tries to acquire the awaited object. Always release locks before calling wait().
Side-by-side examples engineers can pattern-match during review.
synchronized (this.mon1) { // threadB can't enter this block to request this.mon2 lock & release threadA
synchronized (this.mon2) {
this.mon2.wait(); // Noncompliant; threadA is stuck here holding lock on this.mon1
}
}// (no example provided)From the same buckets as this rule.