How do you kill a Thread in Java?

前端 未结 16 2044
天命终不由人
天命终不由人 2020-11-21 05:54

How do you kill a java.lang.Thread in Java?

16条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 06:35

    I'd vote for Thread.stop().

    As for instance you have a long lasting operation (like a network request). Supposedly you are waiting for a response, but it can take time and the user navigated to other UI. This waiting thread is now a) useless b) potential problem because when he will get result, it's completely useless and he will trigger callbacks that can lead to number of errors.

    All of that and he can do response processing that could be CPU intense. And you, as a developer, cannot even stop it, because you can't throw if (Thread.currentThread().isInterrupted()) lines in all code.

    So the inability to forcefully stop a thread it weird.

提交回复
热议问题