在《Java核心技术 卷II》第7章多线程里有这样一段代码:public void run()
{
try
{
...
while(more work to do)
{
do more work
}
}
catch(InterruptedException exception)
{
//thread was interrupted during sleep or wait
}
finally
{
cleanup ,if required
}
//exit run method and terminate thread
}书中说:“这个代码框架存在一个问题,如果在线程处于睡眠或者等待状态时调用interrupt方法,那么将不会产生任何InterruptedException事件”。
-----------------------------------------------------------------------------------------
上面这句话该怎么理解,interrupt方法的作用不就是:如果一个线程被调用的sleep或wait所中断,调用interrupt将产生一个InterruptException事件吗?脑袋晕了,谁给说明说明啊
{
try
{
...
while(more work to do)
{
do more work
}
}
catch(InterruptedException exception)
{
//thread was interrupted during sleep or wait
}
finally
{
cleanup ,if required
}
//exit run method and terminate thread
}书中说:“这个代码框架存在一个问题,如果在线程处于睡眠或者等待状态时调用interrupt方法,那么将不会产生任何InterruptedException事件”。
-----------------------------------------------------------------------------------------
上面这句话该怎么理解,interrupt方法的作用不就是:如果一个线程被调用的sleep或wait所中断,调用interrupt将产生一个InterruptException事件吗?脑袋晕了,谁给说明说明啊
解决方案 »
- JAVA中使用JS函数
- 将float num=123.0001f,保留两位小数,格式化后返回float类型
- 北京一家公司的笔试题,希望大家给个思路
- [ResultSet问题] 请问这种情况可能是什么原因?
- 请问java.applet.Applet与javax.swing.JApplet有什么区别?
- java socket 编程端口问题
- 请帮我解释一下
- 小弟新手上路,请大虾指教用什么工具比较好?
- 如何unload当前被系统调用的Com控件的Dll?
- 函数传递Integer参数不是传递引用吗?
- response.setHeader("Refresh","0"+url)不能redirect另一个网页?
- 本人的新发现,与人共享.
{
//thread was interrupted during sleep or wait
}
因为你没有处理这个异常,调用者当然不知道这个异常的存在了
while(!interrupted() && more work to do)
{
do more work
}
需要调用interrupted方法来确定它最近是否被中断了