How do you kill a Thread in Java?

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

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

16条回答
  •  悲&欢浪女
    2020-11-21 06:32

    Generally you don't kill, stop, or interrupt a thread (or check wheter it is interrupted()), but let it terminate naturally.

    It is simple. You can use any loop together with (volatile) boolean variable inside run() method to control thread's activity. You can also return from active thread to the main thread to stop it.

    This way you gracefully kill a thread :) .

提交回复
热议问题