java 多线程编程系列之4----线程的“打断”

interrupt相关的三个方法:

interrupt(): 实列方法 如t.interrupt() 打断t线程,设置t线程的标志位f=true,并不是打断t线程的运行,就是告诉t线程想打断它的意思。

isInterrupted():查询打断标注位是否被设置(是不是曾经被打断过的意思)

interrupted():查看当前线程是否被打断,如果被打断则恢复标志位f=false

        interrupt()测试:

public class TestThread {

    @SneakyThrows
    public static void main(String[] s){        
        Thread t = new Thread(()->{
            for (;;){
                //当前线程是否被打断过(是否设置了打断标志=true)
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("t is interrupted");
                    System.out.println(Thread.currentThread().isInterrupted());
                    //虽然被设置了打断标志=true,但当前线程会继续运行,因为没有对打断标志进行处理
                    //break;如果放开此处的注释,线程就会马上结束掉。
                }
            }
        });
        t.start();
        TimeUnit.SECONDS.sleep(2);
        t.interrupt();//设置t线程的打断标志位为ture,是否结束t线程由于t线程自己来判断处理

    }
}

         interrupted()测试

public class TestThread {

    @SneakyThrows
    public static void main(String[] s){        
        Thread t = new Thread(()->{
            for (;;){
                if (Thread.interrupted()){//查看当前线程是否被打断过,如有则重置打断标识=false
                    System.out.println("t is interrupted");
                    System.out.println(Thread.currentThread().isInterrupted());//false
                }
            }
        });
        t.start();
        TimeUnit.SECONDS.sleep(2);
        t.interrupt();//设置t线程的打断标志位为ture,是否结束t线程由于t线程自己来判断处理

    }
}

         Thread.interrupted()会把标志位设置为false,只能当前线程来调用,如果你在main方法线程里调用的结果是不一样的,则是main方法线程的interrupted();

    InterruptedException异常测试:

如果一个线程在sleep,wati,join的时候被interrupt()就会抛异常InterruptedException,这时你可以有多种处理方式,比如你可以主动结束线程也可以继续执行线程,不是说你一定要结束线程。

public class TestThread {

    @SneakyThrows
    public static void main(String[] s){        
        Thread t = new Thread(()->{
            for (;;){
                try {
                    Thread.sleep(17000);
                } catch (InterruptedException e) {
                    System.out.println("t is interrupted");
                    //catch异常之后会默认给你恢复标志位=false
                    System.out.println(Thread.currentThread().isInterrupted());
                }

            }
        });
        t.start();
        TimeUnit.SECONDS.sleep(6);
        t.interrupt();

    }
}

        interrup是否干扰枪锁

        interrup不能干扰要去竞争锁的线程t2,因为t2是通过lock.lock()去抢锁的,该去拿锁还得去拿不会抛异常。所以如果你要想达到线程在等锁的时候能被干扰到你就用lock.lockInterruptibly()去枪锁,此时t2去枪锁可以被打断,这时如果线程被interrupt()了就会抛异常,然后你catch到异常了就可以做其他操作。

public class TestThread {

    private static ReentrantLock lock = new ReentrantLock();
    @SneakyThrows
    public static void main(String[] s){

        Thread t1 = new Thread(()->{
           lock.lock();
           try {
               Thread.sleep(10000);
           }catch (InterruptedException e){
               e.printStackTrace();
           }finally {
               lock.unlock();//释放锁
           }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(1);

        Thread t2 = new Thread(()->{
            lock.lock();
            //lock.lockInterruptibly();
            try {
            }finally {
                lock.unlock();//释放锁
            }
            System.out.println("t2 end!");
        });
        t2.start();
        TimeUnit.SECONDS.sleep(1);
        t2.interrupt();
    }
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值