线程停止的三种方式

  • 设置标记位,可以使线程正常退出
  • 使用stop方法强制使线程退出,但是该方法不太安全所以已经被废弃了
  • 使用Thread类中的一个interrupt()可以中断线程

设置标记位

class MyThread implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        int i = 1;
        while(flag){
            try {
                Thread.sleep(1000);
                System.out.println("第"+i+"次执行,线程名称为:"+Thread.currentThread().getName());
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void setFlag(boolean flag){
        this.flag = flag;
    }
}
public class ticket{
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        Thread thread = new Thread(mt,"张一山");
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mt.setFlag(false);
        System.out.println("代码结束");
    }
}

使用stop()方法强制使线程退出

class MyThread implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        int i = 1;
            try {
                Thread.sleep(1000);
                System.out.println("第"+i+"次执行,线程名称为:"+Thread.currentThread().getName());
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }

}
public class ticket{
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        Thread thread = new Thread(mt,"张一山");
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.stop();
        System.out.println("代码结束");
    }
}

使用Thread.interrupt()

  • interrupt()方法只是将线程的状态设置为中断状态而已,它不会中断一个正在运行的线程
  • 此方法只是给线程传递一个中断信号
  • 程序可以根据此信号来判断是否需要终止
  • 当线程使用了wait,sleep,join方法导致主线程阻塞,则interrupt()方法会在线程中抛出InterruptException中断异常,走catch块,并且将线程的中断状态由true变为false
class MyThread implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        int i = 1;
        while(flag){
            try {
                Thread.sleep(1000);
                boolean bool = Thread.currentThread().isInterrupted();
                if(bool){
                    System.out.println("非阻塞情况下执行该操作"+bool);
                    break;
                }
                System.out.println("第"+i+"次执行,线程名称为:"+Thread.currentThread().getName());
                i++;
            } catch (InterruptedException e) {
                System.out.println("退出了");
                boolean bool = Thread.currentThread().isInterrupted();
                System.out.println(bool);
                return ;
            }
        }
    }
    public void setFlag(boolean flag){
        this.flag = flag;
    }
}

public class ticket{
    public static void main(String[] args)throws InterruptedException {
        MyThread mt = new MyThread();
        Thread thread = new Thread(mt,"张一山");
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
        System.out.println("代码结束");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值