what is the best way to stop a thread in java?
-
04-10-2019 - |
Question
I would like to stop a thread in mid execution. From reading around, I am of the thought I will have to check a local variable to determine if the thread should continue or clean up and exit run(). Any ideas of cleanly implementing this?
Solution
Often a way to do this is create a boolean volatile member variable, perhaps called "stopThread". The thread periodically polls this variable to see if it should terminate.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow