Class NotifyWithoutMonitorDetector

java.lang.Object
se.deversity.asynctest.diagnostics.NotifyWithoutMonitorDetector

public final class NotifyWithoutMonitorDetector extends Object
Detects attempted Object.notify() / Object.notifyAll() calls where the calling thread does not hold the target monitor.

Why it matters. Java mandates that notify() and notifyAll() only be called while holding the lock on the object being notified. The JVM enforces this at runtime by throwing IllegalMonitorStateException. In production, that exception is usually caught at a high level (e.g. a worker's catch-all) and silently swallowed, leaving the actual wait()-ers blocked forever and the symptom looking like a deadlock rather than a missed signal.

This detector lets test code declare a notify attempt without actually invoking the JDK call:


 synchronized (mutex) {
     d.recordNotifyAttempt(mutex, "queue-not-empty");
     mutex.notifyAll();   // would throw IllegalMonitorStateException if not synchronized
 }
 

The detector samples Thread.holdsLock(Object) at the moment the attempt is recorded. If the calling thread does not hold the monitor, the call is flagged.

Complements MissedSignalDetector (which catches a notify with no waiter that a later wait misses) by flagging the inverse: notifies that are illegal regardless of whether anyone is waiting.

Since:
1.6.0
  • Constructor Details

    • NotifyWithoutMonitorDetector

      public NotifyWithoutMonitorDetector()
  • Method Details

    • recordNotifyAttempt

      public void recordNotifyAttempt(Object monitor, String label)
      Record a notify() / notifyAll() attempt on monitor.

      Call this before the actual notify*() call. The detector samples Thread.holdsLock(Object) synchronously; if the calling thread does not hold the monitor, the attempt is queued for reporting.

      Parameters:
      monitor - the object the notify is being called on (null-safe)
      label - descriptive name for the report (may be null)
    • analyze

      Report produced by analyze().
      Returns:
      the findings this detector collected during the run