使用这个等待声明:
public final native void wait(long timeout) throws InterruptedException;
它可以通过InterruptedException退出,或者通过超时退出,或者因为Notify/NotifyAll方法是在另一个线程中调用的,所以异常很容易捕获,但是...
有没有办法知道退出的原因是超时还是通知?
编辑:
这是一种可以工作的棘手方法(尽管我不喜欢它)
long tBefore=System.currentTimeMillis();
wait(TIMEOUT);
if ((System.currentTimeMillis() - tBefore) > TIMEOUT)
{
//timeout
}
https://stackoverflow.com/questions/3397722
复制相似问题