zoukankan      html  css  js  c++  java
  • 生产者消费者模型

    /**
     * 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();
                    }
                }
    
            }
        }
    }
  • 相关阅读:
    【CS Round #46 (Div. 1.5) B】Letters Deque
    【CS Round #46 (Div. 1.5) A】Letters Deque
    【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave
    【Codeforces Round #432 (Div. 2) B】Arpa and an exam about geometry
    【Codeforces Round #432 (Div. 1) A】 Five Dimensional Points
    【2017 Multi-University Training Contest
    Managing remote devices
    防止表单重复提交的解决方案整理
    防止表单重复提交的解决方案整理
    日期格式化函数
  • 原文地址:https://www.cnblogs.com/xiaorenwu702/p/5702313.html
Copyright © 2011-2022 走看看