java基礎(chǔ)教程-第9章.ppt_第1頁(yè)
java基礎(chǔ)教程-第9章.ppt_第2頁(yè)
java基礎(chǔ)教程-第9章.ppt_第3頁(yè)
java基礎(chǔ)教程-第9章.ppt_第4頁(yè)
java基礎(chǔ)教程-第9章.ppt_第5頁(yè)
已閱讀5頁(yè),還剩41頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第9章Java的多線程機(jī)制,牙齒章節(jié)主要介紹線程的概念、線程的創(chuàng)建和線程的管理。示例9.1類使用Thread的子類創(chuàng)建線程class calculate Thread extends Thread private string name。public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name線程表示先睡1s(秒),然后計(jì)算圓周率的平方根。try thread . sleep(1000);catch(interrupted except

2、ion ie)system . out . print ln(圓周率的平方根是Math.sqrt(Math .PI);System.out.println(name線程說:你好!);class print thread extends thread private string name;public print thread(string name)super(name);T=name,public void run()system . out . print ln(name線程說:我立即打印10顆星星!);for(int I=1);I=10I)system . out .

3、print(*);system . out . print ln();System.out.println(name線程說:你好!);classthread test public static void main(string args)calculate thread t1=new calculate thread(計(jì)算);PrintThread t2=new PrintThread(打印);System.out.println(開始計(jì)算線程t1);t1 . start();System.out.println(啟動(dòng)打印線程T2);T2 . start();System.out.print

4、ln(主要方法是:再見!);牙齒程序的輸出如下:計(jì)算線程開始t1打印線程開始T2 main方法3360再見!計(jì)算線程先睡1s(秒),計(jì)算出圓周率的平方根打印線程。我馬上打印10顆星星!* * * * * * * * * * * * *打印線程說:再見!圓周率的平方根是:1.7724538509055159計(jì)算線程是“再見!Java語(yǔ)言支持多線程,允許在一個(gè)Java節(jié)目中同時(shí)執(zhí)行多個(gè)線程,從而使使用Java程序的用戶減少等待時(shí)間并提高工作效率。線程是執(zhí)行體,牙齒執(zhí)行體的命令流只有一個(gè)起點(diǎn)和一個(gè)終點(diǎn),在執(zhí)行的特定時(shí)間點(diǎn)只有一個(gè)執(zhí)行點(diǎn)。(David aser,Northern Exposure(美國(guó)

5、電視電視劇),run)線程不能單獨(dú)存在,它可能是程序的一部分。您可以在一個(gè)程序中使用多個(gè)線程。這些線程可以同時(shí)運(yùn)行,每個(gè)線程徐璐執(zhí)行其他任務(wù)。線程之間可以共享電腦系統(tǒng)分配給程序的資源(分配給程序的內(nèi)存、外部設(shè)備權(quán)限等)。線程之間可以使用共享內(nèi)存空間進(jìn)行信息更換(線程之間的通信)。在Java中,線程是活動(dòng)對(duì)象?;顒?dòng)對(duì)象不僅可以接收消息,還可以向其他對(duì)象發(fā)送消息。聲明Thread的子類。在子類中重建方法run,然后聲明子類的配置方法。然后創(chuàng)建子類中的對(duì)象。public viod start()throws illegalthreadstateexception牙齒啟動(dòng)當(dāng)前線程時(shí),將立即返回方法。線

