Java – Kills a thread in Java Android

Kills a thread in Java Android… here is a solution to the problem.

Kills a thread in Java Android

I

have an application where I need to call 3 methods in 3 separate threads and then terminate them. According to Javadoc, I noticed that threads stop() and even destroy() have been deprecated. It’s like I start one thread after the other and then kill similar threads one by one. Is there a special way to kill threads because I can’t use the deprecated method

Any help is greatly appreciated.
Thanks again

Solution

You don’t kill threads. You call Thread.interrupt() and pair within the interrupted thread The interrupted state or InterruptedException uses react. Alternatively, you use the volatile flag. See also the official documentation Learn more about background and more.

Better yet, as suggested in the comments, use the thread pool/executor instead of the original thread.

Related Problems and Solutions