国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java如何使用interrupt()終止線程

瀏覽:50日期:2022-09-03 17:42:04

一、interrupt() 說明

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

本線程中斷自己是被允許的;其它線程調用本線程的interrupt()方法時,會通過checkAccess()檢查權限。這有可能拋出SecurityException異常。

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

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

如果不屬于前面所說的情況,那么通過interrupt()中斷線程時,它的中斷標記會被設置為“true”。中斷一個“已終止的線程”不會產生任何操作。

二、終止線程的方式

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

1、終止處于“阻塞狀態”的線程

通常,我們通過“中斷”方式終止處于“阻塞狀態”的線程。

當線程由于被調用了sleep(), wait(), join()等方法而進入阻塞狀態;若此時調用線程的interrupt()將線程的中斷標記設為true。由于處于阻塞狀態,中斷標記會被清除,同時產生一個InterruptedException異常。將InterruptedException放在適當的位置就能終止線程,形式如下:

@Overridepublic void run() { try { while (true) { // 執行任務... } } catch (InterruptedException ie) { // 由于產生InterruptedException異常,退出while(true)循環,線程終止! }}

說明:在while(true)中不斷的執行任務,當線程處于阻塞狀態時,調用線程的interrupt()產生InterruptedException中斷。中斷的捕獲在while(true)之外,這樣就退出了while(true)循環!

注意:對InterruptedException的捕獲務一般放在while(true)循環體的外面,這樣,在產生異常時就退出了while(true)循環。否則,InterruptedException在while(true)循環體之內,就需要額外的添加退出處理。形式如下:

@Overridepublic void run() { while (true) { try { // 執行任務... } catch (InterruptedException ie) { // InterruptedException在while(true)循環體內。 // 當線程產生了InterruptedException異常時,while(true)仍能繼續運行!需要手動退出 break; } }}

說明:上面的InterruptedException異常的捕獲在whle(true)之內。當產生InterruptedException異常時,被catch處理之外,仍然在while(true)循環體內;要退出while(true)循環體,需要額外的執行退出while(true)的操作。

2、終止處于“運行狀態”的線程

通常,我們通過“標記”方式終止處于“運行狀態”的線程。其中,包括“中斷標記”和“額外添加標記”。

(1)通過“中斷標記”終止線程

形式如下:

@Overridepublic void run() { while (!isInterrupted()) { // 執行任務... }}

說明:isInterrupted()是判斷線程的中斷標記是不是為true。當線程處于運行狀態,并且我們需要終止它時,可以調用線程的interrupt()方法,使用線程的中斷標記為true,即isInterrupted()會返回true。此時,就會退出while循環。注意:interrupt()并不會終止處于“運行狀態”的線程!它會將線程的中斷標記設為true。

(2)通過“額外添加標記”。

形式如下:

private volatile boolean flag= true;protected void stopTask() { flag = false;}@Overridepublic void run() { while (flag) { // 執行任務... }}

說明:線程中有一個flag標記,它的默認值是true;并且我們提供stopTask()來設置flag標記。當我們需要終止該線程時,調用該線程的stopTask()方法就可以讓線程退出while循環。

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

綜合線程處于“阻塞狀態”和“運行狀態”的終止方式,比較通用的終止線程的形式如下:

@Overridepublic void run() { try { // 1. isInterrupted()保證,只要中斷標記為true就終止線程。 while (!isInterrupted()) { // 執行任務... } } catch (InterruptedException ie) { // 2. InterruptedException異常保證,當InterruptedException異常產生時,線程被終止。 }}

三、終止線程的示例

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

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); // 休眠100msi++;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(); // 啟動“線程t1” System.out.println(t1.getName() +' ('+t1.getState()+') is started.'); // 主線程休眠300ms,然后主線程給t1發“中斷”指令。 Thread.sleep(300); t1.interrupt(); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted.'); // 主線程休眠300ms,然后查看t1的狀態。 Thread.sleep(300); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted now.'); } catch (InterruptedException e) { e.printStackTrace(); } } }

運行結果:

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

結果說明:

(01) 主線程main中通過new MyThread('t1')創建線程t1,之后通過t1.start()啟動線程t1。

(02) t1啟動之后,會不斷的檢查它的中斷標記,如果中斷標記為“false”,則休眠100ms。

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

我們對上面的結果進行小小的修改,將run()方法中捕獲InterruptedException異常的代碼塊移到while循環體內。

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();// 啟動“線程t1” System.out.println(t1.getName() +' ('+t1.getState()+') is started.'); // 主線程休眠300ms,然后主線程給t1發“中斷”指令。 Thread.sleep(300); t1.interrupt(); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted.'); // 主線程休眠300ms,然后查看t1的狀態。 Thread.sleep(300); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted now.'); } catch (InterruptedException e) { e.printStackTrace(); } } }

運行結果:

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

結果說明:

程序進入了死循環!

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

下面是通過“額外添加標記”的方式終止“狀態狀態”的線程的示例:

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(); // 啟動“線程t1” System.out.println(t1.getName() +' ('+t1.getState()+') is started.'); // 主線程休眠300ms,然后主線程給t1發“中斷”指令。 Thread.sleep(300); t1.stopTask(); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted.'); // 主線程休眠300ms,然后查看t1的狀態。 Thread.sleep(300); System.out.println(t1.getName() +' ('+t1.getState()+') is interrupted now.'); } catch (InterruptedException e) { e.printStackTrace(); } } }

運行結果:

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

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

最后談談 interrupt() 和 isInterrupted()。interrupt() 和 isInterrupted()都能夠用于檢測對象的“中斷標記”。區別是,interrupt()除了返回中斷標記之外,它還會清除中斷標記(即將中斷標記設為false);而isInterrupted()僅僅返回中斷標記。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 色aaa| 香港三级日本三级人妇网站 | 全免费a级毛片免费看 | 久久精品视频在线观看榴莲视频 | 亚洲日韩精品欧美一区二区 | 亚洲精品成人在线 | 亚洲一级视频在线观看 | 男女视频免费 | 日韩区 | 国产在线一区二区三区在线 | 韩国精品欧美一区二区三区 | 日韩久久久精品首页 | 国产20岁美女一级毛片 | 精品国产一区二区三区四区vr | 国产精品欧美亚洲韩国日本 | 26uuu天天夜夜综合 | 日韩一级欧美一级一级国产 | 久久中文字幕日韩精品 | 亚洲综合爱久久影院 | 午夜精品一区二区三区在线观看 | 亚洲精品线在线观看 | 欧美一级毛片免费播放aa | 亚洲欧美国产精品专区久久 | 99成人精品| 久久综合99re久久爱 | 天天插夜夜爽 | 岛国在线永久免费视频 | 日本一区二区三区不卡视频中文字幕 | 亚洲一区二区三区四区 | 欧美另类视频一区二区三区 | 国产专区一区 | 亚洲精品人成在线观看 | 国产在线步兵一区二区三区 | 俄罗斯一级毛片免费播放 | 毛片免费全部免费播放 | 狠狠干香蕉 | 国产成人www免费人成看片 | 国产精品成人免费 | 亚洲国内 | 一区二区三区四区免费视频 | 国产萌白酱在线一区二区 |