java多線程 生產者消費者模式

來源:互聯網
上載者:User

標籤:out   線程   java多線程   exce   this   產品   線程等待   i++   over   

package de.bvb;/** * 生產者消費者模式 * 通過  wait() 和 notify() 通訊方法實現 * */public class Test1 {    public static void main(String[] args) {        Godown godown = new Godown(50);        for (int i = 0; i < 5; i++) {            new ProducerThread(i * 10, godown).start();            new ConsumerThread(i * 10, godown).start();        }    }}/** 倉庫 */class Godown {    public static final int max_size = 100; // 最大庫存量    public int currentSize; // 當前庫存    public Godown(int currentSize) {        this.currentSize = currentSize;    }    // 生產指定數量的產品    public synchronized void produce(int size) {        try {            while (currentSize + size > max_size) { // 不滿足生產的條件                System.out.println("要生產的產品數量" + size + "超過剩餘庫容量"                        + (max_size - currentSize) + ",暫時不能執行生產任務!");                // 當前的生產線程等待                wait();            }            // 滿足生產條件,則進行生產,這裡簡單的更改當前庫存量            currentSize += size;            System.out.println("生產前倉儲量為:" + (currentSize - size) + ";生產了"                    + size + "個產品,現倉儲量為" + currentSize);            // 喚醒在此對象監視器上等待的所有線程            notifyAll();        } catch (Exception e) {            e.printStackTrace();        }    }    // 消費指定數量的產品    public synchronized void consume(int size) {        try {            while (currentSize < size) { // 不滿足消費的條件                System.out.println("要消費的產品數量" + size + "超過剩餘庫存量"                        + (currentSize) + ",暫時不能執行消費任務!");                // 當前的生產線程等待                wait();            }            // 滿足生產條件,則進行生產,這裡簡單的更改當前庫存量            currentSize -= size;            System.out.println("消費前的倉儲量為" + (currentSize + size) + ";已經消費了"                    + size + "個產品,現倉儲量為" + currentSize);            // 喚醒在此對象監視器上等待的所有線程            notifyAll();        } catch (Exception e) {            e.printStackTrace();        }    }}/** 生產者線程 */class ProducerThread extends Thread {    private int size;    private Godown godown;    public ProducerThread(int size, Godown godown) {        super();        this.size = size;        this.godown = godown;    }    @Override    public void run() {        godown.produce(size);    }}class ConsumerThread extends Thread {    private int size;    private Godown godown;    public ConsumerThread(int size, Godown godown) {        super();        this.size = size;        this.godown = godown;    }    @Override    public void run() {        godown.consume(size);    }}

 

java多線程 生產者消費者模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.