JAVA生產者消費者(線程同步)代碼學習樣本_java

來源:互聯網
上載者:User

一、問題描述

生產者消費者問題是一個典型的線程同步問題。生產者生產商品放到容器中,容器有一定的容量(只能順序放,先放後拿),消費者消費商品,當容器滿了後,生產者等待,當容器為空白時,消費者等待。當生產者將商品放入容器後,通知消費者;當消費者拿走商品後,通知生產者。

二、解決方案

對容器資源加鎖,當取得鎖後,才能對互斥資源進行操作。

複製代碼 代碼如下:

public class ProducerConsumerTest {

    public static void main(String []args){
        Container con = new Container();
        Producer p = new Producer(con);
        Consumer c = new Consumer(con);
        new Thread(p).start();
        new Thread(c).start();
    }

}


class Goods{
    int id;
    public Goods(int id){
        this.id=id;
    }

    public String toString(){
        return "商品"+this.id;
    }
}

class Container{//容器採用棧,先進後出
    private int index = 0;
    Goods[] goods = new Goods[6];

    public synchronized void push(Goods good){
        while(index==goods.length){//當容器滿了,生產者等待
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        goods[index]=good;
        index++;
        notifyAll();//當生產者放入商品後通知消費者
    }

    public synchronized Goods pop(){
        while(index==0){//當容器內沒有商品是等待
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        index--;
        notifyAll();//當消費者消費了商品後通知生產者
        return goods[index];
    }
}

class Producer implements Runnable{

    Container con = new Container();
    public Producer(Container con){
        this.con=con;
    }

    public void run(){
        for(int i=0; i<20; i++){
            Goods good = new Goods(i);
            con.push(good);
            System.out.println("生產了:"+good);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

class Consumer implements Runnable{

    Container con = new Container();
    public Consumer(Container con){
        this.con=con;
    }

    public void run(){
        for(int i=0; i<20; i++){
            Goods good=con.pop();
            System.out.println("消費了:"+good);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

   
}

聯繫我們

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