
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、淺析java中線程組(threadgroup類)淺析java中線程組(threadgroup類)java中用法threadgroup類來代表線程組,表示一組線程的集合,可以對一批線程和線程組舉行管理??梢园丫€程歸屬到某一個線程組中,線程組中可以有線程對象,也可以有線程組,組中還可以有線程,這樣的組織結(jié)構(gòu)有點(diǎn)類似于樹的形式,所示。 用戶創(chuàng)建的全部線程都屬于指定線程組,假如沒有顯式指定屬于哪個線程組,那么該線程就屬于默認(rèn)線程組(即main線程組)。默認(rèn)狀況下,子線程和父線程處于同一個線程組。此外,惟獨(dú)在創(chuàng)建線程時才干指定其所在的線程組,線程運(yùn)行中途不能轉(zhuǎn)變它所屬的線程組,也就是說線程一旦指定所在的
2、線程組就不能轉(zhuǎn)變。二.為什么要用法線程組1.平安同一個線程組的線程是可以互相修改對方的數(shù)據(jù)的。但假如在不同的線程組中,那么就不能跨線程組修改數(shù)據(jù),可以從一定程度上保證數(shù)據(jù)平安。2.批量管理可以批量管理線程或線程組對象,有效地對線程或線程組對象舉行組織或控制。三.線程組用法示例1.線程關(guān)聯(lián)線程組:一級關(guān)聯(lián)所謂一級關(guān)聯(lián)就是父對象中有子對象,但并不創(chuàng)建孫對象。比如創(chuàng)建一個線程組,然后將創(chuàng)建的線程歸屬到該組中,從而對這些線程舉行有效的管理。代碼示例如下:public class threadgrouptest public static void main(string args) threadgro
3、up rootthreadgroup = new threadgroup("root線程組"); thread thread0 = new thread(rootthreadgroup, new mrunnable(), "線程a"); thread thread1 = new thread(rootthreadgroup, new mrunnable(), "線程b"); thread0.start(); thread1.start(); class mrunnable implements runnable override pu
4、blic void run() while (!thread.currentthread().isinterrupted() system.out.println("線程名: " + thread.currentthread().getname() + ", 所在線程組: " + thread.currentthread().getthreadgroup().getname() ; try thread.sleep(1000); catch (interruptedexception e) e.printstacktrace(); 復(fù)制代碼執(zhí)行結(jié)果如下:
5、線程名: 線程a, 所在線程組: root線程組線程名: 線程b, 所在線程組: root線程組復(fù)制代碼2.線程關(guān)聯(lián)線程組:多級關(guān)聯(lián)所謂的多級關(guān)聯(lián)就是父對象中有子對象,子對象中再創(chuàng)建孫對象也就浮現(xiàn)了子孫的效果了。比如用法下圖其次個構(gòu)造辦法,將子線程組歸屬到某個線程組,再將創(chuàng)建的線程歸屬到子線程組,這樣就會有線程樹的效果了。 代碼示例如下:public class threadgrouptest public static void main(string args) threadgroup rootthreadgroup = new threadgroup("root線程組"
6、;); thread thread0 = new thread(rootthreadgroup, new mrunnable(), "線程a"); thread thread1 = new thread(rootthreadgroup, new mrunnable(), "線程b"); thread0.start(); thread1.start(); threadgroup threadgroup1 = new threadgroup(rootthreadgroup, "子線程組"); thread thread2 = new th
7、read(threadgroup1, new mrunnable(), "線程c"); thread thread3 = new thread(threadgroup1, new mrunnable(), "線程d"); thread2.start(); thread3.start(); class mrunnable implements runnable override public void run() while (!thread.currentthread().isinterrupted() system.out.println("
8、線程名: " + thread.currentthread().getname() + ", 所在線程組: " + thread.currentthread().getthreadgroup().getname() + ", 父線程組: " + thread.currentthread().getthreadgroup().getparent().getname(); try thread.sleep(1000); catch (interruptedexception e) e.printstacktrace(); 復(fù)制代碼執(zhí)行結(jié)果如下:線程
9、名: 線程a, 所在線程組: root線程組, 父線程組: main線程名: 線程b, 所在線程組: root線程組, 父線程組: main線程名: 線程c, 所在線程組: 子線程組, 父線程組: root線程組線程名: 線程d, 所在線程組: 子線程組, 父線程組: root線程組復(fù)制代碼3.批量管理組內(nèi)線程用法線程組自然是要對線程舉行批量管理,比如可以批量中斷組內(nèi)線程,代碼示例如下:public class threadgrouptest public static void main(string args) threadgroup rootthreadgroup = new threa
10、dgroup("root線程組"); thread thread0 = new thread(rootthreadgroup, new mrunnable(), "線程a"); thread thread1 = new thread(rootthreadgroup, new mrunnable(), "線程b"); thread0.start(); thread1.start(); threadgroup threadgroup1 = new threadgroup(rootthreadgroup, "子線程組")
11、; thread thread2 = new thread(threadgroup1, new mrunnable(), "線程c"); thread thread3 = new thread(threadgroup1, new mrunnable(), "線程d"); thread2.start(); thread3.start(); rootthreadgerrupt(); system.out.println("批量中斷組內(nèi)線程"); class mrunnable implements runnable ove
12、rride public void run() while (!thread.currentthread().isinterrupted() system.out.println("線程名: " + thread.currentthread().getname() + ", 所在線程組: " + thread.currentthread().getthreadgroup().getname() + ", 父線程組: " + thread.currentthread().getthreadgroup().getparent().getname(); try thread.sleep(1000); catch (interruptedexception e) e.printstacktrace(); break; system.out.println(thread.currentthread().getname() + "執(zhí)行結(jié)束"); 復(fù)制代碼執(zhí)行結(jié)果如
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 合伙人合作協(xié)議書合同
- 加強(qiáng)采購流程管理的工作總結(jié)計(jì)劃
- 黃金質(zhì)押合同
- 家具品牌故事傳播與品牌形象塑造考核試卷
- 太陽能器具在戶外電影慶典活動的現(xiàn)場供電考核試卷
- 玻璃光學(xué)精密加工考核試卷
- 砼結(jié)構(gòu)構(gòu)件的保溫隔熱性能考核試卷
- 禮儀用品企業(yè)團(tuán)隊(duì)建設(shè)考核試卷
- 消費(fèi)金融公司的客戶滿意度調(diào)查考核試卷
- 石棉制品在電氣設(shè)備絕緣維護(hù)考核試卷
- 2025-2030中國團(tuán)餐行業(yè)市場發(fā)展現(xiàn)狀分析及發(fā)展前景與投資機(jī)會研究報(bào)告
- IT系統(tǒng)架構(gòu)規(guī)劃與設(shè)計(jì)手冊
- 檔案檔案管理基礎(chǔ)知識試題及答案
- 2025-2030中國金紅石發(fā)展現(xiàn)狀及未來趨勢研究報(bào)告
- 2025-2030中國慢性腰痛治療行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報(bào)告
- 演出經(jīng)紀(jì)人與文化經(jīng)濟(jì)試題
- pcb抄板合同范例
- 藥浴療法的基本原理操作規(guī)程及臨床應(yīng)用
- 2025年吉林工業(yè)職業(yè)技術(shù)學(xué)院單招職業(yè)傾向性測試題庫完整
- 生態(tài)農(nóng)業(yè)發(fā)展與綠色金融的融合路徑
- 奶茶店應(yīng)聘簡歷范本
評論
0/150
提交評論