简体   繁体   English

Java wait()不会抛出InterruptedException

[英]Java wait() not Throwing InterruptedException

I have an object on thread A that is calling wait() while another object on thread B does some work then calls thread A 's object's notify() . 我在线程A上有一个调用wait()对象,而线程B上的另一个对象执行某些操作然后调用线程A的对象的notify() Thread A then performs some post-processing. 线程A然后执行一些后处理。

My problem is pretty straightforward: 我的问题很简单:

synchronized(this)
{
    while(!flag)
    {
        try
        {
            wait();
            getLogger().info("No longer waiting");
        }
        catch (InterruptedException ie)
        {
            getLogger().info("Wait threw InterruptedException");
        }
    }
}

Results in an info message of "No longer waiting" instead of "Wait threw InterruptedException". 导致信息消息“不再等待”而不是“等待中断异常”。

I am confused, because of this ( http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait() ): 我很困惑,因为这个( http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait() ):

Throws: 抛出:

InterruptedException - if another thread interrupted the current thread before or while the current thread was waiting for a notification. InterruptedException - 如果另一个线程在当前线程等待通知之前或当前线程中断当前线程。 The interrupted status of the current thread is cleared when this exception is thrown. 抛出此异常时,将清除当前线程的中断状态。

Why am I getting odd behavior? 为什么我会出现奇怪的行为?

Thanks. 谢谢。

When a thread waits using wait() , he actually waits for notify() . 当线程等待使用wait() ,他实际上等待notify() So once notify() has been called by the other thread, this thread will continue, if you will call interrupt() , you would get the exception. 所以一旦另一个线程调用了notify() ,这个线程就会继续,如果你调用interrupt() ,你就会得到异常。

Also, from the documents you linked to: 另外,从您链接的文件:

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object 导致当前线程等待,直到另一个线程为此对象调用notify()方法或notifyAll()方法

notify releases the thread from the lock. notify从锁中释放线程。

InterruptedException - if another thread interrupted the current thread before or while the current thread was waiting for a notification. InterruptedException - 如果另一个线程在当前线程等待通知之前或当前线程中断当前线程。

notify() does not make wait() throw InterruptedException . notify()不会使wait()抛出InterruptedException notify() let wait() continue the normal program flow. notify()wait()继续正常的程序流程。

notify() is not an abnormal termintation for a thread that is wait() -ing. notify()不是wait() ing线程的异常终止。 You'd get the exception if, for instance, the thread was terminated before notify() was called - not as a result of notify() being called. 例如,如果线程在调用notify()之前终止,则会得到异常 - 而不是因为调用了notify() The thread hasn't been interrupted - it's been awoken. 线程没有被中断 - 它被唤醒了。

Note that you have a bug in that code. 请注意,您的代码中存在错误。 Wait should always be invoked in a loop, and check a condition after waking up. 应始终在循环中调用Wait,并在唤醒后检查条件。

wait can be awaken by a spurious wakeup. 等待可以被虚假的唤醒唤醒。 See the javadoc wait() 查看javadoc wait()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM