Lia*_*ngs 4 java concurrency synchronization notify wait
我在线程A上有一个对象正在调用,wait()而线程B上的另一个对象做了一些工作,然后调用线程A的对象notify().线程A然后执行一些后处理.
我的问题很简单:
synchronized(this)
{
while(!flag)
{
try
{
wait();
getLogger().info("No longer waiting");
}
catch (InterruptedException ie)
{
getLogger().info("Wait threw InterruptedException");
}
}
}
Run Code Online (Sandbox Code Playgroud)
导致信息消息"不再等待"而不是"等待中断异常".
我很困惑,因为这个(http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait()):
抛出:
InterruptedException - 如果另一个线程在当前线程等待通知之前或当前线程中断当前线程.抛出此异常时,将清除当前线程的中断状态.
为什么我会出现奇怪的行为?
谢谢.
MBy*_*ByD 10
当一个线程等待使用时wait(),他实际上在等待notify().所以一旦notify()被其他线程调用,这个线程将继续,如果你将调用interrupt(),你将获得异常.
另外,从您链接的文件:
导致当前线程等待,直到另一个线程为此对象调用notify()方法或notifyAll()方法
notify 从锁中释放线程.
InterruptedException - 如果另一个线程在当前线程等待通知之前或当前线程中断当前线程.
| 归档时间: |
|
| 查看次数: |
4463 次 |
| 最近记录: |