恢复中断的线程 [英] Resume interrupted thread
问题描述
我想恢复被中断线程的工作,请让我知道一些解决该问题的方法.
I want to resume the work of interrupted thread,please let me know some possible solutions for the same.
class RunnableDemo implements Runnable
{
public void run()
{
while(thread.isInterrupted())
{
try{}
catch(Exception e){ //exception caught}
}
}
}
如果捕获到异常,线程将被中断,但是即使捕获到异常,我也希望线程继续其工作,所以请向我建议一些克服此问题的方法.
If exception is caught, thread is interrupted, but even though exception is caught, i want thread to continue its work, so please suggest me some way to overcome this issue.
预先感谢
推荐答案
在编写线程时,您选择要遵循线程中断的要求.因此,如果您不希望线程被中断,请不要检查中断状态并继续执行.
Thread interruption is something you choose to obey when writing a thread. So if you don't want your thread to be interrupted, don't check the interrupted status and continue regardless.
唯一需要try/catch语句(关于线程中断)的时间是在调用引发InterruptedException
的阻塞方法时.然后,您需要避免让该异常停止线程的工作.
The only time you'll need try/catch statements (with respect to thread interruption) is when calling blocking methods that throw InterruptedException
. Then you'll need to avoid letting that exception stop your thread's work.
当然...您应该考虑一下这是否是合适的行为方式.线程中断是一件有用的事情,选择不遵守它可能会使您的代码用户烦恼.
Of course... you should give some thought about whether this is a suitable way to behave. Thread interruption is a helpful thing and choosing not to adhere to it can be annoying to users of your code.
这篇关于恢复中断的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!