线程中断的三个方法的区别(interrupt/isInterrupted/interrupted)

线程中断的三个方法的区别(interrupt/isInterrupted/interrupted)-总结自Java编程之美

方法定义
  • void interrupt():中断调用该方法的实例线程对象。当线程A正在运行时,线程B调用了线程A的interrupt方法后,就会给线程A设置中断标志为true,并且返回。

    设置中断标志仅仅只是设置中断标志,线程A的运行并没有没中断,它会继续往下执行。如果线程A因为调用了wait系列函数、join方法或者sleep方法而被阻塞挂起,这时候线程B调用了线程A的interrupt方法,线程A就会在调用这些方法的地方抛出InterruptedException异常。

    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(() -> {
            System.out.println("threadOne begin sleep 2000s");
            try {
                Thread.sleep(2000 * 1000);
            } catch (InterruptedException e) {
                System.out.println("threadOne is interrupted when sleeping");
                return;
            }
            System.out.println("threadOne end sleep 2000s");
        });
        threadOne.start();
    
        Thread.sleep(1000);
    	// 线程threadOne处于sleep状态,调用interrupt方法,线程threadOne抛出异常被catch捕获到返回
        threadOne.interrupt();
    
        threadOne.join();
    
        System.out.println("main thread is over");
    }
    

    控制台输出:

    threadOne begin sleep 2000s
    threadOne is interrupted when sleeping
    main thread is over
    
  • boolean isInterrupted():检测调用该方法的实例线程对象是否被中断,如果是返回true,反之返回false。

    public boolean isInterrupted() {
    	return isInterrupted(false);
    }
    
  • boolean interrupted():检测当前方法调用所在的线程是否被中断,如果是则清除中断标志并且返回true,反之返回false。该方法为static,可以通过Thread类直接调用。

    public static boolean interrupted() {
    	return currentThread().isInterrupted(true);
    }
    
三者作用的对象区别:

interrupt:当前调用该方法的实例线程对象(如下例中代表threadOne线程)

isInterrupted:作用的对象和interrupt方法一致

interrupted:当前调用该方法所在的线程(如下例中代表main线程)

public static void main(String[] args) throws InterruptedException {
    Thread threadOne = new Thread(() -> {
        System.out.println("threadOne begin...");
        for (;;) {

        }
    });
    threadOne.start();
    Thread.sleep(1000);
    // 给threadOne线程设置中断标志
    threadOne.interrupt();
    // 判断threadOne线程是否中断
    System.out.println(threadOne.isInterrupted());

    //判断调用该方法的线程是否处于中断状态(也就是main线程)
    System.out.println(threadOne.interrupted());

    //判断调用该方法的线程是否处于中断状态(也就是main线程)
    System.out.println(Thread.interrupted());

    // 判断threadOne线程是否中断
    System.out.println(threadOne.isInterrupted());

    // 阻塞main线程,等待threadOne执行完毕继续执行
    threadOne.join();

    System.out.println("main thread is over");
}

控制台输出:

threadOne begin...
true
false
false
true

补充:

证实interrupted()方法能清除中断标志

public static void main(String[] args) throws InterruptedException {
    Thread threadOne = new Thread(() -> {
        System.out.println("threadOne begin...");
        // 判断是否中断,如果为true则清除中断标志,并且跳出循环
        while (!Thread.interrupted()) {

        }
        System.out.println("threadOne isInterrupted: " + Thread.currentThread().isInterrupted());
    });
    threadOne.start();
    Thread.sleep(5000);

    // 给threadOne线程设置中断标志
    threadOne.interrupt();

    // 阻塞main线程,等待threadOne执行完毕继续执行
    threadOne.join();

    System.out.println("main thread is over");
}

控制台输出:

threadOne begin...
threadOne isInterrupted: false
main thread is over
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值