[Java] 生產者&消費者問題

來源:互聯網
上載者:User

無聊,想起還沒自己實現過。寫了個。備忘。

生產者&消費者問題描述

有一個生產者線程在生產產品,並放入倉庫,另有一消費者線程從倉庫中取出產品進行消費。
這個過程可以是無休止的,不能因為倉庫已滿生產者放不進產品而終止,也不能因為倉庫中沒有產品了消費者停止消費。

本例子採用使生產者與消費者之間的保持同步的方式實現此功能。當生產者生產的產品放入倉庫時,會通知消費者可以進行消費了,如果放滿了倉庫則生產者暫時放棄自己的執行權,進入等待狀態等待消費者消費產品;消費者從倉庫中消費產品,如果發現倉庫已空,則進入等待狀態等待生產者生產新產品,並且每消費一件產品則通知生產者生產。

由程式最後的列印輸出可看出,從“通知”到生產者/消費者真正執行生產/消費操作的時機也是不固定的。

下面是代碼:

package lab.sodino.pc;import java.util.LinkedList;/** * @author Sodino E-mail:sodinoopen@hotmail.com * @version Time:2012-7-24 下午04:25:47 */public class Sync {/** 倉庫。 */private LinkedList<Goods> list;/** 倉庫最多儲存10產品。 */public static final int CAPABILITY = 10;/** 限制只生產/消費100個產品,然後結束運行。 */public static final int MAX_AMOUNT = 100;public static void main(String[] args) {Sync sync = new Sync();sync.run();}public void run() {list = new LinkedList<Goods>();new Product().start();new Consume().start();}/** * 生產者:<br/> * 1.負責生產產品;<br/> * 2.生產出產品後負責通知消費者線程消費;<br/> * 3.庫存為MAX時,暫停生產;<br/> * 4.每次只生產一件產品。<br/> */class Product extends Thread {int goodsIdx = 0;public void run() {try {while (true) {if (goodsIdx > MAX_AMOUNT) {break;}synchronized (list) {while (list.size() == CAPABILITY) {if (list.size() == CAPABILITY) {System.out.println("ProductGoodses's Amount is full.  wait");// 等待消費者消費才有空間騰出給新生產的產品list.wait();}}Goods goods = new Goods(goodsIdx++);if (list.add(goods)) {System.out.println("Productnew[" + String.valueOf(goods) + "]");// 通知消費者消費list.notify();}}}} catch (InterruptedException e) {e.printStackTrace();}}}/** * 消費者:<br/> * 1.一次只消費一個產品;<br/> * 2.消費完產品後負責通知生產者繼續生產。<br/> * */class Consume extends Thread {public void run() {try {while (true) {synchronized (list) {while (list.size() == 0) {System.out.println("ConsumeThere's no goods,wait...");// 等待生產者生產list.wait();}Goods goods = list.remove(0);System.out.println("Consumeremove[" + String.valueOf(goods) + "]");if (goods.getIndex() < MAX_AMOUNT) {// 通知生產者繼續生產list.notify();} else {System.out.println("List.size=" + list.size());break;}}}} catch (InterruptedException e) {e.printStackTrace();}}}}

package lab.sodino.pc;/** * @author Sodino E-mail:sodinoopen@hotmail.com * @version Time:2012-7-24 下午04:25:47 */public class Goods {private int index;public Goods(int idx) {index = idx;}public int getIndex() {return index;}public String toString() {String line = "Goods id:" + Integer.toString(index);return line;}}

輸出:

ConsumeThere's no goods,wait...Productnew[Goods id:0]Consumeremove[Goods id:0]ConsumeThere's no goods,wait...Productnew[Goods id:1]Consumeremove[Goods id:1]ConsumeThere's no goods,wait...Productnew[Goods id:2]Consumeremove[Goods id:2]ConsumeThere's no goods,wait...Productnew[Goods id:3]Consumeremove[Goods id:3]ConsumeThere's no goods,wait...Productnew[Goods id:4]Consumeremove[Goods id:4]ConsumeThere's no goods,wait...Productnew[Goods id:5]Consumeremove[Goods id:5]ConsumeThere's no goods,wait...Productnew[Goods id:6]Consumeremove[Goods id:6]ConsumeThere's no goods,wait...Productnew[Goods id:7]Consumeremove[Goods id:7]ConsumeThere's no goods,wait...Productnew[Goods id:8]Consumeremove[Goods id:8]ConsumeThere's no goods,wait...Productnew[Goods id:9]Consumeremove[Goods id:9]ConsumeThere's no goods,wait...Productnew[Goods id:10]Productnew[Goods id:11]Productnew[Goods id:12]Productnew[Goods id:13]Productnew[Goods id:14]Productnew[Goods id:15]Productnew[Goods id:16]Productnew[Goods id:17]Productnew[Goods id:18]Productnew[Goods id:19]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:10]Consumeremove[Goods id:11]Consumeremove[Goods id:12]Consumeremove[Goods id:13]Consumeremove[Goods id:14]Consumeremove[Goods id:15]Consumeremove[Goods id:16]Consumeremove[Goods id:17]Consumeremove[Goods id:18]Consumeremove[Goods id:19]ConsumeThere's no goods,wait...Productnew[Goods id:20]Productnew[Goods id:21]Productnew[Goods id:22]Productnew[Goods id:23]Productnew[Goods id:24]Productnew[Goods id:25]Productnew[Goods id:26]Productnew[Goods id:27]Productnew[Goods id:28]Productnew[Goods id:29]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:20]Consumeremove[Goods id:21]Consumeremove[Goods id:22]Consumeremove[Goods id:23]Consumeremove[Goods id:24]Consumeremove[Goods id:25]Consumeremove[Goods id:26]Consumeremove[Goods id:27]Consumeremove[Goods id:28]Consumeremove[Goods id:29]ConsumeThere's no goods,wait...Productnew[Goods id:30]Productnew[Goods id:31]Productnew[Goods id:32]Productnew[Goods id:33]Productnew[Goods id:34]Productnew[Goods id:35]Productnew[Goods id:36]Productnew[Goods id:37]Productnew[Goods id:38]Productnew[Goods id:39]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:30]Consumeremove[Goods id:31]Consumeremove[Goods id:32]Consumeremove[Goods id:33]Consumeremove[Goods id:34]Consumeremove[Goods id:35]Consumeremove[Goods id:36]Consumeremove[Goods id:37]Consumeremove[Goods id:38]Consumeremove[Goods id:39]ConsumeThere's no goods,wait...Productnew[Goods id:40]Productnew[Goods id:41]Productnew[Goods id:42]Productnew[Goods id:43]Productnew[Goods id:44]Productnew[Goods id:45]Productnew[Goods id:46]Productnew[Goods id:47]Productnew[Goods id:48]Productnew[Goods id:49]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:40]Consumeremove[Goods id:41]Consumeremove[Goods id:42]Consumeremove[Goods id:43]Consumeremove[Goods id:44]Consumeremove[Goods id:45]Consumeremove[Goods id:46]Consumeremove[Goods id:47]Consumeremove[Goods id:48]Consumeremove[Goods id:49]ConsumeThere's no goods,wait...Productnew[Goods id:50]Productnew[Goods id:51]Productnew[Goods id:52]Productnew[Goods id:53]Productnew[Goods id:54]Productnew[Goods id:55]Productnew[Goods id:56]Productnew[Goods id:57]Productnew[Goods id:58]Productnew[Goods id:59]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:50]Consumeremove[Goods id:51]Consumeremove[Goods id:52]Productnew[Goods id:60]Productnew[Goods id:61]Productnew[Goods id:62]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:53]Consumeremove[Goods id:54]Consumeremove[Goods id:55]Consumeremove[Goods id:56]Consumeremove[Goods id:57]Consumeremove[Goods id:58]Consumeremove[Goods id:59]Consumeremove[Goods id:60]Consumeremove[Goods id:61]Consumeremove[Goods id:62]ConsumeThere's no goods,wait...Productnew[Goods id:63]Productnew[Goods id:64]Productnew[Goods id:65]Productnew[Goods id:66]Productnew[Goods id:67]Productnew[Goods id:68]Productnew[Goods id:69]Productnew[Goods id:70]Productnew[Goods id:71]Productnew[Goods id:72]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:63]Consumeremove[Goods id:64]Consumeremove[Goods id:65]Consumeremove[Goods id:66]Consumeremove[Goods id:67]Consumeremove[Goods id:68]Consumeremove[Goods id:69]Consumeremove[Goods id:70]Consumeremove[Goods id:71]Consumeremove[Goods id:72]ConsumeThere's no goods,wait...Productnew[Goods id:73]Productnew[Goods id:74]Productnew[Goods id:75]Productnew[Goods id:76]Productnew[Goods id:77]Productnew[Goods id:78]Productnew[Goods id:79]Productnew[Goods id:80]Productnew[Goods id:81]Productnew[Goods id:82]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:73]Consumeremove[Goods id:74]Productnew[Goods id:83]Productnew[Goods id:84]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:75]Consumeremove[Goods id:76]Consumeremove[Goods id:77]Consumeremove[Goods id:78]Consumeremove[Goods id:79]Consumeremove[Goods id:80]Consumeremove[Goods id:81]Consumeremove[Goods id:82]Consumeremove[Goods id:83]Consumeremove[Goods id:84]ConsumeThere's no goods,wait...Productnew[Goods id:85]Productnew[Goods id:86]Productnew[Goods id:87]Productnew[Goods id:88]Productnew[Goods id:89]Productnew[Goods id:90]Productnew[Goods id:91]Productnew[Goods id:92]Productnew[Goods id:93]Productnew[Goods id:94]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:85]Productnew[Goods id:95]ProductGoodses's Amount is full.  waitConsumeremove[Goods id:86]Consumeremove[Goods id:87]Consumeremove[Goods id:88]Consumeremove[Goods id:89]Consumeremove[Goods id:90]Consumeremove[Goods id:91]Productnew[Goods id:96]Productnew[Goods id:97]Productnew[Goods id:98]Productnew[Goods id:99]Productnew[Goods id:100]Consumeremove[Goods id:92]Consumeremove[Goods id:93]Consumeremove[Goods id:94]Consumeremove[Goods id:95]Consumeremove[Goods id:96]Consumeremove[Goods id:97]Consumeremove[Goods id:98]Consumeremove[Goods id:99]Consumeremove[Goods id:100]List.size=0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.