开发者

How to check if thread is terminated?

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")){}

Any ideas?


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){ }

And yes: when the run() method ends (either normally or because it throws an exception), then a Thread will go to the TERMINATED state.


test the method thread.isAlive()

... or optionally, join until it finishes with thread.join()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