Java – how do I check if a thread terminates?

When does the thread reach the termination state? When the end of the run () method is reached, will it be terminated?

So what is the correct way to check whether a thread is terminated? Because the following situations always seem to me

if(!(thread.getState()).equals("TERMINATED")){}

Any ideas?

Solution

First: thread Getstate () returns a thread State, it will not be equal to a string, so you need to write such code:

if(thread.getState()!=Thread.State.TERMINATED){ }

Yes, when the run () method ends (usually because it throws an exception), a thread will go to the terminated state

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>