生產(chǎn)者消費(fèi)者報(bào)告_第1頁
生產(chǎn)者消費(fèi)者報(bào)告_第2頁
生產(chǎn)者消費(fèi)者報(bào)告_第3頁
生產(chǎn)者消費(fèi)者報(bào)告_第4頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精品文檔河北建筑工程學(xué)院實(shí)驗(yàn)報(bào)告年月日班級(jí)物聯(lián)孫勝杰學(xué)號(hào)20143260評(píng)分姓名218142實(shí)驗(yàn)臺(tái)號(hào)同組人員實(shí)驗(yàn)名稱經(jīng)典進(jìn)程同步問題- 生產(chǎn)者消課程名稱操作系統(tǒng)費(fèi)者問題模擬實(shí)現(xiàn)儀器名稱型號(hào)規(guī)格儀器編號(hào)裝有 eclipse軟件和 Java 開發(fā)環(huán)境的 PC機(jī)一臺(tái)一 實(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 / 倉庫最大存儲(chǔ)量privatefinalintMAX_SIZE = 100;。1歡迎下載精品文檔/ 倉庫存儲(chǔ)的載體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)/ 如果倉庫存儲(chǔ)量不足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)/ 倉庫對(duì)象Storage storage =new Storage();/ 生產(chǎn)者對(duì)象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)者對(duì)象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 最大存儲(chǔ)空間: 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("請(qǐng)輸入要生產(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("請(qǐng)輸入要消費(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論