How to stop a thread?

前端 未结 4 1771
逝去的感伤
逝去的感伤 2020-11-27 08:15

When a thread is alive, how can I stop the thread? I have given like

if(thread.isAlive()){
    thread.stop();
}

but the method stop is depr

4条回答
  •  情话喂你
    2020-11-27 08:51

    In a non looping thread implmentation, you can always use some thing like this at the very beginning of thread code as:

    void run() {
        if (!shouldContinue) { 
           return 1; 
        } 
        .. 
        then rest of the thread code
        ..
     }
    

提交回复
热议问题