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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章