如何检查线程是否终止? [英] How to check if thread is terminated?
本文介绍了如何检查线程是否终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
线程何时达到已终止状态?当达到 run()
方法结束时它会被终止吗?
when does a thread reach the terminated status? Does it get terminated when the end of the run()
method is reached?
那么正确的方法是什么?检查线程是否被终止?因为以下情况似乎总是适合我
So what is the right way to check if a thread is terminated? Because following condition seems to be true always for me
if(!(thread.getState()).equals("TERMINATED")){}
任何想法?
推荐答案
首先: Thread.getState()
返回 Thread.State
,它永远不会等于字符串
,所以你需要编写这样的代码:
First: Thread.getState()
returns a Thread.State
, which will never be equal to a String
, so you'd need to write that code like this:
if(thread.getState()!=Thread.State.TERMINATED){ }
是的:当 run()
方法结束(正常或因为它抛出异常),然后 Thread
将转到 TERMINATED
州。
And yes: when the run()
method ends (either normally or because it throws an exception), then a Thread
will go to the TERMINATED
state.
这篇关于如何检查线程是否终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文