VOOZH about

URL: https://www.geeksforgeeks.org/java/killing-threads-in-java/

⇱ Killing threads in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Killing threads in Java

Last Updated : 8 Jun, 2026

A Thread in Java stops automatically after run() completes, but sometimes we need to stop it manually. Deprecated methods like stop(), suspend(), and resume() are unsafe, so modern approaches provide safer ways to control thread termination.

  • Proper thread termination helps avoid memory leaks and deadlocks
  • Modern methods improve stability and reliability of applications 

Modern ways to stop thread

1. Using Boolean Flag

A boolean flag can be used to control thread execution, but when the flag is shared between multiple threads it should be declared volatile (or protected using synchronization). Otherwise, visibility issues may occur and the thread may not see updates made by another thread.

  • Simple and easy way to control thread lifecycle
  • Avoids unsafe thread termination
  • Suitable when thread is doing continuous work in loop

Output
New thread: Thread[First thread, 5, main]
New thread: Thread[Second thread, 5, main]
First thread: 0
Second thread: 0
First thread: 1
Second thread: 1
First thread: 2
Second thread: 2
First thread: 3
Second thread: 3
First thread: 4
Second thread: 4
First thread: 5
Second thread Stopped.
First thread Stopped.
Exiting the main Thread

Explanation: Output can be variable everytime. By using a flag we can stop a thread whenever we want to and we can prevent unwanted run-time errors.

2. Using Volatile Boolean Flag

A volatile boolean flag is used when multiple threads access the same variable. Without volatile, threads may cache the variable value locally, causing visibility issues. Using volatile ensures that changes made by one thread are immediately visible to others.

  • Ensures visibility of changes across threads
  • Prevents infinite loops caused by cached values
  • Makes multi-threaded code more reliable.

Runtime Errors:

Time Limit Exceeded

Output:

started main thread.. started inside thread.. exiting main thread..

Explanation : The inside thread does not see the updated value of exit due to a visibility problem, so it keeps looping infinitely while the main thread exits.


The code runs into an infinite loop because the inside thread cannot see updates made to the exit variable. This happens due to local caching. Using volatile ensures the updated value is visible to all threads and prevents this issue.


Output
started main thread..
started inside thread..
exiting main thread..
exiting inside thread..

Explanation: The volatile keyword ensures that changes to exit made by the main thread are visible to the inside thread, allowing it to exit the loop and terminate properly.

Note: Using volatile prevents infinite loops by ensuring changes are visible to all threads, making the code thread-safe. 

Note: Any variable shared between threads and modified by one thread should be declared volatile or accessed through synchronized blocks to guarantee visibility. A plain boolean flag may not work reliably in all JVM implementations.

3. Using Thread.interrupt()

The interrupt() method is used to signal a thread that it should stop its execution. It sets an internal interrupt flag, which the thread can check and handle appropriately. It is consider the best way for stopping the thread.

  • Sets interrupt flag instead of forcing stop
  • Works well with blocking methods like sleep()

Output
New thread: Thread[Thread-0, 5, main]
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread has stopped.
Exiting the main Thread

Explanation :The thread runs in a loop printing "Thread is running" until it is interrupted. When interrupt() is called from the main method, the loop stops, the thread ends, and the main thread prints "Exiting the main Thread".

Note: The output may vary every time. 

Comment