线程停止的三种方式

本文介绍了Java线程停止的三种方式:设置标志位、调用过期的stop方法和使用中断。强调了stop方法的不安全性以及中断在阻塞和非阻塞情况下的不同表现。在阻塞情况下,interrupt会抛出InterruptedException并恢复中断状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程停止的三种方式

1. 设置标志位(无法处理线程阻塞时的问题);
2. 调用Thread类提供的stop方法强行关闭线程;
3. 通过Thread类提供的interrupt方法

举例:设置标志位

class InterruptThread extends Thread{
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        int i =0;
        while(flag){
            i++;
            System.out.println(Thread.currentThread().getName()+",i= "+i);
        }
    }
}
public class TestInterrupt {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread t1 = new InterruptThread();
        t1.start();
        //子线程和主线程并发执行,为了看到效果,休眠1s
        Thread.sleep(1000);
        t1.setFlag(false);
    }
}

举例:使用stop

class InterruptThread extends Thread{
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        int i =0;
        while(flag){
            i++;
            System.out.println(Thread.currentThread().getName()+",i= "+i);
        }
    }
}
public class TestInterrupt {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread t1 = new InterruptThread();
        t1.start();
        //子线程和主线程并发执行,为了看到效果,休眠1s
        Thread.sleep(1000);
        t1.stop();
    }
}

使用stop方法不推荐使用:(1)该方法已经过期;(2)会产生不完整的数据

举例:使用中断(非阻塞情况下)

class InterruptThread extends Thread{
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        int i =0;
        while(flag){
            //查看当前中断状态
            boolean bool = Thread.currentThread().isInterrupted();
            System.out.println(bool);
            i++;
            System.out.println(Thread.currentThread().getName()+",i= "+i);
        }
    }
}
public class TestInterrupt {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread t1 = new InterruptThread();
        t1.start();
        //子线程和主线程并发执行,为了看到效果,休眠1s
        Thread.sleep(1000);
        t1.interrupt();
    }
}

结果表示:interrupt并不能中断线程,只是将原来的中断标志由false改为true。这是在非阻塞的情况下。

举例:使用中断(阻塞情况下)

class InterruptThread extends Thread {
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            try {
                Thread.sleep(200);
                boolean bool = Thread.currentThread().isInterrupted();
                System.out.println("当前线程状态为:" + bool);
                System.out.println(Thread.currentThread().getName()
                        + ",i = " + i++);
            } catch (InterruptedException e) {
                System.out.println("抛出中断异常");
                boolean bool = Thread.currentThread().isInterrupted();
                System.out.println(bool);  //false
                return;
            }
        }
        System.out.println("线程停止");
    }
}
public class TestInterrupt {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread t1 = new InterruptThread();
        t1.start();
        //子线程和主线程并发执行,为了看到效果,休眠1s
        Thread.sleep(1000);
        t1.interrupt();
    }
}

结果表示:在阻塞情况下(sleep),直接会抛出中断异常,并且把将中断标志true又改回为false

总结第三种
1)若线程中为非阻塞(没有使用类似sleep/wait/join)时,调用线程对象的interrupt方法不会真正中断线程,只是简单的将线程的状态由false---->true(中断状态),我们可以根据此状态来进一步确定如何处理线程。

2)若线程中调用了阻塞线程的方法,如:sleep/wait/join,此时在调用线程的interrupt方法时,会抛出InterruptedException。同时将线程状态又还原为false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值