这是线程正在等待notify()或超时的情况.这里添加了一个while循环来处理虚假唤醒.
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();
}
}
在这种情况下,我如何区分虚假唤醒和超时?如果它是一个虚假的醒来,我需要回去等待.如果是超时,我需要退出循环.
我可以很容易地识别notify()的情况,因为我将在notify()调用时将dosleep变量设置为false.
编辑:由于嵌入式项目的要求,我正在使用1.4 java版本.我不能使用条件,因为它只在1.5后可用.
提前致谢.