freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

第八章線程-wenkub

2022-11-04 14:09:27 本頁面
 

【正文】 多線程編程基礎(chǔ) 30 多線程的同步控制 ? 有時(shí)線程之間彼此不獨(dú)立、需要同步 – 線程間的互斥 ? 同時(shí)運(yùn)行的幾個線程需要共享一個(些)數(shù)據(jù) ? 一個線程對共享的數(shù)據(jù)進(jìn)行操作時(shí),不允許其他線程打斷它,否則會破壞數(shù)據(jù)的完整性。 new Thread(t).start()。 } catch ( InterruptedException exception ) {}。 } } 線程間的數(shù)據(jù)共享 (續(xù) ) —— 例 8_5 多線程編程基礎(chǔ) 24 class TestThread implements Runnable { private int sleepTime。 new Thread(threadobj,Thread1).start()。 ( ().getName()+ finished )。 public TestThread() { sleepTime = ( int ) ( () * 6000 )。 new Thread(thread2,Thread2).start()。 TestThread thread2 = new TestThread()。 i=i1。 public FactorialThread( int num ) { =num。 FactorialThread t=new FactorialThread(10)。 } public void run() { try { ( getName() + going to sleep for + sleepTime )。 // 啟動線程 3 ( Threads started, main ends\n )。 ( Starting threads )。 public void interrupt() 打斷此線程 public final void join() 在當(dāng)前線程中加入調(diào)用 join方法的線程 A,直到線程 A死亡才能繼續(xù)執(zhí)行當(dāng)前線程 public final void join(long millis) 在當(dāng)前線程中加入調(diào)用 join方法的線程 A,直到到達(dá)參數(shù)指定毫秒數(shù)或線程 A死亡才能繼續(xù)執(zhí)行當(dāng)前線程 Thread類 (續(xù) ) —— 常用 API函數(shù) 多線程編程基礎(chǔ) 13 public final void setPriority( int newPriority) 設(shè)置線程優(yōu)先級 public final void setDaemon(Boolean on) 設(shè)置是否為后臺線程,如果當(dāng)前運(yùn)行線程均為后臺線程則 JVM停止運(yùn)行。 Thread類 (續(xù) ) —— 例 8_1修改 多線程編程基礎(chǔ) 10 ? 修改后運(yùn)行結(jié)果 main thread starts new thread stared The factorial of 10 is 3628800 new thread ends main thread ends ? 運(yùn)行結(jié)果說明 – 新線程結(jié)束后 main線程才結(jié)束 Thread類 (續(xù) ) —— 例 8_1修改后運(yùn)行結(jié)果 多線程編程基礎(chǔ) 11 Thread類 (續(xù) ) —— 常用 API函數(shù) 名稱 說明 public Thread() 構(gòu)造一個新的線程對象,默認(rèn)名為 Threadn,n是從 0開始遞增的整數(shù) public Thread(Runnable target) 構(gòu)造一個新的線程對象,以一個實(shí)現(xiàn) Runnable接口的類的對象為參數(shù)。 } (The factorial of +num+ is +result)。 int result=1。 (main thread ends )。原線程將同時(shí)繼續(xù)往下執(zhí)行 – Thread類直接繼承了 Object類,并實(shí)現(xiàn)了 Runnable接口。第八章 線程 鄭 莉 JAVA語言程序設(shè)計(jì) 2 目錄 ? 多線程編程基礎(chǔ) ? 線程的生命周期 ? 線程的優(yōu)先級 ? 本章小結(jié) 3 多線程編程基礎(chǔ) ? 本節(jié)內(nèi)容 – 線程的概念 – Thread類 – Runnable接口 – 線程間的數(shù)據(jù)共享 – 多線程的同步控制 – 線程之間的通信 – 后臺線程 4 線程的概念 ? 進(jìn)程和線程的區(qū)別 ? 進(jìn)程 – 一個獨(dú)立程序的每一次運(yùn)行稱為一個進(jìn)程,例如 ? 用字處理軟件編輯文稿時(shí),同時(shí)打開 mp3播放程序聽音樂,這兩個獨(dú)立的程序在同時(shí)運(yùn)行,稱為兩個進(jìn)程 – 設(shè)置一個進(jìn)程要占用相當(dāng)一部分處理器時(shí)間和內(nèi)存資源 – 大多數(shù)操作系統(tǒng)不允許進(jìn)程訪問其他進(jìn)程的內(nèi)存空間,進(jìn)程間的通信很不方便,編程模型比較復(fù)雜 多線程編程基礎(chǔ) 5 ? 線程 – 一個程序中多段代碼同時(shí)并發(fā)執(zhí)行,稱為多線程 – 通過多線程,一個 進(jìn)程 表面上看同時(shí)可以執(zhí)行一個以上的任務(wù) ——并發(fā) – 創(chuàng)建線程比創(chuàng)建進(jìn)程開銷要小得多,線程之間的協(xié)作和數(shù)據(jù)交換也比較容易 – Java是第一個支持內(nèi)置線程操作的主流編程語言 – 多數(shù)程序設(shè)計(jì)語言支持多線程要借助于操作系統(tǒng)“原語(primitives)” 線程的概念 (續(xù) ) 多線程編程基礎(chǔ) 6 Thread類 ? Thread類 – 在 Java程序中創(chuàng)建多線程的方法之一是繼承 Thread類 – 封裝了 Java程序中一個線程對象需要擁有的屬性和方法 – 從 Thread類派生一個子類,并創(chuàng)建這個子類的對象,就可以產(chǎn)生一個新的線程。它位于 ,因而程序開頭不用 import任何包就可直接使用 多線程編程基礎(chǔ) 7 Thread類 (續(xù) ) —— 例 8_1 ? 在新線程中完成計(jì)算某個整數(shù)的階乘 public class Ex8_1 { public static void main( String [] args ) { (main thread starts)。 } } class FactorialThread extends Thread { private int num。 (new thread started )。 (new thread ends)。默認(rèn)名為 Threadn,n是從 0開始遞增的整數(shù) public Thread(String name) 構(gòu)造一個新的線程對象,并同時(shí)指定線程名 public static Thread currentThread() 返回當(dāng)前正在運(yùn)行的線程對象 public static void yield() 使當(dāng)前線程對象暫停,允許別的線程開始運(yùn)行 public static void sleep(long millis) 使當(dāng)前線程暫停運(yùn)行指定毫秒數(shù),但此線程并不失去已獲得的鎖旗標(biāo)。這個方法必須在 start()方法前使用 public final void checkAccess() 判斷當(dāng)前線程是否有權(quán)力修改調(diào)用此方法的線程 public void setName(String name) 更該本線程的名稱為指定參數(shù) public final boolean isAlive() 測試線程是否處于活動狀態(tài),如果線程被啟動并且沒有死亡則返回 true Thread類 (續(xù) ) —— 常用 API函數(shù) 多線程編程基礎(chǔ) 14 ? 創(chuàng)建 3個新線程,每個線程睡眠一段時(shí)間( 0~ 6秒),然后結(jié)束 public class Ex8_2 { public static void main( String [] args ) { //創(chuàng)建并命名每個線程 TestThread thread1 = new TestThread( thread1 )。 ()。 } } Thread類 (續(xù) ) —— 例 8_2 多線程編程基礎(chǔ) 15 class TestThread extends Thread { private int sleepTime。 ( sleepTime )。 new Thread(t).start()。 } public void run() { int i=num。 } (The factorial of +num+ is +result)。 TestThread thread3 = new TestThread()。 new Thread(thread3,Thread3).start()。 } public void run() { try { ( ().getName() + going to sleep for + sleepTime )。 } } Runnable接口 (續(xù) ) —— 例 8_4 多線程編程基礎(chǔ) 22 線程間的數(shù)據(jù)共享 ? 代碼共享 – 多個線程的執(zhí)行代碼來自同一個類的 run方法時(shí),即稱它們共享相同的代碼 ? 數(shù)據(jù)共享 – 當(dāng)共享訪問相同的對象時(shí),即它們共享相同的數(shù)據(jù) – 使用 Runnable接口可以輕松實(shí)現(xiàn)多個線程共享相同數(shù)據(jù),只要用同一個實(shí)現(xiàn)了 Runnable接口的實(shí)例作為參數(shù)創(chuàng)建多個線程就可以了 多線程編程基礎(chǔ) 23 ? 修改例 8_4,只用一個 Runnable類型的對象為參數(shù)創(chuàng)建 3個新線程。 new Thread(threadobj,Thread2).start()。 public TestThread() { sleepTime = ( int ) ( () * 6000 )。 ( ().getName() + finished )。 new Thread(t).start()。即被多個線程共享的數(shù)據(jù)
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1