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异常,且线程继续执行,没有终止

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值