生產者消費者報告_第1頁
生產者消費者報告_第2頁
生產者消費者報告_第3頁
生產者消費者報告_第4頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精品文檔河北建筑工程學院實驗報告年月日班級物聯孫勝杰學號20143260評分姓名218142實驗臺號同組人員實驗名稱經典進程同步問題- 生產者消課程名稱操作系統(tǒng)費者問題模擬實現儀器名稱型號規(guī)格儀器編號裝有 eclipse軟件和 Java 開發(fā)環(huán)境的 PC機一臺一 實驗目的1 深刻理解進程同步的概念。2 掌握經典同步問題,生產者消費者問題。二 實驗設備PC機三 實驗內容在 Java 開發(fā)環(huán)境下模擬經典進程同步問題,生產者消費者問題。四 程序的主要代碼package 生產者與消費者問題 ;import java.util.LinkedList;import java.util.Scanner;cl

2、ass Storage / 倉庫最大存儲量privatefinalintMAX_SIZE = 100;。1歡迎下載精品文檔/ 倉庫存儲的載體privateLinkedList<Object> list =newLinkedList<Object>();/ 生產 num個產品publicvoid produce( intnum)/ 同步代碼段synchronized(list)/ 如果倉庫剩余容量不足while (list.size() + num > MAX_SIZE)System. out .println("要生產的產品數量 :" + nu

3、m +"t 庫存量 :" + list.size() +"t暫時不能執(zhí)行生產任務!");System. out .println("進行生產操作 (1) ,還是消費操作(0)?");try/ 由于條件不滿足,生產阻塞。2歡迎下載精品文檔list.wait();catch (InterruptedException e)e.printStackTrace();/ 生產條件滿足情況下,生產 num個產品for( inti = 1; i <= num; +i)list.add(new Object();System. out .pri

4、ntln("已經生產產品數 :" + num + "t現庫存量 :" + list.size();System. out .println("進行生產操作 (1) ,還是消費操作(0)?");list.notifyAll();。3歡迎下載精品文檔/ 消費 num個產品publicvoid consume( intnum)/ 同步代碼段synchronized(list)/ 如果倉庫存儲量不足while (list.size() < num)System. out .println("要消費的產品數量 :" +

5、 num +"t 庫存量 :"+ list.size() + "t暫時不能執(zhí)行生產任務!");System. out .println("進行生產操作 (1) ,還是消費操作(0)?");try/ 由于條件不滿足,消費阻塞list.wait();。4歡迎下載精品文檔catch (InterruptedException e)e.printStackTrace();/ 消費條件滿足情況下,消費 num個產品for( inti = 1; i <= num; +i)list.remove();System. out .println(

6、"已經消費產品數 :" + num + "t現庫存量為 :" + list.size();System. out .println("進行生產操作 (1) ,還是消費操作(0)?");list.notifyAll();。5歡迎下載精品文檔/ get/set方法publicLinkedList<Object> getList()returnlist;publicvoid setList(LinkedList<Object> list)this .list = list;publicintgetMAX_SIZE()

7、returnMAX_SIZE;/ 生產者類 Producer 繼承線程類 Threadclass Producerextends Thread/ 每次生產的產品數量。6歡迎下載精品文檔privateintnum;/ 所在放置的倉庫privateStorage storage;/ 構造函數,設置倉庫publicProducer(Storage storage)this .storage = storage;/ 線程 run 函數publicvoid run()produce(num);/ 調用倉庫 Storage 的生產函數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歡迎下載精品文檔/ 消費者類 Consumer繼承線程類 Thread class Consumer extends Thread/ 每次消費的產品數量privateintnum;/ 所在

9、放置的倉庫privateStorage storage;/ 構造函數,設置倉庫publicConsumer(Storage storage)this .storage = storage;/ 線程 run 函數publicvoid run()consume(num);。9歡迎下載精品文檔/ 調用倉庫 Storage 的生產函數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();/ 生產者對象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);/ 消費者對象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("已生產產品數量 :0t 已消費產品數量:0t 庫存量: 0t 最大存儲空間: 100");System. out .println("進行生產操作 (1) ,還是消費操作(0)?");。12歡迎下載精品文檔Scanner isProduer=new Scanner(System. in );for ( inti =1;i<10;i+)/System.out.println("進行生產操作 (1) ,還是消費操作

14、(0)?");if (isProduer.nextInt()=1)System. out .print("請輸入要生產的產品數量:");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("請輸入要消費的產品數量:");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歡迎下載精品文檔五、實驗結果本程序應用Java 軟件開發(fā),沒有引入界面,需要使用可以運行 java 的 eclipse 等軟件運行。程序目錄為:生產者與消費者問題生產者消費者問題代碼生產者與消費者問題下的 ProducerAndCons

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論