秋霞电影网午夜鲁丝片无码,真人h视频免费观看视频,囯产av无码片毛片一级,免费夜色私人影院在线观看,亚洲美女综合香蕉片,亚洲aⅴ天堂av在线电影猫咪,日韩三级片网址入口

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

上傳人:muj****520 文檔編號(hào):156539464 上傳時(shí)間:2022-09-27 格式:DOCX 頁(yè)數(shù):20 大小:36.22KB
收藏 版權(quán)申訴 舉報(bào) 下載
Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod_第1頁(yè)
第1頁(yè) / 共20頁(yè)
Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod_第2頁(yè)
第2頁(yè) / 共20頁(yè)
Java程序設(shè)計(jì)教程 冶金工業(yè)出版社第9章pod_第3頁(yè)
第3頁(yè) / 共20頁(yè)

下載文檔到電腦,查找使用更方便

20 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《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 代碼 <>HELLLO,,WORRLD!!<> > > >

展開閱讀全文
溫馨提示:
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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!