[英]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:所以在你的情况下:
InterruptedException
is not a failure.InterruptedException
不是失败。notify()
or notifyAll()
and the exception is never thrownnotify()
或notifyAll()
并且永远不会抛出异常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.