线程停止的三种方式

  • 设置标记位,可以使线程正常退出
  • 使用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("代码结束");
    }
}


猜你喜欢

转载自blog.csdn.net/WZL995/article/details/84201560