



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精品文檔河北建筑工程學(xué)院實(shí)驗(yàn)報(bào)告年月日班級物聯(lián)孫勝杰學(xué)號20143260評分姓名218142實(shí)驗(yàn)臺號同組人員實(shí)驗(yàn)名稱經(jīng)典進(jìn)程同步問題- 生產(chǎn)者消課程名稱操作系統(tǒng)費(fèi)者問題模擬實(shí)現(xiàn)儀器名稱型號規(guī)格儀器編號裝有 eclipse軟件和 Java 開發(fā)環(huán)境的 PC機(jī)一臺一 實(shí)驗(yàn)?zāi)康? 深刻理解進(jìn)程同步的概念。2 掌握經(jīng)典同步問題,生產(chǎn)者消費(fèi)者問題。二 實(shí)驗(yàn)設(shè)備PC機(jī)三 實(shí)驗(yàn)內(nèi)容在 Java 開發(fā)環(huán)境下模擬經(jīng)典進(jìn)程同步問題,生產(chǎn)者消費(fèi)者問題。四 程序的主要代碼package 生產(chǎn)者與消費(fèi)者問題 ;import java.util.LinkedList;import java.util.Scanner;cl
2、ass Storage / 倉庫最大存儲量privatefinalintMAX_SIZE = 100;。1歡迎下載精品文檔/ 倉庫存儲的載體privateLinkedList<Object> list =newLinkedList<Object>();/ 生產(chǎn) num個(gè)產(chǎn)品publicvoid produce( intnum)/ 同步代碼段synchronized(list)/ 如果倉庫剩余容量不足while (list.size() + num > MAX_SIZE)System. out .println("要生產(chǎn)的產(chǎn)品數(shù)量 :" + nu
3、m +"t 庫存量 :" + list.size() +"t暫時(shí)不能執(zhí)行生產(chǎn)任務(wù)!");System. out .println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作(0)?");try/ 由于條件不滿足,生產(chǎn)阻塞。2歡迎下載精品文檔list.wait();catch (InterruptedException e)e.printStackTrace();/ 生產(chǎn)條件滿足情況下,生產(chǎn) num個(gè)產(chǎn)品for( inti = 1; i <= num; +i)list.add(new Object();System. out .pri
4、ntln("已經(jīng)生產(chǎn)產(chǎn)品數(shù) :" + num + "t現(xiàn)庫存量 :" + list.size();System. out .println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作(0)?");list.notifyAll();。3歡迎下載精品文檔/ 消費(fèi) num個(gè)產(chǎn)品publicvoid consume( intnum)/ 同步代碼段synchronized(list)/ 如果倉庫存儲量不足while (list.size() < num)System. out .println("要消費(fèi)的產(chǎn)品數(shù)量 :" +
5、 num +"t 庫存量 :"+ list.size() + "t暫時(shí)不能執(zhí)行生產(chǎn)任務(wù)!");System. out .println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作(0)?");try/ 由于條件不滿足,消費(fèi)阻塞list.wait();。4歡迎下載精品文檔catch (InterruptedException e)e.printStackTrace();/ 消費(fèi)條件滿足情況下,消費(fèi) num個(gè)產(chǎn)品for( inti = 1; i <= num; +i)list.remove();System. out .println(
6、"已經(jīng)消費(fèi)產(chǎn)品數(shù) :" + num + "t現(xiàn)庫存量為 :" + list.size();System. out .println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作(0)?");list.notifyAll();。5歡迎下載精品文檔/ get/set方法publicLinkedList<Object> getList()returnlist;publicvoid setList(LinkedList<Object> list)this .list = list;publicintgetMAX_SIZE()
7、returnMAX_SIZE;/ 生產(chǎn)者類 Producer 繼承線程類 Threadclass Producerextends Thread/ 每次生產(chǎn)的產(chǎn)品數(shù)量。6歡迎下載精品文檔privateintnum;/ 所在放置的倉庫privateStorage storage;/ 構(gòu)造函數(shù),設(shè)置倉庫publicProducer(Storage storage)this .storage = storage;/ 線程 run 函數(shù)publicvoid run()produce(num);/ 調(diào)用倉庫 Storage 的生產(chǎn)函數(shù)publicvoid produce( intnum)storage.p
8、roduce(num);。7歡迎下載精品文檔/ get/set方法publicintgetNum()returnnum;publicvoid setNum( intnum)this .num = num;publicStorage getStorage()returnstorage;publicvoid setStorage(Storage storage)this .storage = storage;。8歡迎下載精品文檔/ 消費(fèi)者類 Consumer繼承線程類 Thread class Consumer extends Thread/ 每次消費(fèi)的產(chǎn)品數(shù)量privateintnum;/ 所在
9、放置的倉庫privateStorage storage;/ 構(gòu)造函數(shù),設(shè)置倉庫publicConsumer(Storage storage)this .storage = storage;/ 線程 run 函數(shù)publicvoid run()consume(num);。9歡迎下載精品文檔/ 調(diào)用倉庫 Storage 的生產(chǎn)函數(shù)publicvoid consume( intnum)storage.consume(num);/ get/set方法publicintgetNum()returnnum;publicvoid setNum( intnum)this .num = num;publicSt
10、orage getStorage()returnstorage;。10歡迎下載精品文檔publicvoid setStorage(Storage storage)this .storage = storage;publicclass ProducerAndConsumerpublicstaticvoid main(String args)/ 倉庫對象Storage storage =new Storage();/ 生產(chǎn)者對象Producer p1 =new Producer(storage);Producer p2 =new Producer(storage);Producer p3 =new
11、 Producer(storage);Producer p4 =new Producer(storage);Producer p5 =new Producer(storage);Producer p6 =new Producer(storage);。11歡迎下載精品文檔Producer p7 =new Producer(storage);Producer p8=new Producer(storage);Producer p9 =new Producer(storage);Producer p10 =new Producer(storage);/ 消費(fèi)者對象Consumer c1 = new
12、Consumer(storage);Consumer c2 = new Consumer(storage);Consumer c3 = new Consumer(storage);Consumer c4 = new Consumer(storage);Consumer c5 = new Consumer(storage);Consumer c6 = new Consumer(storage);Consumer c7 = new Consumer(storage);Consumer c8 = new Consumer(storage);Consumer c9 = new Consumer(sto
13、rage);Consumer c10 = new Consumer(storage);System. out .println("已生產(chǎn)產(chǎn)品數(shù)量 :0t 已消費(fèi)產(chǎn)品數(shù)量:0t 庫存量: 0t 最大存儲空間: 100");System. out .println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作(0)?");。12歡迎下載精品文檔Scanner isProduer=new Scanner(System. in );for ( inti =1;i<10;i+)/System.out.println("進(jìn)行生產(chǎn)操作 (1) ,還是消費(fèi)操作
14、(0)?");if (isProduer.nextInt()=1)System. out .print("請輸入要生產(chǎn)的產(chǎn)品數(shù)量:");Scanner p11= new Scanner(System. in );if (i=1)p1.setNum(p11.nextInt();p1.start(); elseif (i=2)p2.setNum(p11.nextInt();p2.start(); elseif (i=3)p3.setNum(p11.nextInt();p3.start();elseif (i=4)p4.setNum(p11.nextInt();。13歡
15、迎下載精品文檔p4.start();elseif (i=5)p5.setNum(p11.nextInt();p5.start(); elseif (i=6)p6.setNum(p11.nextInt();p6.start(); elseif (i=7)p7.setNum(p11.nextInt();p7.start();elseif (i=8)p8.setNum(p11.nextInt();p8.start();elseif (i=9)p9.setNum(p11.nextInt();p9.start();elseif (i=10)p10.setNum(p11.nextInt();。14歡迎下載
16、精品文檔p10.start(); else System. out .print("請輸入要消費(fèi)的產(chǎn)品數(shù)量:");Scanner p12= new Scanner(System. in );if (i=1)c1.setNum(p12.nextInt();c1.start(); elseif (i=2)c2.setNum(p12.nextInt();c2.start(); elseif (i=3)c3.setNum(p12.nextInt();c3.start();elseif (i=4)c4.setNum(p12.nextInt();c4.start();。15歡迎下載精品
17、文檔elseif (i=5)c5.setNum(p12.nextInt();c5.start(); elseif (i=6)c6.setNum(p12.nextInt();c6.start(); elseif (i=7)c7.setNum(p12.nextInt();c7.start(); elseif (i=8)c8.setNum(p12.nextInt();c8.start();elseif (i=9)c9.setNum(p12.nextInt();c9.start();elseif (i=10)c10.setNum(p12.nextInt();c10.start();。16歡迎下載精品文檔五、實(shí)驗(yàn)結(jié)果本程序應(yīng)用Java 軟件開發(fā),沒有引入界面,需要使用可以運(yùn)行 java 的 eclipse 等軟件運(yùn)行。程序目錄為:生產(chǎn)者與消費(fèi)者問題生產(chǎn)者消費(fèi)者問題代碼生產(chǎn)者與消費(fèi)者問題下的 ProducerAndCons
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 生態(tài)農(nóng)場餐飲承包經(jīng)營與鄉(xiāng)村旅游合作合同
- 車輛租賃合同租賃車輛維修保養(yǎng)期限補(bǔ)充協(xié)議范本
- 草牧場綜合開發(fā)與承包管理協(xié)議
- 植物園代養(yǎng)收養(yǎng)入住生態(tài)旅游合同
- 餐飲連鎖店店長全面管理合同
- 餐飲服務(wù)員勞動合同解除與終止合同范本
- 《知識產(chǎn)權(quán)保護(hù)規(guī)則與格式合同條款詳細(xì)說明》
- 工業(yè)園區(qū)場地租賃合同終止與環(huán)保設(shè)施遷移協(xié)議
- 車牌租賃市場調(diào)查分析報(bào)告合同范本
- 采購談判與跟單執(zhí)行標(biāo)準(zhǔn)合同范本
- MES業(yè)務(wù)藍(lán)圖(合并版)-V1
- 煉鋼-精煉-連鑄過程鋼水頁P(yáng)PT課件
- 安全知識進(jìn)校園宣傳課件——XX小學(xué)
- 剖宮產(chǎn)術(shù)后再次妊娠陰道分娩管理的專家共識
- 《掃除道》樊登讀書文字版
- 教學(xué)演示文稿,建筑企業(yè)科技創(chuàng)新方法講座()
- 裝飾工程材料清單
- 中國傳統(tǒng)節(jié)日文化中現(xiàn)代德育價(jià)值的研究課題結(jié)題報(bào)告
- 肺動脈導(dǎo)管監(jiān)測的參數(shù)及意義
- 職稱評審申報(bào)系統(tǒng)PPT課件
- 水利工程漿砌石工程監(jiān)理細(xì)則
評論
0/150
提交評論