Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod



《Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod》由會(huì)員分享,可在線閱讀,更多相關(guān)《Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod(20頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、 第9章多多線程與與Appplett //例程程9-11:Pii.jaava /*演示示采用多多線程技技術(shù)計(jì)算算圓周率率*/ publlic claass Pi{{ publlic staaticc vooid maiin(SStriing[[] aargss){ PiCaacullatoor pc = nnew PiCCacuulattor((); Threead t == neew TThreead((pc)); t.sttartt();; try{{ Threead..sleeep (1000000); //休眠眠,等待待可能出出現(xiàn)的異異常情況況
2、t.innterrruppt ();; }cattch((IntterrrupttedEExceeptiion e){{ e.prrinttStaackTTracce()); } } } classs PPiCaacullatoor iimpllemeentss Ruunnaablee{ privvatee douublee laatesstPiiEsttimaate;; publlic voiid rrun((){ try{{ Systtem..outt.prrintt (""Matth.PPI == "++ Matth.PPI + ""\t"" );; calPP
3、i(00.0000011); Systtem..outt.prrinttln ("tthe lattestt PI = ""+thhis..lattesttPiEEstiimatte )); }cattch((IntterrrupttedEExceeptiion e){{ Systtem..outt.prrinttln(("Thhe cacculaatorr iss Innterrrupptedd.")); } } /***用于計(jì)計(jì)算圓周周率的方方法,aaccuuraccy為計(jì)計(jì)算精度度*/ privvatee voiid ccalPPi(ddoubble acccu
4、raacy)) thhrowws IInteerruupteedExxcepptioon { thiss.laatesstPiiEsttimaate =0..0; longg itteraatioon = 00; int siggn == -11; //按給給定精度度計(jì)算圓圓周率 whille(MMathh.abbs ((Matth.PPI-tthiss.laatesstPiiEsttimaate))>acccurracyy){ if(TThreead..intterrruptted ())) throow nnew IntterrrupttedEExceeptiio
5、n((); iterratiion+++; signn = -siign;; thiss.laatesstPiiEsttimaate += (siign**4.00/(22*itteraatioon-11));; } } } //例程程9-22:SyynDeemo..javva /*演示示沒有進(jìn)進(jìn)行線程程同步所所帶來的的問題**/ publliccclasssSyynDeemo{{ publlic staaticc vooid maiin(SStriing[[] aargss){ Demoostrratoor ssharreDeemosstraatorr =
6、neww Deemosstraatorr();; Threead t1 = nnew Thrreadd(shhareeDemmosttrattor,,"t11");; Threead t2 = nnew Thrreadd(shhareeDemmosttrattor,,"t22");; t1.sstarrt()); t2.sstarrt()); } } classs DDemoostrratoor iimpllemeentss Ruunnaablee{ privvatee innt ssharreDaata = 00; publlic voiid rrun(()
7、{ Threead t == Thhreaad.ccurrrenttThrreadd ()); for((intt i = 11; ii <== 5;; i+++){{ int coppy == shhareeDatta; try{{ Threead..sleeep ((iint))(Maath..ranndomm ())*10000))); }cattch((Exccepttionn e)){ e.prrinttStaackTTracce()); } Systtem..outt.prrinttln ("TThreead "+tt.geetNaame ()++"
8、: coppy=""+coopy +"\\tshhareeDatta=""+shhareeDatta);; sharreDaata+++; } } } //例程程9-33:DeeadLLockkDemmo.jjavaa publlic claass DeaadLoockDDemoo{ publlic staaticc vooid maiin(SStriing[[] aargss){ DemooObjjectt a = nnew DemmoObbjecct()); DemooObjjectt b = nnew DemmoObbjecct())
9、; a.annothher = bb; b.annothher = aa; Threead t1 = nnew Thrreadd(a,,"t11");; Threead t2 = nnew Thrreadd(b,,"t22");; t1.sstarrt ((); t2.sstarrt ((); } } classs DDemooObjjectt immpleemennts Runnnabble{{ publlic DemmoObbjecct aanottherr = nulll; publlic voiid rrun((){ thiss.meet
10、hood ((); } publlic synnchrroniizedd vooid metthodd(){{ if(tthiss.annothher != nulll){{ try{{ Threead..sleeep (10000)); }cattch((Exccepttionn e)){ e.prrinttStaackTTracce()); } anottherr.meethood ((); //下面面的代碼碼段實(shí)際際上是執(zhí)執(zhí)行不到到的 Systtem..outt.prrinttln ("IIf yyou cann seee tthiss liine,,
11、no deaadloock happpenned""); } } } //例程程9-44:ThrreeTThreeadDDemoo.jaava /*ThhreeeThrreaddDemmo.jjavaa*/ publlic claass ThrreeTThreeadDDemoo{ puubliic sstattic voiid mmainn(Sttrinng[]] arrgs)){ //創(chuàng)建建新線程程 CCusttomTThreead ct11 = neww CuustoomThhreaad(00); CCusttomTThreead ct
12、22 = neww CuustoomThhreaad(11); ///啟動(dòng)新新線程 cct1..staart ();; cct2..staart ();; //輸出出maiin線程程信息 ffor((intt i = 00; ii < 5; i+++){ Sysstemm.ouut.pprinntlnn("mmainn thhreaad: "+ii); }} SSysttem..outt.prrinttln ("mmainn thhreaad hhas donne!""); } } classs CCus
13、ttomTThreead exttendds TThreead{{ innt iid; puubliic CCusttomTThreead((intt cuustoomThhreaadIDD){ tthiss.idd = cusstommThrreaddID;; } //重定定義子線線程的rrun(()方法 puubliic vvoidd ruun()){ //輸出出自定義義線程的的信息 ffor((intt i = 00; ii < 5; i+++){ Sysstemm.ouut.pprinntlnn (""CusstommT
14、hrreadd #""+ tthiss.idd +"": ""+i)); }} SSysttem..outt.prrinttln ("CCusttomTThreead #"++thiis.iid+"" haas ddonee!")); } } //例程程9-55:DiggitaalCllockk.jaava /*采用用多線程程技術(shù)演演示一個(gè)個(gè)簡(jiǎn)單的的數(shù)字時(shí)時(shí)鐘*// impoort javva.aawt..eveent..*; impoort javva.aawt..*; impoort javvax..swiing..*; publlic cl
15、aass DiggitaalCllockk exxtennds JFrramee{ pubblicc sttatiic vvoidd maain((Strringg[] arggs) { JFFramme fframme == neew DDigiitallCloock((); frramee.shhow((); } pubblicc DiigittalCClocck()){ tthiss.seetSiize((2000,1550);; tthiss.seetDeefauul
16、tCClosseOpperaatioon(JJFraame..EXIIT_OON_CCLOSSE);; ///數(shù)字時(shí)時(shí)鐘面板板 ffinaal CClocckPaane cp = nnew CloockPPanee();; //設(shè)置置按鈕狀狀態(tài)并注注冊(cè)事件件監(jiān)聽者者 ffinaal JJButttonn sttartt = neww JBButtton(("sttartt");; ffinaal JJButttonn sttop = nnew JBuuttoon(""stoop")); sstopp.s
17、eetEnnablled((fallse)); sstarrt.aaddAActiionLListteneer(nnew ActtionnLisstenner((){ puubliic vvoidd acctioonPeerfoormeed(AActiionEEvennt ee){ cpp.sttarttCloock((); sttartt.seetEnnablled((fallse)); sttop..settEnaableed(ttruee);
18、 } }}); sstopp.adddAcctioonLiisteenerr(neew AActiionLListteneer()){ pubblicc vooid acttionnPerrforrmedd(AcctioonEvventt e)){ cpp.sttopCClocck()); sttartt.seetEnnablled((truue);; sttop..settEnaableed(
19、ffalsse);; } }}); //設(shè)設(shè)置面板板布局 JJPannel butttommPanne == neew JJPannel((); bbutttomPPanee.addd(sstarrt);; bbutttomPPanee.addd(sstopp); JJPannel conntenntPaane = nnew JPaanell();; cconttenttPanne.ssetLLayoout((neww BoordeerLaayouut()))
20、; cconttenttPanne.aadd((cp,,BorrderrLayyoutt.CEENTEER);; cconttenttPanne.aadd((butttommPanne,BBordderLLayoout..SOUUTH)); tthiss.seetCoonteentPPanee(coonteentPPanee); tthiss.seetReesizzablle(ffalsse);; } } //例程程9-66:CloockPPanee.jaava /*數(shù)字字時(shí)鐘面面板的實(shí)實(shí)現(xiàn)類**/ impoo
21、rt javvax..swiing..JPaanell; impoort javva.uutill.*;; impoort javva.aawt..*; impoort javva.aawt..fonnt.FFonttRennderrConntexxt; impoort javva.aawt..geoom.**; impoort javva.ttextt.*;; publlic claass CloockPPanee exxtennds JPaanell immpleemennts Runnnabble { //線程程是否中中止的標(biāo)標(biāo)志 booo
22、leaan rrunnningg = fallse;; //用于于顯示當(dāng)當(dāng)前時(shí)間間的字符符串 Strringg tiime = ""Cloock""; Fonnt ffontt = neww Foont(("SaanSeeriff", Fonnt.BBOLDD, 440);; //啟動(dòng)動(dòng)報(bào)時(shí)器器 pubblicc vooid staartCClocck()) { thhis..runnninng == trrue;; Thhreaad tt = neww Thhreaad(tthiss);
23、 t..staart((); } //終止止報(bào)時(shí)器器 pubblicc vooid stoopCllockk() { thhis..runnninng == faalsee; } //實(shí)現(xiàn)現(xiàn)Runnnabble接接口的rrun()方方法 pubblicc vooid runn() { whhilee (tthiss.ruunniing)) { //獲取取當(dāng)前時(shí)時(shí)間并轉(zhuǎn)轉(zhuǎn)換成字字符串 tthiss.tiime = DDateeForrmatt.g
24、eetTiimeIInsttancce()).foormaat(nnew Datte())); tthiss.reepaiint((); //讓當(dāng)當(dāng)前線程程休眠11秒鐘 ttry { Thrreadd.slleepp(10000)); }} caatchh (IInteerruupteedExxcepptioon ee) {{ e.pprinntSttackkTraace(();
25、 }} } } //輸出出當(dāng)前時(shí)時(shí)間 pubblicc vooid paiintCCompponeent((Graaphiics g) { suuperr.paainttCommponnentt(g)); Grraphhicss2D g2 = ((Graaphiics22D) g; g22.seetFoont((thiis.ffontt); FoontRRendderCConttextt coonteex == g22.geetFoontRRe
26、ndderCConttextt();; Reectaanglle2DD booundd = thiis.ffontt.geetSttrinngBooundds(tthiss.tiime,, coonteex);; innt sstrXX = (innt) ((tthiss.geetWiidthh() - bbounnd.ggetWWidtth())) // 2)); innt sstrYY = (innt) ((tthiss.geetHeeighht()) - bouund..gettHeiightt())) /
27、 2) + 440; g22.drrawSStriing((thiis.ttimee, sstrXX, sstrYY); } } //例程程9-77:IOOPippeDeemo..javva /*演示示采用管管道機(jī)制制的線程程間通信信*/ impoort javva.iio.**; publlic claass IOPPipeeDemmo{ publlic staaticc vooid maiin(SStriing[[] aargss){ try{{ //創(chuàng)建建并連接接管道 finaal PPipeedOuutpuutSttrea
28、am ppoutt = neww PiipeddOuttputtStrreamm();; finaal PPipeedInnputtStrreamm piin == neew PPipeedInnputtStrreamm(poout)); //創(chuàng)建建并啟動(dòng)動(dòng)輸出線線程 Threead outtputtThrreadd = neww Thhreaad(nnew Runnnabble((){ publlic voiid rrun((){ writteByytess(poout)); } }); outpputTThreead..staart((); //創(chuàng)建建并啟動(dòng)動(dòng)輸
29、入線線程 Threead inpputTThreead = nnew Thrreadd(neew RRunnnablle()){ publlic voiid rrun((){ readdByttes((pinn); } }); inpuutThhreaad.sstarrt()); }cattch((IOEExceeptiion e){{ e.prrinttStaackTTracce()); } } //往管管道中寫寫入數(shù)據(jù)據(jù) publlic staaticc vooid wriiteBBytees(OOutpputSStreeam outtstrream
30、m){ try{{ DataaOuttputtStrreamm ouut == neew DDataaOuttputtStrreamm( new BuffferredOOutpputSStreeam((outtstrreamm) ); Threead t == Thhreaad.ccurrrenttThrreadd ()); for((intt i==0; i<110; i+++){ Systtem..outt.prrinttln ("wwritte iinteegerr "++i+"" too piipe..");; out..wriiteIInt(
31、(i);; t.yiieldd ()); } out..fluush((); out..cloose((); Systtem..outt.prrinttln ("WWritte ddataa too piipe hass doone""); }cattch((IOEExceeptiion e){{ e.prrinttStaackTTracce()); } } //從管管道中讀讀取數(shù)據(jù)據(jù) publlic staaticc vooid reaadByytess(InnputtStrreamm innputtstrreamm){ try{{ DataaInpput
32、SStreeam in = nnew DattaInnputtStrreamm( new BuffferredIInpuutSttreaam(iinpuutsttreaam) ); Threead t == Thhreaad.ccurrrenttThrreadd ()); boolleann eoof == faalsee; whille(!!eoff){ try{{ int i == inn.reeadIInt((); Systtem..outt.prrinttln(("Reead inttegeer ""+i++" ffromm piipe"
33、"); t.yiieldd ()); }cattch((EOFFExccepttionn e)){ eof = ttruee; } } Systtem..outt.prrinttln ("RReadd daata froom ppipee haas ddonee");; }cattch((IOEExceeptiion e){{ e.prrinttStaackTTracce()); } } } //例程程9-8 Heelloo_Worrld..javva impoort javvax..swiing..*; impoort javva.aawt..*;
34、publlic claass Helllo__worrld exttendds JJAppplett{
publlic voiid ppainnt(GGrapphiccs gg){
g.drrawSStriing(("Heelloo,woorldd!",5,10));
}
}
HellloWoorldd.httml 代碼
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 專題黨課講稿:以高質(zhì)量黨建保障國(guó)有企業(yè)高質(zhì)量發(fā)展
- 廉政黨課講稿材料:堅(jiān)決打好反腐敗斗爭(zhēng)攻堅(jiān)戰(zhàn)持久戰(zhàn)總體戰(zhàn)涵養(yǎng)風(fēng)清氣正的政治生態(tài)
- 在新錄用選調(diào)生公務(wù)員座談會(huì)上和基層單位調(diào)研座談會(huì)上的發(fā)言材料
- 總工會(huì)關(guān)于2025年維護(hù)勞動(dòng)領(lǐng)域政治安全的工作匯報(bào)材料
- 基層黨建工作交流研討會(huì)上的講話發(fā)言材料
- 糧食和物資儲(chǔ)備學(xué)習(xí)教育工作部署會(huì)上的講話發(fā)言材料
- 市工業(yè)園區(qū)、市直機(jī)關(guān)單位、市紀(jì)委監(jiān)委2025年工作計(jì)劃
- 檢察院政治部關(guān)于2025年工作計(jì)劃
- 辦公室主任2025年現(xiàn)實(shí)表現(xiàn)材料
- 2025年~村農(nóng)村保潔員規(guī)范管理工作方案
- 在深入貫徹中央8項(xiàng)規(guī)定精神學(xué)習(xí)教育工作部署會(huì)議上的講話發(fā)言材料4篇
- 開展深入貫徹規(guī)定精神學(xué)習(xí)教育動(dòng)員部署會(huì)上的講話發(fā)言材料3篇
- 在司法黨組中心學(xué)習(xí)組學(xué)習(xí)會(huì)上的發(fā)言材料
- 國(guó)企黨委關(guān)于推動(dòng)基層黨建與生產(chǎn)經(jīng)營(yíng)深度融合工作情況的報(bào)告材料
- 副書記在2025年工作務(wù)虛會(huì)上的發(fā)言材料2篇
相關(guān)資源
更多