跟着師傅學模式-生產者消費者模式

圖片

引入 真實快遞模式下的 生產者消費者模式

消費者類

package cn.edu.Thread2;

public class Consume implements Runnable{
    public int speed;
    public Moniter moniter ;


    public Consume() {
        super();
        // TODO Auto-generated constructor stub
    }


    public Consume(Moniter moniter , int speed) {
        super();
        this.speed = speed;
        this.moniter = moniter;
        new Thread(this,"Consume").start();
    }

    /**
     * 消費
     */
    @Override
    public void run() {
        while(true){
            this.moniter.get(speed);
            try {
                Thread.sleep( (long) (Math.random()*1000));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


}

生存者類

package cn.edu.Thread2;

public class Produce implements Runnable {
    private Moniter moniter;
    private int speed;

    public Produce(Moniter moniter, int speed) {
        super();
        this.moniter = moniter;
        this.speed = speed;
        new Thread(this,"Produce").start();
    }

    public Produce() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            this.moniter.set(speed);
            try {
                Thread.sleep( (long) ( Math.random()*1000 ));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

緩衝區類

package cn.edu.Thread2;
/**
 * 緩衝區
 * @author Deri
 *
 */
public class Moniter {
    private int goods;
    private final int MAX = 100;
    /**
     *  消費
     * @param need
     * @return
     */
    public synchronized int get(int need){
        // 當需求  大於  庫存量時候。  線程等待
        if(goods < need ){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        goods -= need;
        System.out.println("  consume get:" + need+"\t庫存量" +goods);
        //喚醒其他線程
        notify();
        return need;
    }

    /**
     * 生產下
     * @param goods
     */
    public synchronized void set(int newGood){
        int count = newGood;
        //如果生產達到庫存 峯值 線程等待
        if( (goods+newGood) > MAX){
            newGood = newGood - (MAX - goods); //把 能存的先 存進來
            this.goods = MAX;
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 線程等待中  消費後   喚醒 該線程 就 繼續 儲存
        goods += newGood;  
        System.out.println("produce set: " + count +"\t 庫存量" +goods);
        notify();

    }
}

測試主類

package cn.edu.Thread2;

public class TestDemo {

    public static void main(String[] args) {
        int proSpeed = 10;
        int conSpeed = 11;
        Moniter m = new Moniter();
        new Produce(m, proSpeed);
        new Consume(m, conSpeed);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.exit(0);
    }

}

班主任強調了的多線程,今天用eclipse 做了一遍。升級了
2016年12月6日 15:31:14
mangodai

發佈了45 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章