How to check if thread is terminated?

后端 未结 2 1245
感动是毒
感动是毒 2021-02-06 01:47

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 wa

相关标签:
2条回答
  • 2021-02-06 01:56

    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.

    0 讨论(0)
  • 2021-02-06 02:09

    test the method thread.isAlive()

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

    0 讨论(0)
提交回复
热议问题