WAIT method question
I'm using 'public final void wait(long timeout, int nanos)' on a monitor.
Reading the javadoc I've found the following sentences:
* Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
* The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.
How can I differentiate between the two cases?
If the timeout period has elapsed I want to throw an exception and in the other case I want to continue to the following steps.
You may either:
-measure the time spent waiting, and if it's big enough, throw an exception. This is simple to implement but you could get some false positives. If your request is meant to be a debugging facility, it is likely just fine, if you keep in mind the thread might in fact have been notified.
-instead of timed wait, use an infinite wait + a separate watchdog thread. If the watchdog interrupts your thread, you get an interruptedException. If the watchdog interrups the other thread after returning from wait, the thread gets its interrupt flag set - you can check the flag after resetting the watchdog.
-create a new watchdog thread every time you wait. This causes considerable performance penalty but it simplifies the implementation a bit.
There might other option I haven't thought of, please share your ideas.