zoukankan      html  css  js  c++  java
  • Java管程解决生产者消费者问题

    同样是实验存档。//。。

    依然以生产者消费者问题作为背景。

    管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就ok,不用去考虑进程同步的问题。

    管程:

    package entity.producerconsumer;
    
    public class Monition {
        private Buffer buffer;
    
        public Monition(int bufferSize) {
            buffer = new Buffer(bufferSize);
        }
    
        /**
         * 如果放入产品成功返回 true
         * @return
         */
        public synchronized boolean put() {
            if (buffer.notFull()) {
                buffer.putItem();
                return true;
            }
            return false;
        }
    
        /**
         * 如果取出产品成功返回 true
         * @return
         */
        public synchronized boolean get() {
            if (buffer.notEmpty()) {
                buffer.getItem();
                return true;
            }
            return false;
        }
    
        public String getStatus() {
            return "" + buffer;
        }
    }

     生产者 & 消费者:

    package entity.producerconsumer;
    
    public class Produc implements Runnable {
        /**
         * 统计生产者数量
         */
        private static int total = 0;
        /**
         * 生产者个体的 id
         */
        private int id;
    
        private Monition PC;
    
        public Produc(Monition monition) {
            id = ++total;
            PC = monition;
        }
    
        @Override
        public void run() {
            while (true) {
                if (PC.put()) {
                    // 如果操作成功打印缓冲区状态
                    System.out.println(id + " 号生产者: " + PC.getStatus());
                }
            }
        }
    }

    /

    package entity.producerconsumer;
    
    public class Consu implements Runnable {
        /**
         * 统计消费者数量
         */
        private static int total = 0;
        /**
         * 消费者个体的 id
         */
        private int id;
    
        private Monition PC;
    
        public Consu(Monition monition) {
            id = ++total;
            PC = monition;
        }
    
        @Override
        public void run() {
            while (true) {
                if (PC.get()) {
                    // 如果操作成功打印缓冲区状态
                    System.out.println(id + " 号消费者: " + PC.getStatus());
                }
            }
        }
    }

    /

    Buffer.java

    测试:

    package test;
    
    import entity.producerconsumer.Consu;
    import entity.producerconsumer.Monition;
    import entity.producerconsumer.Produc;
    
    public class MonitionTest {
        public static void main(String[] args) {
            Monition PC = new Monition(10);
            // 创建 5 个生产者和 5 个消费者
            for (int i = 0; i != 5; ++i) {
                new Thread(new Produc(PC)).start();
                new Thread(new Consu(PC)).start();
            }
        }
    }
  • 相关阅读:
    494. Target Sum 添加标点符号求和
    636. Exclusive Time of Functions 进程的执行时间
    714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
    377. Combination Sum IV 返回符合目标和的组数
    325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
    275. H-Index II 递增排序后的论文引用量
    274. H-Index论文引用量
    RabbitMQ学习之HelloWorld(1)
    java之struts2的数据处理
    java之struts2的action的创建方式
  • 原文地址:https://www.cnblogs.com/xkxf/p/7867042.html
Copyright © 2011-2022 走看看