I have a waiting thread:
synchronized(sharedCounter) {
while(sharedCounter > 0) {
sharedCounter.wait(60000); //wait at most 1 minute
/*if it wakens from the end of the timeout, it should break the loop
or it could potentially restart the timeout*/
}
}
And a thread that can notify:
synchronized (sharedCounter) {
if(sharedCounter == 0)
sharedCounter.notify();
}
How can I distinguish a notify from a timeout?
I could do something like this:
synchronized(sharedCounter) {
while(sharedCounter > 0) {
sharedCounter.wait(60000);
if(sharedCounter == -1) { //if it was a notify()
//I could save the fact that it was a notify() here
break;
}
//Otherwirse, assume it was a timeout, save the fact and break
break;
}
}
synchronized (sharedCounter) {
if(sharedCounter == 0) {
sharedCounter = -1; //to signal that it comes from a notify()
sharedCounter.notify();
}
}
The problem is, that a spurious wake up would spoil my design.
How would you handle this problem?