日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

一、interrupt() 說明

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

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

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

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

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

二、終止線程的方式

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

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

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

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

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

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

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

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

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

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

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

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

形式如下:

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

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

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

形式如下:

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

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

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

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

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

三、終止線程的示例

interrupt()常常被用來終止“阻塞狀態(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); // 休眠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發(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 1t1 (RUNNABLE) loop 2t1 (TIMED_WAITING) is interrupted.t1 (RUNNABLE) catch InterruptedException.t1 (TERMINATED) is interrupted now.

結(jié)果說明:

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

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

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

我們對上面的結(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();// 啟動“線程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 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....

結(jié)果說明:

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

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

下面是通過“額外添加標(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(); // 啟動“線程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 1t1 (RUNNABLE) loop 2t1 (RUNNABLE) is interrupted.t1 (RUNNABLE) loop 3t1 (TERMINATED) is interrupted now.

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲精品免费观看| 亚洲精品123区| 伊人国产精品| 黄色亚洲在线| 日韩伦理一区| 日韩大片在线播放| 日产午夜精品一线二线三线| 久久97久久97精品免视看秋霞| 国产精品免费大片| 国产精品巨作av| 久久av电影| 久久天堂影院| 日产精品一区| 久久精品国产68国产精品亚洲| 91精品久久久久久久久久不卡| 日韩一区亚洲二区| 欧美日韩中文一区二区| 欧美特黄一级大片| 香蕉国产精品| 亚洲天堂久久| 一区福利视频| 亚洲免费婷婷| 亚洲ww精品| 青青国产精品| 麻豆精品一区二区综合av| 麻豆91精品视频| 一区二区精品伦理...| 久久影视一区| 亚洲视频二区| 国产高清日韩| 91日韩免费| 国精品一区二区三区| 午夜在线一区| 日韩精品一区二区三区免费视频| 日韩三级视频| 欧美黑人巨大videos精品| 欧美一区久久久| 中文亚洲欧美| 青青草国产精品亚洲专区无| 精品国产欧美日韩| 久久视频一区| 影音先锋久久精品| 欧美黑人做爰爽爽爽| 久久精品国产大片免费观看| 日韩一区精品| 樱桃视频成人在线观看| 国产精品嫩草99av在线| 日韩成人av影视| 久久99国产精品视频| 三级精品视频| 日韩综合一区二区| 国产自产自拍视频在线观看| 亚洲免费一区二区| 久久av网站| 另类av一区二区| 美女国产精品久久久| 欧美日韩第一| 国产精品久久久久久久久久久久久久久| 国产一区二区三区国产精品| 水蜜桃久久夜色精品一区的特点| 国产精品超碰| 久久亚洲欧洲| 成人在线超碰| 久久亚洲欧美| 国产一区二区三区亚洲综合| 国产精品婷婷| 久久中文字幕导航| 久久av在线| 日韩中文影院| 国产探花在线精品一区二区| 米奇777超碰欧美日韩亚洲| 国产视频一区二区在线播放| 日韩专区在线视频| 精品国产不卡一区二区| 国产亚洲久久| 久久的色偷偷| 最近国产精品视频| 日韩午夜免费| 亚洲在线国产日韩欧美| 国产成人精品三级高清久久91| 国产在线成人| 国产精品日本一区二区不卡视频| 婷婷亚洲五月| 国产中文字幕一区二区三区| 亚洲va久久久噜噜噜久久| 青青久久av| 麻豆精品少妇| 午夜精品影视国产一区在线麻豆| 久久久久国产| 免费在线欧美黄色| 亚洲日本在线观看视频| 国产一区二区中文| а√天堂8资源在线| 国产日韩欧美一区二区三区| 亚洲少妇一区| 亚洲a在线视频| 国产精品99一区二区三区| 国产亚洲字幕| 中文一区一区三区免费在线观| 午夜精品成人av| 水蜜桃久久夜色精品一区的特点| 亚洲电影在线| 日本美女一区| 精品视频一区二区三区四区五区| 日韩国产欧美三级| 中文视频一区| 波多野结衣一区| 日韩精品麻豆| 国产成人黄色| 国产精品天天看天天狠| 福利一区二区免费视频| 国产精品mv在线观看| 日本成人在线视频网站| 模特精品在线| 欧美日韩国产免费观看视频| 国产综合精品一区| 日韩精品专区| av一区在线| 久久久久网站| 欧美日韩精品免费观看视完整 | 久久av网站| 91成人在线网站| 香蕉久久一区| 中文字幕亚洲影视| 免费欧美日韩| 视频国产精品| 日本午夜精品久久久| 日韩av中文字幕一区| 日本中文字幕一区二区| 日本亚洲最大的色成网站www | 欧美一区久久| 久久国产麻豆精品| 国产精品美女久久久久久不卡| 奇米狠狠一区二区三区| 日本aⅴ精品一区二区三区| 欧美天堂一区二区| 国产欧美日韩影院| 久久精品三级| 国产传媒在线观看| 色天使综合视频| 成人在线免费观看网站| 另类专区亚洲| 亚洲性图久久| 亚洲少妇在线| 日韩和欧美一区二区| 国产精品theporn| а√在线中文在线新版| 999久久久精品国产| 美女网站一区| 伊人精品久久| 国产精品久久久免费| 久久电影tv| 影音先锋国产精品| 亚洲精品一级二级三级| 欧美国产不卡| 久久久成人网| 麻豆亚洲精品| 奇米亚洲欧美| 中文字幕高清在线播放| 亚洲夜间福利| 亚洲精品日韩久久| 国产精品久久久久久久久久白浆 | 国产一区久久| 日韩精品一卡二卡三卡四卡无卡| 日韩激情av在线| 精品国产午夜肉伦伦影院| 99国内精品| 久久狠狠久久| 成人国产综合| 久久男人av资源站| 欧美另类专区| 97久久超碰| 日韩和欧美的一区| 欧美国产精品| 欧美极品一区二区三区| 久久国内精品视频| 国产精品不卡| 男女激情视频一区| 你懂的国产精品| 欧美精品黄色| 国产精品高清一区二区| 久久91导航| 欧美一级网站| 青青久久av| 亚洲专区视频| 国产不卡av一区二区| 亚洲综合二区| 国产一区丝袜| 鲁大师影院一区二区三区| 捆绑调教美女网站视频一区| 婷婷综合社区| 国产精品探花在线观看| 激情欧美日韩一区| 国产日韩1区| 亚洲午夜黄色| 久久精品国产福利| 亚洲一区二区av| 久久九九精品| 欧美精品成人| 好看的av在线不卡观看|