线程的终止操作

1.使用interrupt()/isInterrupted()/interrupted()

interrupt() 在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号-->线程中断标志已设置

isInterrupted() 判断当前线程的中断状态(true 终止)

interrupted() 线程的静态方法,用来恢复中断状态(注:作用与当前线程中)

interrupt:使用案例

public class InterruptDemo {

    public static void main(String[] args) throws InterruptedException {

      Thread t = new Thread(()->{
            int i = 0;
            while(!Thread.currentThread().isInterrupted()){ //判断是否有中断标记
                System.out.printf("输出类容%s 线程标记%s \n" ,i++,String.valueOf(Thread.currentThread().isInterrupted()));
            }
        });

      t.start();
      Thread.sleep(100);
      //interrupt 线程中断 指的是t这个线程
      t.interrupt();
    }

}

interrupted()使用案例:

public class InterruptDemo {

    public static void main(String[] args) throws InterruptedException {

      Thread t = new Thread(()->{
            int i = 0;
            while(!Thread.currentThread().isInterrupted()){
                System.out.printf("输出类容%s 线程标记%s \n" ,i++,String.valueOf(Thread.currentThread().isInterrupted())); //isInterrupted 不具有复位操作
            }
        });

      t.start();
      Thread.sleep(100);
      //interrupt 线程标记
      t.interrupt();

      //设置当前线程 中断标记
      Thread.currentThread().interrupt();

      System.out.println("mian 前 线程的中断标记:" + Thread.interrupted()); //true


      System.out.println("mian 后 线程的中断标记:" + Thread.currentThread().isInterrupted()); //复位操作 true-> false
    }

}

isInterrupted() 只是用来判断是否有中断标志,不具有复位操作

interrupted() 用来判断线程是否有中断标志 但是interrupted()具有复位操作 调用后会清楚中断标志

源码:

public boolean isInterrupted() {
        return isInterrupted(false);
    }

    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

interrupted() 调用的也是isInterrupted() 方法 true表示复位

Thread.sleep()与interrupt

public class InterruptAndSleepDemo {

    public static void main(String[] args) throws InterruptedException {
        Thread t  = new Thread(()->{
            System.out.printf("线程标记%s \n" ,String.valueOf(Thread.currentThread().isInterrupted()));
            while(!Thread.currentThread().isInterrupted()){
                System.out.printf("输出类容s 线程标记%s \n" ,String.valueOf(Thread.currentThread().isInterrupted()));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(1000);
        t.interrupt();
    }
}

会抛出interrupted异常 同时会将线程的中断标志复位

看JDK sleep源码可知道 如果任何一个线程已经中断了当前线程,中断状态会清除,并抛出异常

 

2.异常中断线程

public class InterruptAndExcepionDemo {

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            System.out.println("前--->当前线程:" + Thread.currentThread().getName());

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String str = null;
            str.length();
            System.out.println("后--->当前线程:" + Thread.currentThread().getName());
        });
        t.start();
    }

}

 

3.设置中断标志

public class ThreadStopDemo {

    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread t = new Thread(myThread);
        t.start();
        Thread.sleep(100);
        myThread.setFlag(false);

    }

    static  class MyThread implements Runnable{
        private volatile boolean flag = true;
        public void setFlag(Boolean flag) {
            this.flag = flag;
        }
        @Override
        public void run() {
            while (flag){
                System.out.println("当前线程:" + Thread.currentThread().getName());
            }
        }
    }

 

注:flag需用volatile关键字修饰,保证变量的可见性。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值