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

正文內(nèi)容

[理學(xué)]jaa2實用教程課件線程-資料下載頁

2025-01-19 14:42本頁面
  

【正文】 00 Thread 1, tick = 250000 Thread 1, tick = 300000 Thread 1, tick = 350000 Thread 1, tick = 400000 Thread 0, tick = 50000 Thread 0, tick = 100000 Thread 0, tick = 150000 Thread 0, tick = 202200 Thread 0, tick = 250000 Thread 0, tick = 300000 Thread 0, tick = 350000 Thread 0, tick = 400000 ? 結(jié)果說明 – 具有較高優(yōu)先級的線程1一直運行到結(jié)束,具有較低優(yōu)先級的線程 0才開始運行 – 雖然具有較高優(yōu)先級的線程 1調(diào)用了 yield方法放棄 CPU資源,允許線程 0進行爭奪,但馬上又被線程 1搶奪了回去,所以有沒有 yield方法都沒什么區(qū)別 ??? 52 ? 如果在 yield方法后增加一行 sleep語句,讓線程 1暫時放棄一下在 CPU上的運行,哪怕是 1毫秒,則線程0也可以有機會被調(diào)度。修改后的 run方法如下 public void run() { while (tick 400000) { tick++。 if ((tick % 50000) == 0) { (Thread + num + , tick = + tick)。 yield()。 try{ sleep(1)。}catch(Exception e){}。 } } } 線程的優(yōu)先級 (續(xù) ) —— 例 8_13修改 53 線程的優(yōu)先級 (續(xù) ) —— 例 8_13修改后運行結(jié)果 ? 運行結(jié)果 Thread 1, tick = 50000 Thread 1, tick = 100000 Thread 1, tick = 150000 Thread 1, tick = 202200 Thread 0, tick = 50000 Thread 1, tick = 250000 Thread 1, tick = 300000 Thread 0, tick = 100000 Thread 1, tick = 350000 Thread 1, tick = 400000 Thread 0, tick = 150000 Thread 0, tick = 202200 Thread 0, tick = 250000 Thread 0, tick = 300000 Thread 0, tick = 350000 Thread 0, tick = 400000 ? 說明 – 具有較低優(yōu)先權(quán)的線程 0在線程 1沒有執(zhí)行完畢前也獲得了一部分執(zhí)行,但線程 1還是優(yōu)先完成了執(zhí)行 – 通常,我們在一個線程內(nèi)部插入 yield()語句,這個方法會使正在運行的線程暫時放棄執(zhí)行,這是具有同樣優(yōu)先級的線程就有機會獲得調(diào)度開始運行,但較低優(yōu)先級的線程仍將被忽略不參加調(diào)度 54 本章小結(jié) ? 本章內(nèi)容 – 線程的基礎(chǔ)知識 – 線程的生命周期 – 線程的優(yōu)先級 ? 本章要求 – 了解線程的概念 – 學(xué)會如何通過 Thread類和 Runnable接口創(chuàng)建線程,如何實現(xiàn)多線程的資源共享和通信,及如何控制線程的生命 – 掌握線程同步的方法 – 理解線程優(yōu)先級的概念,以及基于優(yōu)先級的線程調(diào)度 55 /holder hold ingergers class HoldInteger{ int shareInt=1。 boolean moreData = true。 public void setSharedInt(int val) {shareInt = val。} public int getSharedInt(){return shareInt。} public void setMoreData(boolean b) {moreData = b。} public boolean hasMoreData() {return moreData。} } 56 //producer produces integers and put them into holder class ProducInteger extends Thread{ HoldInteger pHold。 public ProducInteger(HoldInteger h) { pHold = h。 } public void run() { for(int count=0。 count10。count++){ //sleep for random interval try{ ((int)(()*300))。 } catch(InterruptedException e){ (())。 } (count)。 (Prodecer set shareInt to + count)。 } //setMoreData to false when count10. It means holder is full (false)。 } } 返回 57 //consumer get integers from holder class ConsumeInteger extends Thread{ HoldInteger cHold。 public ConsumeInteger(HoldInteger h) { cHold = h。 } public void run() { int val。 while(()){ //sleep for random interval try{ ((int)(()*3000))。 } catch(InterruptedException e){ (())。 } val = ()。 (Consumer retrieved + val)。 } } } 返回 58 public class synDemo { public static void main(String args[]) { HoldInteger h = new HoldInteger()。 ProducInteger p=new ProducInteger(h)。 ConsumeInteger c=new ConsumeInteger(h)。 ()。 ()。 } } 返回 59 運行結(jié)果 ? Prodecer set shareInt to 0 ? Prodecer set shareInt to 1 ? Prodecer set shareInt to 2 ? Prodecer set shareInt to 3 ? Prodecer set shareInt to 4 ? Consumer retrieved4 ? Prodecer set shareInt to 5 ? Prodecer set shareInt to 6 ? Prodecer set shareInt to 7 ? Prodecer set shareInt to 8 ? Prodecer set shareInt to 9 ? Consumer retrieved9 返回 60 ? 掛起 有時候兩個線程并不是同步的,即不涉及都需要調(diào)用一個同步方法,但線程也可能需要暫時的掛起。所謂掛起一個線程就是讓線程暫時讓出CPU的使用權(quán)限,暫時停止執(zhí)行,但停止執(zhí)行的持續(xù)時間不確定,因此不能使用 sleep方法暫停線程。掛起一個線程需使用 wait方法,即讓準(zhǔn)備掛起的線程調(diào)用 wait 方法,主動讓出 CPU的使用權(quán)限 . ? 恢復(fù) 為了恢復(fù)該線程,其它線程在占有 CUP資源期間,讓掛起的線程的目標(biāo)對象執(zhí)行 notifyAll()方法,使得掛起的線程繼續(xù)執(zhí)行;如果線程沒有目標(biāo)對象,為了恢復(fù)該線程,其它線程在占有 CUP資源期間,讓掛起的線程調(diào)用 notifyAll()方法,使掛起的線程繼續(xù)執(zhí)行。 返回 61 public synchronized void 存取 (int number) //存取方法 { if(()==會計 ) { (今天是星期 +weekDay+\n)。 for(int i=1。i=3。i++) { money=money+number。 try { (1000)。 } catch(InterruptedException e){} (帳上有 +money+萬 \n)。 } } 為什么在會計讓出 CPU后,出納仍然不能運行存取 () 方法? 當(dāng)會計讓出 CPU后,出納啟動線程 調(diào)用 run()方法,但會計在調(diào)用存取 () 方法時使用了 synchronized
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1