Firstly, it is almost a duplicate: How to distinguish the output for waiting (long timeout) for notification or timeout?
But this is a new follow-up question.
Availability of this announcement pending:
public final native void wait(long timeout) throws InterruptedException;
It can exit InterruptedException either with a timeout or because the Notify / NotifyAll method was called in another thread, the exception is easy to catch, but ...
My code absolutely needs to know if the timeout has been exited or notified. (In the future, this code needs to be redesigned, but it cannot be done now. Therefore, I need to know the reason for exiting the wait.)
In particular, can someone point out an example of using ThreadLocal Boolean, which is set to true only upon notification (), and where is all this inside an existing loop, as shown below? (This was a more or less accepted answer in another thread, but no specific code example was provided. I am not so familiar with Java, so I need a specific code example - ideally in the context of the existing code below.)
public synchronized int getLastSequenceNumber() { while (empty) { try { wait(waitTimeValue); } catch (InterruptedException e) {} } empty = true; return reportedSequenceNumber; } public synchronized void reconcileLastSequenceNumber(int sequenceNumber) { empty = false; this.reportedSequenceNumber = sequenceNumber; notifyAll(); }
the logical “empty” serves a purpose outside the specific question I ask here. I believe that I will need to add another boolean in order to fulfill the proposed answer from the original question. How would I integrate this proposed solution into the existing code snippet above? Thanks.
source share