Java停止线程的三种方式

Java停止线程的三种方式

停止线程的核心:个人认为是让线程结束执行run方法,即线程退出

围绕该核心,可以使用以下几种方式停止线程

  1. 通过标记位停止
  2. (推荐) 通过interrupted()给线程设置清除标记方式停止
  3. 通过stop方法停止,jdk过时方法,不推荐使用,有线程安全问题
通过标记位停止

代码示例:

package com.doudou;

public class TestStopThread extends Thread {
          
   

    // 标记位
    private static boolean stop = false;

    private String name;

    public TestStopThread(String name) {
          
   
        this.name = name;
    }

    @Override
    public void run() {
          
   
        // 通过指定标记位,让线程停止运行run方法,即线程停止运行
        while (!stop) {
          
   
            try {
          
   
                // 让线程休眠1s
                Thread.sleep(1000);
            } catch (InterruptedException e) {
          
   
                e.printStackTrace();
            }
            System.out.println(name + "运行中");
        }
    }

    public static void main(String[] args) throws InterruptedException {
          
   
        TestStopThread testStopThreadA = new TestStopThread("A");
        TestStopThread testStopThreadB = new TestStopThread("B");
        TestStopThread testStopThreadC = new TestStopThread("C");
        TestStopThread testStopThreadD = new TestStopThread("D");
        testStopThreadA.start();
        testStopThreadB.start();
        testStopThreadC.start();
        testStopThreadD.start();
        Thread.sleep(5000);
        // 使其退出while循环,即退出run方法
        stop = true;
    }
}

运行结果:

线程执行5s后,线程停止运行,程序结束

通过interrupted()给线程设置清除标记方式停止

其思想跟设置标记位差不多,不过该标记位设置在线程上,若需停止线程,需要判断线程是否设置清除标记

代码示例:

package com.doudou;

public class TestInterruptedThread implements Runnable {
          
   

    @Override
    public void run() {
          
   
        // 线程没有设置清除标记,让其执行逻辑
        while (!Thread.currentThread().isInterrupted()) {
          
   
            System.out.println("运行中");
        }
    }

    public static void main(String[] args) throws InterruptedException {
          
   
        TestInterruptedThread testInterruptedThread = new TestInterruptedThread();
        Thread thread = new Thread(testInterruptedThread);
        thread.start();
        Thread.sleep(5000);
        // 给线程设置清除标记
        thread.interrupt();
    }
}

执行结果:

线程执行5s后,线程停止运行,线程结束

注意:

此种方式无法停止阻塞中的线程,比如线程正在执行sleep、wait方法等,会抛出java.lang.InterruptedException: sleep interrupted异常。

代码示例:

package com.doudou;

public class TestInterruptedThread implements Runnable {
          
   

    @Override
    public void run() {
          
   
        // 线程没有设置清除标记,让其执行逻辑
        while (!Thread.currentThread().isInterrupted()) {
          
   
            try {
          
   
                Thread.sleep(1000);
            } catch (InterruptedException e) {
          
   
                e.printStackTrace();
            }
            System.out.println("运行中");
        }
    }

    public static void main(String[] args) throws InterruptedException {
          
   
        TestInterruptedThread testInterruptedThread = new TestInterruptedThread();
        Thread thread = new Thread(testInterruptedThread);
        thread.start();
        Thread.sleep(5000);
        // 给线程设置清除标记
        thread.interrupt();
    }
}

执行结果:

程序抛出java.lang.InterruptedException: sleep interrupted异常,且线程继续执行,没有终止

经验分享 程序员 微信小程序 职场和发展