How to stop a thread that is running forever without any use

后端 未结 6 2119
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 14:58

In the below code, i have a while(true) loop. considering a situation where there is some code in the try block where the thread is supposed to perform some tasks which take

6条回答
  •  不知归路
    2020-12-07 15:34

    Create a field boolean keepGoing that you set to true before starting your thread and replace while (true) with while (keepGoing). At some point, you decide where, simply change the value of keepGoing to false and it will exit the loop.

提交回复
热议问题