Java怎樣使用interrupt()終止線程

這篇文章主要介紹了Java怎樣使用interrupt()終止線程,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),石樓企業(yè)網(wǎng)站建設(shè),石樓品牌網(wǎng)站建設(shè),網(wǎng)站定制,石樓網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,石樓網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

一、interrupt() 說(shuō)明

interrupt()的作用是中斷本線程。

本線程中斷自己是被允許的;其它線程調(diào)用本線程的interrupt()方法時(shí),會(huì)通過(guò)checkAccess()檢查權(quán)限。這有可能拋出SecurityException異常。

如果本線程是處于阻塞狀態(tài):調(diào)用線程的wait(), wait(long)或wait(long, int)會(huì)讓它進(jìn)入等待(阻塞)狀態(tài),或者調(diào)用線程的join(), join(long), join(long, int), sleep(long), sleep(long, int)也會(huì)讓它進(jìn)入阻塞狀態(tài)。若線程在阻塞狀態(tài)時(shí),調(diào)用了它的interrupt()方法,那么它的“中斷狀態(tài)”會(huì)被清除并且會(huì)收到一個(gè)InterruptedException異常。例如,線程通過(guò)wait()進(jìn)入阻塞狀態(tài),此時(shí)通過(guò)interrupt()中斷該線程;調(diào)用interrupt()會(huì)立即將線程的中斷標(biāo)記設(shè)為“true”,但是由于線程處于阻塞狀態(tài),所以該“中斷標(biāo)記”會(huì)立即被清除為“false”,同時(shí),會(huì)產(chǎn)生一個(gè)InterruptedException的異常。

如果線程被阻塞在一個(gè)Selector選擇器中,那么通過(guò)interrupt()中斷它時(shí);線程的中斷標(biāo)記會(huì)被設(shè)置為true,并且它會(huì)立即從選擇操作中返回。

如果不屬于前面所說(shuō)的情況,那么通過(guò)interrupt()中斷線程時(shí),它的中斷標(biāo)記會(huì)被設(shè)置為“true”。
中斷一個(gè)“已終止的線程”不會(huì)產(chǎn)生任何操作。

二、終止線程的方式

Thread中的stop()和suspend()方法,由于固有的不安全性,已經(jīng)建議不再使用!
下面,我先分別討論線程在“阻塞狀態(tài)”和“運(yùn)行狀態(tài)”的終止方式,然后再總結(jié)出一個(gè)通用的方式。

1、終止處于“阻塞狀態(tài)”的線程

通常,我們通過(guò)“中斷”方式終止處于“阻塞狀態(tài)”的線程。

當(dāng)線程由于被調(diào)用了sleep(), wait(), join()等方法而進(jìn)入阻塞狀態(tài);若此時(shí)調(diào)用線程的interrupt()將線程的中斷標(biāo)記設(shè)為true。由于處于阻塞狀態(tài),中斷標(biāo)記會(huì)被清除,同時(shí)產(chǎn)生一個(gè)InterruptedException異常。將InterruptedException放在適當(dāng)?shù)奈恢镁湍芙K止線程,形式如下:

@Override
public void run() {
  try {
    while (true) {
      // 執(zhí)行任務(wù)...
    }
  } catch (InterruptedException ie) { 
    // 由于產(chǎn)生InterruptedException異常,退出while(true)循環(huán),線程終止!
  }
}

說(shuō)明:在while(true)中不斷的執(zhí)行任務(wù),當(dāng)線程處于阻塞狀態(tài)時(shí),調(diào)用線程的interrupt()產(chǎn)生InterruptedException中斷。中斷的捕獲在while(true)之外,這樣就退出了while(true)循環(huán)!

注意:對(duì)InterruptedException的捕獲務(wù)一般放在while(true)循環(huán)體的外面,這樣,在產(chǎn)生異常時(shí)就退出了while(true)循環(huán)。否則,InterruptedException在while(true)循環(huán)體之內(nèi),就需要額外的添加退出處理。形式如下:

@Override
public void run() {
  while (true) {
    try {
      // 執(zhí)行任務(wù)...
    } catch (InterruptedException ie) { 
      // InterruptedException在while(true)循環(huán)體內(nèi)。
      // 當(dāng)線程產(chǎn)生了InterruptedException異常時(shí),while(true)仍能繼續(xù)運(yùn)行!需要手動(dòng)退出
      break;
    }
  }
}

說(shuō)明:上面的InterruptedException異常的捕獲在whle(true)之內(nèi)。當(dāng)產(chǎn)生InterruptedException異常時(shí),被catch處理之外,仍然在while(true)循環(huán)體內(nèi);要退出while(true)循環(huán)體,需要額外的執(zhí)行退出while(true)的操作。

2、終止處于“運(yùn)行狀態(tài)”的線程

通常,我們通過(guò)“標(biāo)記”方式終止處于“運(yùn)行狀態(tài)”的線程。其中,包括“中斷標(biāo)記”和“額外添加標(biāo)記”。

