SoFunction
Updated on 2025-03-04

How to stop a running thread in java

How to stop a running thread

1) Set the flag bit

If the thread's run method executes a repeated execution loop, a flag can be provided to control whether the loop continues

Code example:

public class FunDemo02 {

    /**
      * Exercise 2: Design a thread: it is terminated after running for 10 seconds (master the thread's termination method)
      * @param args
      */
    public static void main(String[] args)  throws Exception{
        MyRunable02 runnable = new MyRunable02();
        new Thread(runnable).start();
        (10000); // The main thread sleeps for 10 seconds         = false;
        ("main、  end ...");
    }
}

class MyRunable02 implements Runnable{

    boolean flag = true;

    @Override
    public void run() {
        while(flag){
            try {
                (1000);
                (new Date());
            } catch (InterruptedException e) {
                ();
            }
        }
        (().getName() + "Execution Completed");
    }
}

2) Use the interrupt flag bit

There is an interrupt flag in the thread, which is false by default. When we call the interrupted method or isInterrupted method, the flag bit will be modified to true.

We can use this to interrupt running threads.

Code example:

package ;

public class FunDemo07 extends Thread{

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new FunDemo07();
        ();
        (3000);
         (); // Interrupt thread Modify the interrupt flag from false to true        // (); // I killed the thread directly        ("main .... ");
    }

    @Override
    public void run() {
        (() + " start...");
        int i = 0 ;
        // () If it is not interrupted, then it is false. If the interrupt method is displayed, it will be modified to true.        while(!()){
            (() + " " + i);
            i++;
        }

        (()+ " end .... ");

    }
}

3) Use InterruptedException

If the thread enters a blocking state because of executing join(), sleep() or wait(), if you want to stop it, you can ask it to call interrupt(), and the program will throw an InterruptedException exception.

We can use this exception to terminate the thread.

package ;

public class FunDemo08 extends Thread{

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new FunDemo08();
        ();
        (3000);
         (); // Interrupt thread Modify the interrupt flag from false to true        // (); // I killed the thread directly        ("main .... ");
    }

    @Override
    public void run() {
        (() + " start...");
        int i = 0 ;
        // () If it is not interrupted, then it is false. If the interrupt method is displayed, it will be modified to true.         while(!()){
        //while(!().isInterrupted()){
             try {
                 (10000);
             } catch (InterruptedException e) {
                 ();
				 break;
             }
             (() + " " + i);
            i++;
        }

        (()+ " end .... ");

    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.