How to properly stop the Thread in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Stopping a thread in Java is a crucial aspect of multithreaded programming, ensuring that an application can terminate safely and promptly without risking resource leaks or corrupting shared data. Effective thread management significantly contributes to the stability and performance of Java applications.
Java provides several methods to manage the life cycle of threads, but not all are recommended or safe. It's important to understand the dos and don'ts of stopping threads in Java.
Deprecated Methods
Historically, Java included methods like Thread.stop(), Thread.suspend(), and Thread.resume() to control thread execution. However, these methods have been deprecated due to their unsafe nature, as they can leave shared data in inconsistent states and cause deadlock conditions. Therefore, using these methods is strongly discouraged.
Recommended Approach
The recommended way to stop a thread in Java is to ensure that the thread exits its run method naturally. The thread should regularly check a condition and return from the run method if the condition indicates that it should stop. The steps to implement this are as follows:
- Define a Volatile Boolean Flag: Use a
volatile booleanflag in your thread class. Thevolatilekeyword ensures that changes to the variable are visible to all threads. - Check the Flag Periodically in the
run()Method: Within the run method, periodically check the value of this flag. If the flag is set to true, use a return statement to exit the method cleanly. - Provide a Method to Set the Flag: Expose a public method in your thread class that external code can call to set the flag to true.
Below is an example of how to implement these steps:
Handling Interruptions
Java threads can also be stopped by interruption. This requires checking the thread's interrupted status by calling Thread.interrupted() or handling InterruptedException.
Here is how you can use interruption:
- Call
Thread.interrupt(): Trigger an interruption from another thread. - Handle
InterruptedException: Catch and handleInterruptedExceptionwithin the thread's run method. - Check Interruption Status: Optionally check the thread's interrupted status periodically within the run method.
Cancellation via Executors
If using the Executor Framework, you can manage thread termination more robustly using a Future<?> object returned by the executor:
Key Points Summary
| Method | Use Case | Safe to Use | Notes |
Thread.stop() | Deprecated, unsafe | No | Can cause corrupted data |
| Volatile Flag | Recommended for manual checks within run() | Yes | Flexible and safe |
| Interruptions | External control for stopping threads | Yes | Requires catching interruptions in thread logic |
| ExecutorService | Managing threads in a pool | Yes | Provides Future for task control |
Conclusion
Proper thread termination in Java requires careful consideration to avoid unnecessary issues and ensure resource release. Avoid deprecated methods and use flags, interruptions, or executor services as per your requirement. Code responsible thread termination patterns to allow Java applications to maintain robustness and responsiveness.