(1)通過(guò)“中斷標(biāo)記”終止線程

形式如下:

@Override
public void run() {
  while (!isInterrupted()) {
    // 執(zhí)行任務(wù)...
  }
}

說(shuō)明:isInterrupted()是判斷線程的中斷標(biāo)記是不是為true。當(dāng)線程處于運(yùn)行狀態(tài),并且我們需要終止它時(shí),可以調(diào)用線程的interrupt()方法,使用線程的中斷標(biāo)記為true,即isInterrupted()會(huì)返回true。此時(shí),就會(huì)退出while循環(huán)。
注意:interrupt()并不會(huì)終止處于“運(yùn)行狀態(tài)”的線程!它會(huì)將線程的中斷標(biāo)記設(shè)為true。

(2)通過(guò)“額外添加標(biāo)記”。

形式如下:

private volatile boolean flag= true;
protected void stopTask() {
  flag = false;
}

@Override
public void run() {
  while (flag) {
    // 執(zhí)行任務(wù)...
  }
}

說(shuō)明:線程中有一個(gè)flag標(biāo)記,它的默認(rèn)值是true;并且我們提供stopTask()來(lái)設(shè)置flag標(biāo)記。當(dāng)我們需要終止該線程時(shí),調(diào)用該線程的stopTask()方法就可以讓線程退出while循環(huán)。

注意:將flag定義為volatile類型,是為了保證flag的可見(jiàn)性。即其它線程通過(guò)stopTask()修改了flag之后,本線程能看到修改后的flag的值。

綜合線程處于“阻塞狀態(tài)”和“運(yùn)行狀態(tài)”的終止方式,比較通用的終止線程的形式如下:

@Override
public void run() {
  try {
    // 1. isInterrupted()保證,只要中斷標(biāo)記為true就終止線程。
    while (!isInterrupted()) {
      // 執(zhí)行任務(wù)...
    }
  } catch (InterruptedException ie) { 
    // 2. InterruptedException異常保證,當(dāng)InterruptedException異常產(chǎn)生時(shí),線程被終止。
  }
}

三、終止線程的示例

interrupt()常常被用來(lái)終止“阻塞狀態(tài)”線程。參考下面示例:

package com.demo.interrupt;

public class MyThread extends Thread{
  
  public MyThread(String name) {
    super(name);
  }
  
  @Override
  public void run() {
    try { 
      int i=0;
      while (!isInterrupted()) {
        Thread.sleep(100); // 休眠100ms
        i++;
        System.out.println(Thread.currentThread().getName()+" ("+this.getState()+") loop " + i); 
      }
    } catch (InterruptedException e) { 
      System.out.println(Thread.currentThread().getName() +" ("+this.getState()+") catch InterruptedException."); 
    }
  }
}
package com.demo.interrupt;

public class Demo1 {
  
  public static void main(String[] args) { 
    try { 
      Thread t1 = new MyThread("t1"); // 新建“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is new."); 

      t1.start(); // 啟動(dòng)“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is started."); 

      // 主線程休眠300ms,然后主線程給t1發(fā)“中斷”指令。
      Thread.sleep(300);
      t1.interrupt();
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");

      // 主線程休眠300ms,然后查看t1的狀態(tài)。
      Thread.sleep(300);
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
    } catch (InterruptedException e) { 
      e.printStackTrace();
    }
  } 
}

運(yùn)行結(jié)果:

t1 (NEW) is new.
t1 (RUNNABLE) is started.
t1 (RUNNABLE) loop 1
t1 (RUNNABLE) loop 2
t1 (TIMED_WAITING) is interrupted.
t1 (RUNNABLE) catch InterruptedException.
t1 (TERMINATED) is interrupted now.

結(jié)果說(shuō)明:

(01) 主線程main中通過(guò)new MyThread("t1")創(chuàng)建線程t1,之后通過(guò)t1.start()啟動(dòng)線程t1。

(02) t1啟動(dòng)之后,會(huì)不斷的檢查它的中斷標(biāo)記,如果中斷標(biāo)記為“false”,則休眠100ms。

(03) t1休眠之后,會(huì)切換到主線程main;主線程再次運(yùn)行時(shí),會(huì)執(zhí)行t1.interrupt()中斷線程t1。t1收到中斷指令之后,會(huì)將t1的中斷標(biāo)記設(shè)置“false”,而且會(huì)拋出InterruptedException異常。在t1的run()方法中,是在循環(huán)體while之外捕獲的異常;因此循環(huán)被終止。

我們對(duì)上面的結(jié)果進(jìn)行小小的修改,將run()方法中捕獲InterruptedException異常的代碼塊移到while循環(huán)體內(nèi)。

package com.demo.interrupt;

public class MyThread extends Thread{
  
   public MyThread(String name) {
     super(name);
   }
   