6、程啟動(dòng)后,再次調(diào)用方法start將引發(fā)IllegalThreadStateException異常。如果線程啟動(dòng)后獲得CPU許可證,則調(diào)用牙齒線程的run方法。,示例實(shí)施9.2介面Runnable以生成線程class calculate class implements run nable private string name;public calculate class(string name)this . name=name;public void run()system . out . print ln(name線程表示先睡1s(秒),然后計(jì)算圓周率的平方根。try thread . s

7、leep(1000);catch(interrupted exception ie)system . out . print ln(圓周率的平方根是Math.sqrt(Math .PI);System.out.println(name線程說:你好!);class print class implements runnable private string name;public print class(string name)this . name=name;public void run()system . out . print ln(name線程說:我立即打印10顆星星!);for(in

8、t I=1);I=10I)system . out . print(*);system . out . print ln();System.out.println(name線程說:你好!);class runnable test public static voidmain(string args)calculate class calculate obj=new calculate class(計(jì)算);print class print obj=new print class(打印);threadt 1=newthread(calculate obj);Thread T2=new Threa

9、d(print obj);System.out.println(開始計(jì)算線程t1);t1 . start();System.out.println(啟動(dòng)打印線程T2);T2 . start();system.out.println(主方法是:再見!);首先,聲明實(shí)現(xiàn)介面Runnable的類以創(chuàng)建活動(dòng)對(duì)象,并將牙齒活動(dòng)對(duì)象用作配置方法的參數(shù)以創(chuàng)建線程對(duì)象。public static void sleep(long millis)throws interrupted exception使當(dāng)前線程放棄millis毫秒運(yùn)行(睡眠)。如果當(dāng)前線程在睡眠期間被另一個(gè)線程中斷,則睡眠過程將終止,并拋出Int

10、erruptedException異常。Public Thread(Runnable runObj)創(chuàng)建新線程。牙齒線程使用活動(dòng)對(duì)象runObj的run方法。新線程的名稱由系統(tǒng)生成。示例9.2線程狀態(tài)控制class calculate thread extends thread private string name;public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name線程表示先睡1s(秒),然后計(jì)算圓周率的平方根。try thre

11、ad . sleep(1000);catch(interrupted exception ie)system . out . print ln(圓周率的平方根是Math.sqrt(Math .PI);System.out.println(name線程說:你好!);class print thread extends thread private string name;public print thread(string name)super(name);T=name,class runnable test public static voidmain(string args)

12、calculate class calculate obj=new calculate class(計(jì)算);print class print obj=new print class(打印);threadt 1=newthread(calculate obj);Thread T2=new Thread(print obj);System.out.println(開始計(jì)算線程t1);t1 . start();System.out.println(啟動(dòng)打印線程T2);T2 . start();system.out.println(主方法是:再見!);首先,聲明實(shí)現(xiàn)介面Runnable的類以創(chuàng)建活動(dòng)

13、對(duì)象,并將牙齒活動(dòng)對(duì)象用作配置方法的參數(shù)以創(chuàng)建線程對(duì)象。public static void sleep(long millis)throws interrupted exception使當(dāng)前線程放棄millis毫秒運(yùn)行(睡眠)。如果當(dāng)前線程在睡眠期間被另一個(gè)線程中斷,則睡眠過程將終止,并拋出InterruptedException異常。Public Thread(Runnable runObj)創(chuàng)建新線程。牙齒線程使用活動(dòng)對(duì)象runObj的run方法。新線程的名稱由系統(tǒng)生成。,示例9.3線程狀態(tài)控制class calculate thread extends thread private s

14、tring name;public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name線程表示先睡1s(秒),然后計(jì)算圓周率的平方根。try thread . sleep(1000);catch(interrupted exception ie)system . out . print ln(圓周率的平方根是Math.sqrt(Math .PI);System.out.println(name線程說:你好!);class print threa

15、d extends thread private string name;public print thread(string name)super(name);T=name,public void run()system . out . print ln(name線程說:我立即打印10顆星星!);for(int I=1);I=10I)system . out . print(*);system . out . print ln();try suspend();安全執(zhí)行se(catch)system . out . print ln(name線程說:你好!);classthre

16、adstate public static void main(string args)calculate threadt 1=new calculate thread(計(jì)算);PrintThread t2=new PrintThread(打印);System.out.println(開始計(jì)算線程t1);t1 . start();System.out.println(啟動(dòng)打印線程T2);T2 . start();try t1 . join();catch(interrupted exception ie)try T2 . resume();安全執(zhí)行se(catch)try T2 . join(

17、);catch(interrupted exception ie)if(!t1 . is alive()system . out . print ln(結(jié)束計(jì)算線程);If(!T2 . is alive()system . out . print ln(終止打印線程);System.out.println(主要方法是:再見!);牙齒程序的輸出如下:?jiǎn)?dòng)計(jì)算線程以啟動(dòng)打印線程計(jì)算行。先睡4s(秒),然后計(jì)算圓周率的平方根打印線。馬上打印10顆星星!* * * * * * * * * * *圓周率的平方根是1.772 453 850 905 515 9計(jì)算線程。再見!印刷線程說:你好!計(jì)算線程結(jié)束

18、。打印線程結(jié)束。main方法結(jié)束。再見。使用IsAlive,您可以驗(yàn)證線程是否已啟動(dòng)而未進(jìn)入關(guān)閉狀態(tài)。門t1 . join();等待計(jì)算線程結(jié)束語(yǔ)句t2.resume(),然后打印線程語(yǔ)句suspend();暫停當(dāng)前線程,使其進(jìn)入阻塞狀態(tài)。另一個(gè)線程調(diào)用當(dāng)前線程的方法resume(),然后喚醒當(dāng)前線程并使其進(jìn)入就緒狀態(tài)。處于阻塞狀態(tài)的線程可以使用stop()方法終止。示例9.4具有相同優(yōu)先級(jí)的線程class same priority public static void main(string args)new demoa(長(zhǎng)江)。start();新示范(黃河)。啟動(dòng)();class demoa extends thread private string name;public DemoA(String s)super(s);this . name=s;public void run()for(int I=0;I=4;I)system . out . print

溫馨提示

  • 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. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論