等待通知无限循环
Wait notify infinite loop
我在 java 中有以下无限循环:
while (!Thread.interrupted())
{
synchronized (this)
{
try
{
wait();
}
catch (InterruptedException e)
{
logger.error(e);
Thread.currentThread().interrupt();
}
}
}
这是应用程序 runForever()
方法的主体。该应用程序附加了一个关闭挂钩,以便在它获得 control-c-ed
时执行名为 stop()
的方法
问题是,为什么这样做有效?线程一直在等待,谁通知这个线程停止等待?
根据 java.lang.Object...
的 Javadoc
Causes the current thread to wait until another thread invokes the
notify()
method or the notifyAll()
method for this object. In other
words, this method behaves exactly as if it simply performs the call
wait(0)
. The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up either
through a call to the notify method or the notifyAll method. The
thread then waits until it can re-obtain ownership of the monitor and
resumes execution.
... 因此线程将根据您描述的单元 notify()
或 notifyAll()
被调用的行为无限期等待。至于你问题的第二部分,它也在JavaDoc中:
Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
- Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
- Some other thread invokes the notifyAll method for this object.
- Some other thread interrupts thread T.
- The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
这也解释了为什么 wait()
无限期等待。这样做的原因是因为调用 wait()
与调用 wait(0)
相同,在这种情况下,根据文档,“ 线程只是等待通知 ”。
我在 java 中有以下无限循环:
while (!Thread.interrupted())
{
synchronized (this)
{
try
{
wait();
}
catch (InterruptedException e)
{
logger.error(e);
Thread.currentThread().interrupt();
}
}
}
这是应用程序 runForever()
方法的主体。该应用程序附加了一个关闭挂钩,以便在它获得 control-c-ed
stop()
的方法
问题是,为什么这样做有效?线程一直在等待,谁通知这个线程停止等待?
根据 java.lang.Object...
的 JavadocCauses the current thread to wait until another thread invokes the
notify()
method or thenotifyAll()
method for this object. In other words, this method behaves exactly as if it simply performs the callwait(0)
. The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
... 因此线程将根据您描述的单元 notify()
或 notifyAll()
被调用的行为无限期等待。至于你问题的第二部分,它也在JavaDoc中:
Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
- Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
- Some other thread invokes the notifyAll method for this object.
- Some other thread interrupts thread T.
- The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
这也解释了为什么 wait()
无限期等待。这样做的原因是因为调用 wait()
与调用 wait(0)
相同,在这种情况下,根据文档,“ 线程只是等待通知 ”。