• 生产者消费者模型


    /**
     * 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();
                    }
                }
    
            }
        }
    }
  • 相关阅读:
    Linuxqq shell脚本安装后的卸载
    A Spy in the Metro UVA-1025(dp)
    L1-064 估值一亿的AI核心代码
    龙芯 3A4000 安装 Debian10 (via debootstrap)
    Linux用户和用户组
    /etc/issue、/etc/issue.net和/etc/motd的区别
    一种注释
    龙芯平台51单片机开发环境搭建笔记
    Rails UVA-514 (stack)
    The SetStack Computer UVA-12096 (set 操作)
  • 原文地址:https://www.cnblogs.com/xiaorenwu702/p/5702313.html
Copyright © 2020-2023  润新知