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();
                    }
                }
    
            }
        }
    }
  • 相关阅读:
    sql server 日期
    restore database
    7.1设计并实现有理数库,使用整数表示分子和分母,完成有理数的加减乘除与化简运算
    6.2写search函数对已经排好的n个元素的整数数组a,查找整数key。
    6.1写sort函数对n个元素的整数数组n,按从小到大排序
    5.2将随机数模拟为不含大小王的扑克牌
    实现一个随机数库
    5.1写函数,返回1~52之间的随机数
    4.2分别使用循环和递归两种策略求二项式从c(n,k);
    4.1将某个大于1的自然数n分解为其素因子的乘积
  • 原文地址:https://www.cnblogs.com/xiaorenwu702/p/5702313.html
Copyright © 2011-2022 走看看