简体   繁体   English

wait() 总是抛出 InterruptedException

[英]wait() is always throwing InterruptedException

The below wait() call is always throwing InterruptedException .下面的wait()调用总是抛出InterruptedException It is not that some other thread is interrupting it.并不是其他线程正在中断它。 There is no delay between the call and the exception being thrown.在调用和抛出异常之间没有延迟。 I have put logs to check the time lapse between the call and the catch of exception.我已经放置了日志来检查调用和捕获异常之间的时间间隔。

Also, Thread.sleep(long) yields the same result.此外, Thread.sleep(long)产生相同的结果。

public synchronized void retryConnection()
{
// . .. some code
    try
    {
        wait();
    }
    catch(InterruptedException e)
    {
        log(Level.SEVERE, "InterruptedException " , e);
    }
// . .. some code
}

I have tried these random things:我试过这些随机的东西:

call this method in a new thread.在新线程中调用此方法。 Then it works fine.然后它工作正常。 It is a waiting and notification mechanism also works.它是一种等待和通知机制也有效。

put the same code again, that is, wait again after the exception is caught.再次放入相同的代码,即捕获异常后再次等待。 Then it is properly waiting.然后它正在等待。

Observation: When the call is coming from a netty(server) thread, it fails but if it is coming from some other java thread, it works.观察:当调用来自 netty(server) 线程时,它会失败,但如果它来自某个其他 java 线程,则它可以工作。 So my question is: Is there any mechanism or thread state where wait() or Thread.sleep() is forbidden and throws an exception if they are invoked?所以我的问题是:是否有任何机制或线程状态禁止wait()Thread.sleep()并在调用它们时抛出异常?

I have checked the interrupted flag and it is 'false' always.我检查了中断标志,它总是“假”。

InterruptedException is a perfectly normal event to occur when using Object.wait() , and thread.sleep() coupled with Object.notify() , and Object.notifyAll() . InterruptedException是在使用Object.wait()thread.sleep()Object.notify()Object.notifyAll()时发生的完全正常的事件。

This is called Inter-thread Communication .这称为线程间通信

A thread which owns the object's monitor, can call Object.wait() - a call which causes this current thread to lose the ownership of the monitor and to wait / block until another thread owning said object's monitor will call Object.notify() or Object.notifyAll() .拥有对象监视器的线程可以调用Object.wait() - 该调用会导致当前线程失去监视器的所有权并等待/阻塞,直到拥有所述对象监视器的另一个线程调用Object.notify()Object.notifyAll()

This means that active thread owning the lock awakes other thread(s) from waiting (or sleeping, or blocking) state.这意味着拥有锁的活动线程将其他线程从等待(或睡眠或阻塞)状态唤醒。 This originating thread has to also relinquish its grasp on objects lock, before the thread(s) that got notified can proceed.在收到通知的线程可以继续之前,这个发起线程还必须放弃对对象锁的控制。

The thread which is waiting on a lock receives the notification as InterruptedException .等待锁的线程接收通知为InterruptedException It then can proceed as by the time it has received the exception, it has been given the ownership of the lock.然后它可以继续进行,因为在它收到异常时,它已经获得了锁的所有权。

Please see Java Thread lifecycle diagram to for more information . 有关详细信息,请参阅Java 线程生命周期图

So in your case:所以在你的情况下:

  • Receiving InterruptedException is not a failure.接收InterruptedException不是失败。
  • Netty trying to be performant, does not let threads wait too long, hence you keep getting the notifications. Netty 试图提高性能,不会让线程等待太久,因此您不断收到通知。
  • When you run your threads, you don't call notify() or notifyAll() and the exception is never thrown当您运行线程时,您不会调用notify()notifyAll()并且永远不会抛出异常
  • For both Object.wait() and Thread.sleep() you get the following (from javaDoc):对于Object.wait()Thread.sleep()你得到以下(来自 javaDoc):

    The interrupted status of the current thread is cleared when this exception is thrown.抛出此异常时清除当前线程的中断状态。

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

相关问题 Java wait()不会抛出InterruptedException - Java wait() not Throwing InterruptedException 谁在抛出InterruptedException? - Who is throwing the InterruptedException? FutureTask中的awaitDone抛出InterruptedException - awaitDone in FutureTask throwing InterruptedException 如果线程在调用Object.wait()之前被中断,该线程甚至会在抛出InterruptedException之前费心地释放锁。 - If a thread is interrupted before calling Object.wait(), will the thread even bother to release the lock before throwing InterruptedException ThreadPoolExecutor.shutdownNow() 不在线程中抛出 InterruptedException - ThreadPoolExecutor.shutdownNow() not throwing InterruptedException in Thread 从主线程休眠是抛出InterruptedException - sleep from main thread is throwing InterruptedException 条件的类的await方法不抛出InterruptedException - await method of class Condition not throwing InterruptedException 调用interrupt()时,线程未引发InterruptedException - Thread not throwing an InterruptedException when interrupt() is called 以某种方式等待并抛出InterruptedException的Java方法 - Java methods that get to wait somehow and that throw InterruptedException Future.get()总是被InterruptedException中断 - Future.get() gets interrupted always with an InterruptedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM