Simple answer: You can stop a thread INTERNALLY in one of two common ways:
You can also stop threads EXTERNALLY:
system.exit
(this kills your entire process)interrupt()
method *kill()
or stop()
)*: The expectation is that this is supposed to stop a thread. However, what the thread actually does when this happens is entirely up to what the developer wrote when they created the thread implementation.
A common pattern you see with run method implementations is a while(boolean){}
, where the boolean is typically something named isRunning
, it's a member variable of its thread class, it's volatile, and typically accessible by other threads by a setter method of sorts, e.g. kill() { isRunnable=false; }
. These subroutines are nice because they allow the thread to release any resources it holds before terminating.