In fact, you can find out, I just discovered from my learned boss:
The wait() and notify() methods on java.lang.Object require that you hold the lock on the object. If you do not hold the lock, these methods will throw IllegalMonitorStateException. This gives a way to determine whether you hold the lock.
The usual
pattern for using wait() or notify() is to explicitly synchronise on the object before calling. But, if you call wait(
a small time) or notify() on an object, without explicitly synchronising, you will find out whether you held the lock on the object. If you did, the wait() or notify() will work. If you did not, IllegalMonitorStateException gets thrown.
A little care needs to be taken, because neither wait() nor notify() are without side effects. Use of the above mechanism could cause problems if used on an object which is already involved in some type of wait()/notify() notification scheme. But, where that is not the case, it sounds to me like a very useful technique. I propose to use it for an assertion mechanism, creating a debug-build method that I can call that says "I should have the lock on such-and-such-object, please check that I do".