Java并發(fā)編程(2):線程中斷(含代碼)-編程開發(fā)技術_第1頁
Java并發(fā)編程(2):線程中斷(含代碼)-編程開發(fā)技術_第2頁
Java并發(fā)編程(2):線程中斷(含代碼)-編程開發(fā)技術_第3頁
Java并發(fā)編程(2):線程中斷(含代碼)-編程開發(fā)技術_第4頁
Java并發(fā)編程(2):線程中斷(含代碼)-編程開發(fā)技術_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、java并發(fā)編程(2):線程中斷(含代碼)-編程 開發(fā)技術java并發(fā)編程(2):線程中斷(含代碼)原文出處:蘭亭風雨使用interrupt ()中斷線程當一個線程運行時,另一個線程可以調用對應的thread對象的interrupt () 方法來中斷它,該方法只是在目標線程中設置一個標志,表示它已經被中斷,并 立即返回。這里需要注意的是,如果只是單純的調用interrupt ()方法,線程 并沒有實際被中斷,會繼續(xù)往下執(zhí)行。卜面一段代碼演示了休眠線程的中斷:public class sleepinterrupt extends object implements runnable public

2、 void run() try system. out. printin(in run() - about to sleep for20 seconds");thread, sleep (20000);system. out. println(in run () - woke up);catch(interruptedexception e) system. out. printin ("in run() - interrupted while sleeping");/處理完中斷異常后,返回到run ()方法人口,/如果沒有return,線程不會實際被中斷,它會繼

3、續(xù) 打印下面的信息return;systemout. printin("in run() 一 leaving normally");public static void main (string args) sleepinterrupt si = new sleepinterrupt ();thread t = new thread (si);t. start ();/主線程休眠2秒,從而確保剛才啟動的線程有機會執(zhí)行一段 吋間try thread.sleep (2000); catch(interruptedexception e) e. printstacktrace (

4、);system, out. printin(/zin main() - interrupting otherthread");/中斷線程t t. interrupt ();system, out. println(in main() - leaving");運行結果如下:二 c:tiid0tssystea32cbd. ezef:thread>jauac sleeplnterrupt.jauaf:tbread>jaua sleeplnterruptin run<> 一 about to sleep for 20 seconds in naino 一

5、interrupting other thread in naino 一 leavingin run<> 一 interrupted while sleepingf:thread>搜狗拼音半:?主線程啟動新線程后,自身休眠2秒鐘,允許新線程獲得運行時間。新線程打印信息 "about to sleep for 20 seconds"后,繼而休眠20秒鐘,大約2秒鐘后,main線程通知新線程 中斷,那么新線程的20秒的休眠將被打斷,從而拋岀inteiruptexception界常,執(zhí)行跳轉到 catch塊,打印出“interrupted while sleep

6、ing”信息,并立即從run ()方法返回,然后消亡, 而不會打印出catch塊后面的“l(fā)eaving normally信息。?請注意:由于不確定的線程規(guī)劃,上圖運行結果的后兩行可能順序相反,這取決于主線 程和新線程哪個先消亡。但前兩行信息的順序必定如上圖所示。?另外,如果將catch塊中的return語句注釋掉,貝u線程在拋出異常后,會繼續(xù)往下執(zhí)行, 而不會被中斷,從而會打印出"leaving normally“信息。待決中斷在上面的例了中,sleep ()方法的實現(xiàn)檢杳到休眠線程被中斷,它會相當友好 地終止線程,并拋出interruptedexception異常。另外一種情況,如

7、果線程在 調用sleep ()方法前被中斷,那么該中斷稱為待決中斷,它會在剛調用sleep ()方法吋,立即拋lb interruptedexception 異常。下面的代碼演示了待決中斷:public class pcndinglntcrrupt cxtcnds object public static void main(string args) 線程)/如果輸入了參數(shù),則在mian線程中中斷當前線程(亦即mainif ( args, length > 0 ) thread, currcntthrcad(). intcrrupt();/獲取當前時間long starttime 二 s

8、ystem, currenttimem訂lis(); try thread, sleep(2000);system. out printin (zzwas not interrupted");catch(interruptedexception x) system.out. println(was interrupted);/計算中間代碼執(zhí)行的時間system out. printin("elapsedtime二"+(system. currenttimemillis() - starttime);如果pendinglntcrrupt不帶任何命令行參數(shù),那么線程不

9、會被中斷,最終輸出 的時間差距應該在2000附近(具體時間由系統(tǒng)決定,不精確),如果 pendinglnterrupt帯有命令彳亍參數(shù),則調用中斷當前線程的代碼,但main線程 仍然運行,最終輸出的吋間差距應該遠小于2000,因為線程尚未休眠,便被屮 斷,因此,一旦調用sleep ()方法,會立即打印出catch塊中的信息。執(zhí)彳亍結 果如下:f:thread>jaua pendinglnterrupt was not interruptedelapsedtime=2000f: thread>jaua pendinglnterrupt yes was interruptedelaps

10、edtime=0f:thread>搜狗拼音半:? ?這種模式下,main線程中斷它口身。除了將屮斷標志(它是thread的內部標志)設置 為true外,沒冇其他任何影響。線程被中斷了,但main線程仍然運行,main線程繼續(xù)監(jiān)視 實時時鐘,并進入try塊,一旦調用sleep ()方法,它就會注意到待決屮斷的存在,并拋出 interruptexceptiono于是執(zhí)行跳轉到catch塊,并打印出線程被中斷的信息。最后,計算并 打印出時間差。使用islnterrupted ()方法判斷中斷 狀態(tài)?可以在thread對象上調用islnterrupted ()方法來檢查任何線程的中斷狀態(tài)。這里需

11、要注 意:線程一旦被中斷,islnterrupted ()方法便會返回true,而一旦sleep ()方法拋出異常, 它將清空中斷標志,此時islnterrupted ()方法將返回false。? ?卜面的代碼演示了 islnterrupted ()方法的使用:publ ic class tnterruptcheck extends objectpublic static void main(string args)thread t = thread. currentthread();system, out. println(z/point a: t. islnterrupted()=/z +

12、t. islnterrupted();/待決中斷,中斷自身t. intcrrupt ();system, out println(point b: t. islnterrupted()=/z + t. islnterrupted();system, out. printin(z/point c: t. islnterrupted()=z/ + t. istnterrupted();thread, sleep(2000);system, out. println(wa.s not interrupted/z);catch( tnterruptedexception x) systcni. out

13、. println ("was intcrruptcd);/拋出異常后,會清除中斷標志,這里會返冋false system, out. printin(z/point d: t. islnterrupted()=z/ + t. istnterrupted();運行結果如下:i -c:yikd0yssysteb32cbd.exep:thread>javac int巳rruptcheck.javal itt7:thread>java interruptcheck toint a : t. is i nterruptedo =f alse toint b: t.isinterr

14、upted<> =true toint c: t.isinterrupted<> =true k*/as in t 巳 rrupt e d toint d: t. is i nterruptedo =f alseof: thread>搜狗拼音半:使用 thread, interrupted ()方法判斷 中斷狀態(tài)可以使用thread, interrupted ()方法來檢杳當前線程的中斷狀態(tài)(并隱式重 置為false)。又由于它是靜態(tài)方法,因此不能在特定的線程上使用,而只能報 告調用它的線程的中斷狀態(tài),如果線程被中斷,而口中斷狀態(tài)尚不清楚,那么, 這個方法返冋tr

15、ue。與islnternjpted()不同,它將自動重置屮斷狀態(tài)為false, 第二次調用thread, interrupted ()方法,總是返回false,除非中斷了線程。如下代碼演示了 thread, interrupted ()方法的使用:public class interruptreset extends object public static void main(string args) system, out. printin(z,point x: thread, interrupted()二 +thread intcrruptcdo);thread currentthrea

16、d() interrupt();system, out printin(point y: thread, interrupted()二 +thread, interrupted();system. out printin("point z: thread, interrupted()=" +thread, interrupted ();運行結杲如i : c:tikd0tssysteb32cbd. exef:thread>jauac interruptreset.jauaf:thread>jaua interruptreset point x : thread, interniptedo =f alse point v: terrupted<> =true po int z: thread. interniptedo =f alsef:thread>搜狗拼音半:從結果中可以看岀,當前線程中斷自身后,在y點,中斷狀態(tài)為true,并由 thread, i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論