本文介绍了如果在睡眠线程上调用interrupt()会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个线程,在run()
上我呼叫sleep()
.如果我中断该线程会怎样?
I have a thread, and on run()
I call sleep()
. What will happen if I interrupt this thread?
MyThread extends Thread{
public void run(){
try{
sleep(1000000);
} catch(InterruptedException e) {//}
}
}
我想澄清以下内容:
- 如果线程尚未启动,则调用
interrupt()
不会执行任何操作,对吧? - 如果线程已启动并且现在正在休眠,则在休眠时调用
interrupt()
会抛出InterruptedException
;因此,转到catch()
然后结束线程,对吗?
- If the thread is not yet started, calling
interrupt()
would do nothing, right? - If the thread is started, and is now sleeping, calling
interrupt()
while sleeping will throw anInterruptedException
; and thus, goes tocatch()
and then ends the thread, right?
推荐答案
1)Thread.interrupt API:中断未运行的线程不会产生任何效果.
1) Thread.interrupt API: Interrupting a thread that is not alive need not have any effect.
2)在您的示例中,被中断的线程将进入catch块,然后离开run方法并终止
2) In your example the interrupted thread will enter catch block then leave run method and terminate
这篇关于如果在睡眠线程上调用interrupt()会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!