日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java中主线程如何捕获子线程抛出 ...

發(fā)布時(shí)間:2025/3/16 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中主线程如何捕获子线程抛出 ... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這么來看待這個(gè)問題。首先明確線程代碼的邊界。其實(shí)很簡單,Runnable接口的run方法所界定的邊界就可以看作是線程代碼的邊界。Runnable接口中run方法原型如下: ?
? << ?
????????? public?? void?? run(); ?
? >> ?
? 而所有的具體線程都實(shí)現(xiàn)這個(gè)方法,所以這里就明確了一點(diǎn),線程代碼不能拋出任何checked異常。所有的線程中的checked異常都只能被線程本身消化掉。:)?? 這樣本身也是符合線程的設(shè)計(jì)理念的,線程本身就是被看作獨(dú)立的執(zhí)行片斷,它應(yīng)該對(duì)自己負(fù)責(zé),所以由它來消化所有的checked異常是很正常的。 ?
? 這樣就回答了樓主的第一個(gè)問題:checked異常一定要在線程內(nèi)部消化。 ?
? ?
? 但是,線程代碼中是可以拋出錯(cuò)誤(Error)和運(yùn)行級(jí)別異常(RuntimeException)的。Error俺們可以忽略,因?yàn)橥ǔrror是應(yīng)該留給vm的,而RuntimeException確是比較正常的,如果在運(yùn)行過程中滿足了某種條件導(dǎo)致線程必須中斷,可以選擇使用拋出運(yùn)行級(jí)別異常來處理,如下: ?
? << ?
????????? public?? void?? run()?? { ?
??????????????? if?? (...)?? throw?? new?? RuntimeException(); ?
????????? } ?
? >> ?
? 當(dāng)線程代碼拋出運(yùn)行級(jí)別異常之后,線程會(huì)中斷。:)這點(diǎn)java中解釋得很清楚: ?
? <<?? @see?? Thread ?
? All?? threads?? that?? are?? not?? daemon?? threads?? have?? died,?? either?? by?? returning?? from?? the?? call?? to?? the?? run?? method?? or?? "by?? throwing?? an?? exception?? that?? propagates?? beyond?? the?? run?? method".?? ?
? >> ?
? 但是對(duì)于invoke此線程的主線程會(huì)產(chǎn)生什么影響呢?主線程不受這個(gè)影響,不會(huì)處理這個(gè)RuntimeException,而且根本不能catch到這個(gè)異常。會(huì)繼續(xù)執(zhí)行自己的代碼?? ?
? 所以得到結(jié)論:線程方法的異常只能自己來處理。 ?
? ?
? 關(guān)于最后一點(diǎn),不相信的話大家可以做這么一個(gè)試驗(yàn): ?
? << ?
? public?? class?? TestThreadException?? extends?? Thread?? { ?
????????? public?? void?? run()?? { ?
????????????????? throw?? new?? RuntimeException(); ?
????????? } ?
? ?
????????? public?? static?? void?? main(String[]?? args)?? throws?? InterruptedException?? { ?
????????????????? try?? { ?
????????????????????????? new?? TestThreadException().start(); ?
????????????????? }?? catch(RuntimeException?? ex)?? { ?
????????????????????????? //?? 看看能不能到達(dá)這里??? ?
????????????????? } ?
????????????????? ?
????????????????? Thread.sleep(1000); ?
????????????????? //?? 看看能不能到達(dá)這里??? ?
????????? } ?
? } ?
? >> ?


記不得在哪里看到的代碼,可以處理到線程中拋出的RuntimeException: ?
? ?
? public?? class?? ApplicationLoader?? extends?? ThreadGroup ?
? { ?
????????? private?? ApplicationLoader() ?
????????? { ?
? ?
????????????????? super("ApplicationLoader"); ?
? ?
????????? } ?
? ?
????????? public?? static?? void?? main(String[]?? args) ?
????????? { ?
? ?
????????????????? Runnable?? appStarter?? =?? new?? Runnable() ?
????????????????? { ?
? ?
????????????????????????? public?? void?? run() ?
????????????????????????? { ?
????????????????????????????????? //invoke?? your?? application?? (i.e.MySystem.main(args) ?
? ?
????????????????????????????????? throw?? new?? NullPointerException();?? //example,?? throw?? a?? runtime?? exception ?
????????????????????????? } ?
????????????????? }; ?
? ?
????????????????? new?? Thread(new?? ApplicationLoader(),?? appStarter).start(); ?
????????? } ?
? ?
????????? //We?? overload?? this?? method?? from?? our?? parent ?
????????? //ThreadGroup?? ,?? which?? will?? make?? sure?? that?? it ?
????????? //gets?? called?? when?? it?? needs?? to?? be.???? This?? is?? ?
????????? //where?? the?? magic?? occurs. ?
????????? public?? void?? uncaughtException(Thread?? thread,?? Throwable?? exception) ?
????????? { ?
????????????????? //Handle?? the?? error/exception. ?
????????????????? //Typical?? operations?? might?? be?? displaying?? a ?
????????????????? //useful?? dialog,?? writing?? to?? an?? event?? log,?? etc. ?
? ?
????????????????? exception.printStackTrace();//example,?? print?? stack?? trace ?
????????? } ?
? } ?


呵呵,uncaughtException好像是唯一能夠處理線程拋出的uncaught異常的入口。看來還是有細(xì)心人啊。確實(shí)如此,通過 ThreadGroup的uncaughtException方法還是有處理的機(jī)會(huì)。當(dāng)線程拋出uncaughtException的時(shí)候,JVM會(huì)調(diào)用 ThreadGroup的此方法。默認(rèn)的處理如下: ?
? << ?
????????? public?? void?? uncaughtException(Thread?? t,?? Throwable?? e)?? { ?
? if?? (parent?? !=?? null)?? { ?
????????? parent.uncaughtException(t,?? e); ?
? }?? else?? if?? (!(e?? instanceof?? ThreadDeath))?? { ?
????????? e.printStackTrace(System.err); ?
? } ?
????????? } ?
? >> ?
? 每個(gè)Thread都會(huì)有一個(gè)ThreadGroup對(duì)象,可以通過Thread.getThreadGroup()方法得到,提供了上述默認(rèn)的uncaught異常處理方法。 ?
? 上面沒有提這點(diǎn),因?yàn)榘痴J(rèn)為在正常的情況下,這個(gè)方法的處理情況就已經(jīng)足夠了。還是那個(gè)線程設(shè)計(jì)的理念:“線程的問題應(yīng)該線程自己本身來解決,而不要委托到外部。”通常情況下,外部不需要處理線程的異常。當(dāng)然也有例外。:)??

總結(jié)

以上是生活随笔為你收集整理的Java中主线程如何捕获子线程抛出 ...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。