How can we stop a thread in Java?

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method. The stop() method is deprecated in Java due to thread-safety issues.


public final void stop()


import static java.lang.Thread.currentThread;
public class ThreadStopTest {
   public static void main(String args[]) throws InterruptedException {
      UserThread userThread = new UserThread();
      Thread thread = new Thread(userThread, "T1");
      System.out.println(currentThread().getName() + " is stopping user thread");
      System.out.println(currentThread().getName() + " is finished now");
class UserThread implements Runnable {
   private volatile boolean exit = false;
   public void run() {
      while(!exit) {
         System.out.println("The user thread is running");
      System.out.println("The user thread is now stopped");
   public void stop() {
      exit = true;


main is stopping user thread
The user thread is running
The user thread is now stopped
main is finished now

Updated on: 15-Sep-2023

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started