美文网首页
多线程实现消费者-生产者模型

多线程实现消费者-生产者模型

作者: 洋__ | 来源:发表于2016-12-22 18:05 被阅读91次
package consumer_provider;

/**
 * 资源共享,任务协作
 * Created by zhangjiqiang on 2016/12/21.
 */
public class Test {

    public static void main(String[] args) {
        final GoDown goDown=new GoDown(2);
        Thread A=new Thread(new Consumer(goDown,3),"A");
        Thread B=new Thread(new Consumer(goDown,5),"B");
        Thread C=new Thread(new Consumer(goDown,7),"C");

        Thread AAAAAA=new Thread(new Producer(goDown,9),"AAAAAA");
        Thread BBBBBB=new Thread(new Producer(goDown,6),"BBBBBB");
        Thread CCCCCC=new Thread(new Producer(goDown,2),"CCCCCC");


        System.out.println("初始库存:"+goDown.getCurrent());
        A.start();
        B.start();
        C.start();

        AAAAAA.start();
        BBBBBB.start();
        CCCCCC.start();


        Thread last=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("最后库存为:"+goDown.getCurrent());
            }
        });
        last.start();

    }

}

class GoDown {

    public static final int MAX_SIZE = 10;

    private int current;

    public int getCurrent() {
        return current;
    }

    public GoDown(int current) {
        this.current = current;
    }

    public synchronized void consume(int need) {
        /**
         * 若需要的数量超过当前库存,等待生产
         */
        while (need > this.current) {
            System.out.println(Thread.currentThread().getName() + "需要" + need + "个,当前库存不足,库存为" + this.current);
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /**
         * 进行消费
         */
        this.current = this.current - need;
        System.out.println(Thread.currentThread().getName()+"消费了"+need+",当前库存:"+this.current);
        /**
         * 消费结束后唤醒所有等待线程
         */
        notifyAll();

    }

    public synchronized void produce(int goods) {
        /**
         * 若生产数量+当前库存量>库存上限,等待消费
         */
        while (goods + this.current > MAX_SIZE) {
            System.out.println(Thread.currentThread().getName()+"要生产"+goods+"个,超过库存上限,当前库存为"+this.current);
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        /**
         * 进行生产
         */
        this.current=this.current+goods;
        System.out.println(Thread.currentThread().getName()+"生产了"+goods+",当前库存:"+this.current);
        /**
         * 唤醒所有生产线程
         */
        notifyAll();

    }
}

class Consumer implements Runnable{
    private GoDown goDown;
    private int need;

    public Consumer(GoDown goDown, int need) {
        this.goDown = goDown;
        this.need = need;
    }

    @Override
    public void run(){
        goDown.consume(need);
    }
}

class Producer implements Runnable{
    private GoDown goDown;
    private int goods;

    public Producer(GoDown goDown, int goods) {
        this.goDown = goDown;
        this.goods = goods;
    }

    @Override
    public void run(){
        goDown.produce(goods);
    }
}

相关文章

网友评论

      本文标题:多线程实现消费者-生产者模型

      本文链接:https://www.haomeiwen.com/subject/uwdhvttx.html