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

正文內(nèi)容

java多線程ppt課件-文庫吧資料

2024-10-25 04:51本頁面
  

【正文】 調(diào)用來解決這一問題,但是 Java 為我們提供了更好的解決辦法,那就是 synchronized 塊。這種機(jī)制確保了同一時(shí)刻對(duì)于每一個(gè)類實(shí)例,其所有聲明為 synchronized 的成員函數(shù)中至多只有一個(gè)處于可執(zhí)行狀態(tài)(因?yàn)橹炼嘀挥幸粋€(gè)能夠獲得該類實(shí)例對(duì)應(yīng)的鎖),從而有效避免了類成員變量的訪問沖突(只要所有可能訪問類成員變量的方法均被聲明為 synchronized)。 – 格式為: public synchronized void accessVal(int newVal)。 同步方法 ? 2. 鎖定沖突的方法 – 即在待鎖定方法聲明中加入 synchronized關(guān)鍵字。對(duì)象的 “ 鎖標(biāo)志 ” 和 class的 “ 鎖標(biāo)志 ” 是相互獨(dú)立的。 – 每個(gè) class也有一個(gè) “ 鎖標(biāo)志 ” 。它只表示如果兩個(gè)線程正在等待相同的鎖,則它們不能同時(shí)執(zhí)行該代碼。一個(gè)鎖可以保護(hù)許多代碼塊或方法。從其它線程的角度看,該代碼塊可以看作是原子的,它要么全部執(zhí)行,要么根本不執(zhí)行。當(dāng)對(duì)象的一個(gè)線程訪問了對(duì)象的某個(gè)synchronized數(shù)據(jù)時(shí),這個(gè)對(duì)象就將被 “ 上鎖 ” – 被聲明為 synchronized的數(shù)據(jù)都不能被調(diào)用 – 只有當(dāng)前線程訪問完它要訪問的 synchronized數(shù)據(jù)者拋出了沒有在 synchronized 塊中捕獲的異常時(shí),釋放 “ 鎖標(biāo)志 ” 后,同一個(gè)對(duì)象的其它線程才能訪問 synchronized數(shù)據(jù)。 ? 可以通過 private 關(guān)鍵字來保證數(shù)據(jù)對(duì)象只能被方法訪問 ? synchronized 關(guān)鍵字:鎖定沖突的方法和鎖定沖突的對(duì)象。( )。 27. ( )。 25. Thread t2=new Thread(new rThread(Obj, 2))。 19. } 20.} ? 【 實(shí)例 86】 class ThreadClash { 22. public static void main(String args[ ]) { 23. myThread Obj=new myThread( )。 15. num=n。 12. int num。} 7. (結(jié)束線程 NO: +n)。 4. try{(3)。 23. } 24. } ? 【 實(shí)例 85】 ? 程序輸出結(jié)果: the Thread Name=MaxPriorityThread, Priority=10 the Thread Name=NormPriorityThread, Priority=5 the Thread Name=MinPriorityThread, Priority=1 資源沖突 ? 多個(gè)線程同時(shí)運(yùn)行雖然可以提高程序的執(zhí)行效率,但由于共享一組資源,可能會(huì)產(chǎn)生沖突,例如【 實(shí)例 86】 。 21. ( )。 19. ()。 17. ()。 15. Thread normThread=new myThread(NormPriorityThread )。 10. } 11.} ? 【 實(shí)例 85】 驗(yàn)證了 Java對(duì)多線程的調(diào)度方法。} 5. catch(InterruptedException e) { 6. (())。 1. class myThread extends Thread{ 2. myThread(String str){ super(str)。 ? 如果多個(gè)線程具有相同的優(yōu)先級(jí),它們將被輪流調(diào)度。一般主線程的優(yōu)先級(jí)是普通。優(yōu)先級(jí)別主要分高、中、低三個(gè)級(jí)別,分別代表的數(shù)字是 1。 資源的協(xié)調(diào)與同步 ? 線程調(diào)度模型 ? 資源沖突 ? 同步方法 線程調(diào)度模型 ? 當(dāng)計(jì)算機(jī)中只有一個(gè) CPU時(shí),同一時(shí)刻正在運(yùn)行的線程只能有一個(gè),當(dāng)一個(gè)新的線程通過 new()創(chuàng)建并通過 start()方法啟動(dòng)后,線程只是進(jìn)入就緒狀態(tài),是否能運(yùn)行要看調(diào)度的結(jié)果。 創(chuàng)建多線程的方法 ? 3.兩種方法的簡(jiǎn)單比較 – 使用 Thread方法簡(jiǎn)單明了,符合大家的習(xí)慣,但是,它也有一個(gè)很大的缺點(diǎn),那就是如果我們的類已經(jīng)從一個(gè)類繼承(如小程序必須繼承自 Applet 類),則無法再繼承 Thread 類。 i++) new Thread(new myThread(i+1)).start()。 11. } 12. } 13. } 14. public class runnableThreads{ 15. public static void main(String args[]){ 16. for(int i = 0。 6. } 7. public void run(){ 8. while(true){ 9. (線程 + number + :計(jì)數(shù) + count)。 3. public myThread(int num){ 4. number=num。 – ( 4)通過 start( )方法啟動(dòng)線程。 Runnable接口中有一個(gè)空的方法 run( ),實(shí)現(xiàn)它的類必須覆蓋此方法。 創(chuàng)建多線程的方法 ? 2.通過實(shí)現(xiàn) Runnable接口實(shí)現(xiàn)多線程 – ( 1)定義一個(gè)實(shí)現(xiàn) Runnable接口的類。 ( )。 ( )。 25. t4=new myThread(Thread 4)。 23. t2=new myThread(Thread 2)。 17. } 18.} ? 【 實(shí)例 82】 class fourThreads{ 20. public static void main(String args[ ]) { 21. myThread t1, t2, t3, t4。 12. } 13. catch(InterruptedException e){ 14. (Exception: +())。 6. (The Thread Name=+getName( )+ 7. , Sleeping: +sleeptime)。 3. public myThread(String id) { // 構(gòu)造函數(shù) 4. super(id)。 – ( 4) 通過 start( )方法啟動(dòng)線程。 – ( 2) 定義子類中的方法 run( ),覆蓋父類中的方法run( )。 圖 81 線程生命周期示意圖 start 創(chuàng)建 就緒 運(yùn)行 掛起 睡眠 阻塞 結(jié)束 等待 時(shí)間片結(jié)束 分配時(shí)間片 睡眠時(shí) 間
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1