Here is a case where a thread is waiting for a notification () or timeout. This adds a while loop to handle false wakeups.
boolean dosleep = true; while (dosleep){ try { wait(2000); /** * Write some code here so that * if it is spurious wakeup, go back and sleep. * or if it is timeout, get out of the loop. */ } catch (InterruptedException e) { e.printStackTrace(); } }
In this case, how can I distinguish between false awakening and timeout? If this is a false awakening, I need to go back and wait. And if it's a timeout, I need to exit the loop.
I can easily determine the case of notify (), because I will set the dosleep variable to false when notify () is called.
EDIT: I am using version 1.4 of java because of the built-in project requirement. I cannot use Condition since it is only available after 1.5.
Thanks in advance.
source share