Interrupt总结

Interrupt系列理解

在调用如下方法进行阻塞的线程,都可以调用该线程的interrupt()方法打断其阻塞

  • Object的wait方法
  • Thread的sleep方法
  • Thread的join方法
  • InterruptibleChannel的io操作
  • Selector的wakeup方法

上述方法统称为可中断方法,实际上,能抛出InterruptedException异常的方法都是可中断方法

interrupt()方法

//源码

public void interrupt() {
    if (this != Thread.currentThread())
        checkAccess();
        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0();           // Just to set the interrupt flag
                b.interrupt(this);
                return;
            }
	}
	interrupt0();
}

当一个线程被别的线程调用它的阻塞方法时,它会调用interrupt0()设置一个中断标识,如果被interrupt的线程正在阻塞状态,该线程的阻塞状态会被中断并且中断标识被清除,如例一

//例一
package online.hengtian.Thread;

import java.util.concurrent.TimeUnit;

public class InterruptDemo {
    public static void main(String[] args){
        Thread t1=new Thread(()->{
            int i=0;
            while(i<5){
                System.out.println(i+" : 我当前的中断状态"+Thread.currentThread().isInterrupted());
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(i+" : 我没被中断");
                } catch (InterruptedException e) {
                    System.out.println("我被中断了");
                    System.out.println(i+" : 此时我的中断状态是"+Thread.currentThread().isInterrupted());
                }
                i++;
            }
        });
        t1.start();
        t1.interrupt();
    }
}
/**输出
0 : 我当前的中断状态true
我被中断了
0 : 此时我的中断状态是false
1 : 我当前的中断状态false
1 : 我没被中断
2 : 我当前的中断状态false
2 : 我没被中断
3 : 我当前的中断状态false
3 : 我没被中断
4 : 我当前的中断状态false
4 : 我没被中断
**/

如果被interrupt的线程并没有进入阻塞状态,该线程在进入阻塞状态后会立即被中断,然后清除中断状态,测试如下:

//当线程被interrupt之后才会进入sleep方法
package online.hengtian.Thread;

import java.util.concurrent.TimeUnit;

public class InterruptDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            //changed beginning
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("我当前没被中断");
            }
            //changed end
            int i=0;
            while(i<5){
                System.out.println(i+" : 我当前的中断状态"+Thread.currentThread().isInterrupted());
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(i+" : 我没被中断");
                } catch (InterruptedException e) {
                    System.out.println("我被中断了");
                    System.out.println(i+" : 此时我的中断状态是"+Thread.currentThread().isInterrupted());
                }
                i++;
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(2);
        t1.interrupt();
    }
}
/*截取部分输出如下
我当前没被中断
我当前没被中断
我当前没被中断
我当前没被中断
我当前没被中断
我当前没被中断
我当前没被中断
我当前没被中断
0 : 我当前的中断状态true
我被中断了
0 : 此时我的中断状态是false
1 : 我当前的中断状态false
1 : 我没被中断
2 : 我当前的中断状态false
2 : 我没被中断
3 : 我当前的中断状态false
3 : 我没被中断
4 : 我当前的中断状态false
4 : 我没被中断
*/

当理解了interrupt方法中断的根本原因是中断标识之后,一切都会变的很简单

![未命名表单 (2)](D:\Users\HengTian\Downloads\未命名表单 (2).png)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值