Class NotifyWithoutMonitorDetector
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
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyze()Report produced byanalyze().voidrecordNotifyAttempt(Object monitor, String label) Record anotify()/notifyAll()attempt onmonitor.
-
Constructor Details
-
NotifyWithoutMonitorDetector
public NotifyWithoutMonitorDetector()
-
-
Method Details
-
recordNotifyAttempt
Record anotify()/notifyAll()attempt onmonitor.Call this before the actual
notify*()call. The detector samplesThread.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 benull)
-
analyze
Report produced byanalyze().- Returns:
- the findings this detector collected during the run
-