   @Override
   public void run() {
    int i=0;
    while (!isInterrupted()) {
      try {
        Thread.sleep(100); // 休眠100ms
      } catch (InterruptedException ie) { 
        System.out.println(Thread.currentThread().getName() +" ("+this.getState()+") catch InterruptedException."); 
      }
      i++;
      System.out.println(Thread.currentThread().getName()+" ("+this.getState()+") loop " + i); 
    }
   }

}
package com.demo.interrupt;

public class Demo2 {

  public static void main(String[] args) { 
    try { 
      Thread t1 = new MyThread("t1"); // 新建“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is new."); 
    
      t1.start();// 啟動(dòng)“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is started."); 
    
      // 主線程休眠300ms,然后主線程給t1發(fā)“中斷”指令。
      Thread.sleep(300);
      t1.interrupt();
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");
    
      // 主線程休眠300ms,然后查看t1的狀態(tài)。
      Thread.sleep(300);
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
    } catch (InterruptedException e) { 
      e.printStackTrace();
    }
  } 

}

運(yùn)行結(jié)果:

t1 (NEW) is new.
t1 (RUNNABLE) is started.
t1 (RUNNABLE) loop 1
t1 (RUNNABLE) loop 2
t1 (TIMED_WAITING) is interrupted.
t1 (RUNNABLE) catch InterruptedException.
t1 (RUNNABLE) loop 3
t1 (RUNNABLE) loop 4
t1 (RUNNABLE) loop 5
t1 (TIMED_WAITING) is interrupted now.
t1 (RUNNABLE) loop 6
t1 (RUNNABLE) loop 7
t1 (RUNNABLE) loop 8
t1 (RUNNABLE) loop 9
....

結(jié)果說(shuō)明:

程序進(jìn)入了死循環(huán)!

為什么會(huì)這樣呢?這是因?yàn)?,t1在“等待(阻塞)狀態(tài)”時(shí),被interrupt()中斷;此時(shí),會(huì)清除中斷標(biāo)記[即isInterrupted()會(huì)返回false],而且會(huì)拋出InterruptedException異常[該異常在while循環(huán)體內(nèi)被捕獲]。因此,t1理所當(dāng)然的會(huì)進(jìn)入死循環(huán)了。
解決該問(wèn)題,需要我們?cè)诓东@異常時(shí),額外的進(jìn)行退出while循環(huán)的處理。例如,在MyThread的catch(InterruptedException)中添加break 或 return就能解決該問(wèn)題。

下面是通過(guò)“額外添加標(biāo)記”的方式終止“狀態(tài)狀態(tài)”的線程的示例:

package com.demo.interrupt;

public class MyThread extends Thread {

  private volatile boolean flag= true;
  public void stopTask() {
    flag = false;
  }
  
  public MyThread(String name) {
    super(name);
  }

  @Override
  public void run() {
    synchronized(this) {
      try {
        int i=0;
        while (flag) {
          Thread.sleep(100); // 休眠100ms
          i++;
          System.out.println(Thread.currentThread().getName()+" ("+this.getState()+") loop " + i); 
        }
      } catch (InterruptedException ie) { 
        System.out.println(Thread.currentThread().getName() +" ("+this.getState()+") catch InterruptedException."); 
      }
    } 
  }
}
package com.demo.interrupt;

public class Demo3 {
  
   public static void main(String[] args) { 
    try { 
      MyThread t1 = new MyThread("t1"); // 新建“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is new."); 

      t1.start(); // 啟動(dòng)“線程t1”
      System.out.println(t1.getName() +" ("+t1.getState()+") is started."); 

      // 主線程休眠300ms,然后主線程給t1發(fā)“中斷”指令。
      Thread.sleep(300);
      t1.stopTask();
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");

      // 主線程休眠300ms,然后查看t1的狀態(tài)。
      Thread.sleep(300);
      System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
    } catch (InterruptedException e) { 
      e.printStackTrace();
    }
  } 
}

運(yùn)行結(jié)果:

t1 (NEW) is new.
t1 (RUNNABLE) is started.
t1 (RUNNABLE) loop 1
t1 (RUNNABLE) loop 2
t1 (RUNNABLE) is interrupted.
t1 (RUNNABLE) loop 3
t1 (TERMINATED) is interrupted now.

四、interrupt() 和 isInterrupted()的區(qū)別

最后談?wù)?interrupt() 和 isInterrupted()。interrupt() 和 isInterrupted()都能夠用于檢測(cè)對(duì)象的“中斷標(biāo)記”。區(qū)別是,interrupt()除了返回中斷標(biāo)記之外,它還會(huì)清除中斷標(biāo)記(即將中斷標(biāo)記設(shè)為false);而isInterrupted()僅僅返回中斷標(biāo)記。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java怎樣使用interrupt()終止線程”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

標(biāo)題名稱:Java怎樣使用interrupt()終止線程
本文URL:http://muchs.cn/article10/isjcdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄、做網(wǎng)站、關(guān)鍵詞優(yōu)化、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)