java生產者消費者問題

來源:互聯網
上載者:User

今天想寫資料庫的一個應用的,因為涉及到加鎖解鎖設計,我就查了查生產中消費者問題,自己寫了一下。

首先,有一個倉庫,也就是一個當做緩衝區的東西

public class WareHouse {    private int products[];    private String name;    private int base = 0;    private int top = 0;    private int capacity;    public WareHouse(int capacity, String name) {        products = new int[capacity];        this.name = name;        this.capacity = capacity;    }    public synchronized int pop() {        int product = 0;        if (top == base) {            try {                System.out.println("倉庫空了,快點來生產啊");                wait();            } catch (InterruptedException e) {                System.out.println("stop push");            }        }        if(top!=base){        product = products[top--];        System.out.println("消費了一個產品"+product);        notify();        }        return product;    }    public synchronized void push(int n) {        if (top == capacity - 1) {            try {                System.out.println("倉庫滿了,快點來消費");                wait();            } catch (InterruptedException e) {                System.out.println("stop push");            }        }        products[++top] = n;        System.out.println("生產了一個產品 "+n);        notify();    }    public int[] getProducts() {        return products;    }    public String getName() {        return name;    }}
然後是生產者和消費者
public class Producer extends Thread {    private int no;    private WareHouse wareHouse;    public Producer(int no, WareHouse house) {        this.no = no;        this.wareHouse = house;    }    @Override    public void run() {        this.excuteProduce();    }    private void excuteProduce() {        int i = 0;        while (true) {            wareHouse.push(i);           // System.out.println(no + "我生產了一個產品" + i);            try {                Thread.sleep(1000);            } catch (Exception e) {                return;            }            ++i;        }    }}
public class Consumer extends Thread {    private int no;    private WareHouse wareHouse;    public Consumer(int no, WareHouse house) {        this.no = no;        wareHouse = house;    }    @Override    public void run() {        excuteConsume();    }    private void excuteConsume() {        while (true) {            int n=this.wareHouse.pop();           // System.out.println("我消費了"+n);            try {                Thread.sleep(1000);            }catch (Exception e){                return;            }        }    }}

聯繫我們

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