interrupt(),interrupted() 和 isInterrupted() 的區別

1. 結論先行

interrupt():將調用該方法的對象所表示的線程標記一箇中止標記,並非真的中止該線程。

interrupted():獲取當前線程的中斷狀態,而且會清除線程的狀態標記。是一個是靜態方法。html

isInterrupted():獲取調用該方法的對象所表示的線程,不會清除線程的狀態標記。是一個實例方法。微信

如今對各方法逐一進行具體介紹:ide

2. interrupt()

首先咱們來使用一下 interrupt() 方法,觀察效果,代碼以下:測試

public class MainTest {
   @Test
   public void test() {
       try {
           MyThread01 myThread = new MyThread01();
           myThread.start();
           myThread.sleep(2000);
           myThread.interrupt();
       } catch (Exception e) {
           System.out.println("main catch");
           e.printStackTrace();
       }
   }
}

public class MyThread01 extends Thread {
   @Override
   public void run() {
       super.run();
       for (int i = 0; i < 500; i++) {
           System.out.println("i= " + i);
       }
   }
}

輸出結果:spa

image

能夠看出,子線程已經執行完成了。說明 interrupt() 方法是不能讓線程中止,和咱們一開始所說的那樣,它僅僅是在當前線程記下一個中止標記而已。線程

那麼這個中止標記咱們又怎麼知道呢?——此時就要介紹下面的 interrupted() 和 isInterrupted() 方法了。code

3. interrupted() 和 isInterrupted()

  • interrupted() 方法的聲明爲 public static boolean interrupted()
  • isInterrupted() 方法的聲明爲 public boolean isInterrupted()

這兩個方法很類似,下面咱們用程序來看下使用效果上的區別吧htm

先來看下使用 interrupted() 的程序。對象

@Test
public void test() {
      try {
          MyThread01 myThread = new MyThread01();
          myThread.start();
          myThread.sleep(1000);
//     7行: Thread.currentThread().interrupt(); // Thread.currentThread() 這裏表示 main 線程
          myThread.interrupt();
          // myThread.interrupted() 底層調用了 currentThread().isInterrupted(true); 做用是判斷當前線程是否爲中止狀態
          System.out.println("是否中斷1 " + myThread.interrupted());
          System.out.println("是否中斷2 " + myThread.interrupted());
      } catch (InterruptedException e) {
          System.out.println("main catch");
      }
  System.out.println("main end");
}

輸出結果:blog

image

由此能夠看出,線程並未中止,同時也證實了 interrupted() 方法的解釋:測試當前線程是否已經中斷,這個當前線程就是 main 線程,它從未中斷過,因此打印結果都是 false。

那麼如何使 main 線程產生中斷效果呢?將上面第 8 行代碼註釋掉,並將第 7 行代碼的註釋去掉再運行,咱們就能夠獲得如下輸出結果:

image

從結果上看,方法 interrupted() 的確判斷出了當前線程(此例爲 main 線程)是不是中止狀態了,但爲何第二個布爾值爲 false 呢?咱們在最開始的時候有說過——interrupted() 測試當前線程是否已是中斷狀態,執行後會將狀態標誌清除。

由於執行 interrupted() 後它會將狀態標誌清除,底層調用了 isInterrupted(true),此處參數爲 true 。因此 interrupted() 具備清除狀態標記功能。

在第一次調用時,因爲此前執行了 Thread.currentThread().interrupt();,致使當前線程被標記了一箇中斷標記,所以第一次調用 interrupted() 時返回 true。由於 interrupted() 具備清除狀態標記功能,因此在第二次調用 interrupted() 方法時會返回 false。

以上就是 interrupted() 的介紹內容,最後咱們再來看下 isInterrupted() 方法吧。

isInterrupted() 和 interrupted() 有兩點不一樣:一是不具備清除狀態標記功能,由於底層傳入 isInterrupted() 方法的參數爲 false。二是它判斷的線程調用該方法的對象所表示的線程,本例爲 MyThread01 對象。

咱們修改一下上面的代碼,看下運行效果:

@Test
public void test() {
   try {
       MyThread01 myThread = new MyThread01();
       myThread.start();
       myThread.sleep(1000);
       myThread.interrupt();
       // 修改了下面這兩行。
       // 上面的代碼是 myThread.interrupted();
       System.out.println("是否中斷1 " + myThread.isInterrupted());
       System.out.println("是否中斷2 " + myThread.isInterrupted());
   } catch (InterruptedException e) {
       System.out.println("main catch");
       e.printStackTrace();
   }
   System.out.println("main end");
}

輸出結果:

image

結果很明顯,由於 isInterrupted() 不具備清除狀態標記功能,因此兩次都輸出 true。

參考文章:http://www.cnblogs.com/hapjin...

歡迎關注微信公衆號「不僅Java」,後臺回覆「電子書」,送說不定有你想要的呢
圖片描述

相關文章
相關標籤/搜索