生产者消费者模型

/**
 * Created by damon on 7/25/16.
 * 生产者消费者 测试
 */
public class Producer_Consumer_test3 {
    /*
    知识点(以下知识点随便百度都能找到):
    0.生产者消费者模型的理解
    1.wait notify 的用法
    2.synchronized 的用法
    3. wait和sleep的区别
     */
    public static void main(String[] args) {
        Operation operation = new Operation();
        new Thread(new ConsumerThread(operation)).start();
        new Thread(new ProducerThread(operation)).start();
    }

    static class Good {
        /**
         * 商品的id
         */
        public int id;

        public Good(int id) {
            this.id = id;
        }
    }

    static class Operation {

        /**
         * 仓库的最大的存储数据量
         */
        static final int MAX_NUMBER = 10;
        Good[] goods = new Good[MAX_NUMBER];

        /**
         * 当前仓库的个数
         */
        private int currNumber = 0;

        public synchronized void produce(Good good) {
            while (currNumber == MAX_NUMBER) {
                try {
                    System.out.println("===仓库满了!!!停止生产,仓库最大容量-->>"+MAX_NUMBER);
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            currNumber++;
            System.out.println("===生产一个,仓库最大容量-->>"+MAX_NUMBER+", 当然仓库数量-->>"+currNumber);
            goods[currNumber - 1] = good;
            notify();
        }

        public synchronized void consume() {
            while(currNumber==0){
                try {
                    System.out.println("===仓库为空了!!!等待中...");
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            currNumber--;
            goods[currNumber] = null;
            System.out.println("===消费一个!!!仓库还有"+currNumber+"个");
            notify();
        }

    }

    /**
     * 生产产品的线程
     */
    static class ProducerThread implements Runnable {

        private Operation operation;
        private int id;

        public ProducerThread(Operation operation) {
            this.operation = operation;
        }

        @Override
        public void run() {
            while(true) {
            try {
                operation.produce(new Good(id));
                id++;
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
        }
    }

    /**
     * 消费产品的线程
     */
    static class ConsumerThread implements Runnable {

        private Operation operation;


        public ConsumerThread(Operation operation) {
            this.operation = operation;
        }

        @Override
        public void run() {
            while(true) {
                try {
                    operation.consume();
                    Thread.sleep(2400L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

 

posted @ 2016-07-25 09:02  小人物702  阅读(174)  评论(0编辑  收藏  举报