Bits of Knowledge: Properly Terminating a Java Thread
Introduction
When working with multi-threaded Java application, it is generally a good practice to ensure that the threads you created will be properly terminated. Not doing so might clog your system resources because not all threads are automatically terminated when the application is closed.
This is not a problem with threads that has a single task to perform. These threads can just do their job and properly cleanup after themselves.
But how about threads that are supposed to run indefinitely? How do we tell it to stop running, and it's time to go?
Remember that Thread.stop()
method is not an option. This method is deprecated since Java 1.2 because of being unsafe. The .stop()
method will force terminate a thread whatever it is doing at the time the method is called – whether it is safe or not.
Thread Interrupted Status
A good way to stop a thread without using Thread.stop()
is to interrupt it. This is done by calling the Thread.interrupt()
method.
Unlike stopping the thread, interrupt will only flag the thread as interrupted. The method run by the thread will have to handle the .interrupted()
flag to properly respond to the interruption. This way, the thread can do whatever it needs to do to ensure that it will terminate cleanly.
After running the uploadWatcherThread.interrupt()
from the example above, the loop will properly end due to the condition !Thread.interrupted()
not satisfied anymore. Now that the loop has ended, all the code needed to run has been completed, too. This will properly terminate the thread.
We need to keep in mind that calling the method .interrupted()
will clear the interrupted status of that thread. Meaning, when you call the method .interrupted()
twice in the same thread consecutively, the 2nd call will return false
.