使用interrupt()中斷線程
當一個線程運行時,另一個線程可以調用對應的Thread對象的interrupt()方法來中斷它,該方法只是在目標線程中設置一個標誌,表示它已經被中斷,並立即返回。這裏需要注意的是,如果只是單純的調用interrupt()方法,線程並沒有實際被中斷,會繼續往下執行。
下面一段代碼演示了休眠線程的中斷:
- public class SleepInterrupt extends Object implements Runnable{
- public void run(){
- try{
- System.out.println("in run() - about to sleep for 20 seconds");
- Thread.sleep(20000);
- System.out.println("in run() - woke up");
- }catch(InterruptedException e){
- System.out.println("in run() - interrupted while sleeping");
- //處理完中斷異常後,返回到run()方法人口,
- //如果沒有return,線程不會實際被中斷,它會繼續打印下面的信息
- return;
- }
- System.out.println("in run() - leaving normally");
- }
- public static void main(String[] args) {
- SleepInterrupt si = new SleepInterrupt();
- Thread t = new Thread(si);
- t.start();
- //主線程休眠2秒,從而確保剛纔啓動的線程有機會執行一段時間
- try {
- Thread.sleep(2000);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- System.out.println("in main() - interrupting other thread");
- //中斷線程t
- t.interrupt();
- System.out.println("in main() - leaving");
- }
- }
待決中斷
在上面的例子中,sleep()方法的實現檢查到休眠線程被中斷,它會相當友好地終止線程,並拋出InterruptedException異常。另外一種情況,如果線程在調用sleep()方法前被中斷,那麼該中斷稱爲待決中斷,它會在剛調用sleep()方法時,立即拋出InterruptedException異常。
下面的代碼演示了待決中斷:
- public class PendingInterrupt extends Object {
- public static void main(String[] args){
- //如果輸入了參數,則在mian線程中中斷當前線程(亦即main線程)
- if( args.length > 0 ){
- Thread.currentThread().interrupt();
- }
- //獲取當前時間
- long startTime = System.currentTimeMillis();
- try{
- Thread.sleep(2000);
- System.out.println("was NOT interrupted");
- }catch(InterruptedException x){
- System.out.println("was interrupted");
- }
- //計算中間代碼執行的時間
- System.out.println("elapsedTime=" + ( System.currentTimeMillis() - startTime));
- }
- }
使用isInterrupted()方法判斷中斷狀態
- public class InterruptCheck extends Object{
- public static void main(String[] args){
- Thread t = Thread.currentThread();
- System.out.println("Point A: t.isInterrupted()=" + t.isInterrupted());
- //待決中斷,中斷自身
- t.interrupt();
- System.out.println("Point B: t.isInterrupted()=" + t.isInterrupted());
- System.out.println("Point C: t.isInterrupted()=" + t.isInterrupted());
- try{
- Thread.sleep(2000);
- System.out.println("was NOT interrupted");
- }catch( InterruptedException x){
- System.out.println("was interrupted");
- }
- //拋出異常後,會清除中斷標誌,這裏會返回false
- System.out.println("Point D: t.isInterrupted()=" + t.isInterrupted());
- }
- }
使用Thread.interrupted()方法判斷中斷狀態
可以使用Thread.interrupted()方法來檢查當前線程的中斷狀態(並隱式重置爲false)。又由於它是靜態方法,因此不能在特定的線程上使用,而只能報告調用它的線程的中斷狀態,如果線程被中斷,而且中斷狀態尚不清楚,那麼,這個方法返回true。與isInterrupted()不同,它將自動重置中斷狀態爲false,第二次調用Thread.interrupted()方法,總是返回false,除非中斷了線程。
如下代碼演示了Thread.interrupted()方法的使用:
- public class InterruptReset extends Object {
- public static void main(String[] args) {
- System.out.println(
- "Point X: Thread.interrupted()=" + Thread.interrupted());
- Thread.currentThread().interrupt();
- System.out.println(
- "Point Y: Thread.interrupted()=" + Thread.interrupted());
- System.out.println(
- "Point Z: Thread.interrupted()=" + Thread.interrupted());
- }
- }