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();
            }
        }
    }
  • 相关阅读:
    TimesTen ODBC 链接库差异及相关命令行工具的使用注意事项
    Makefile当中宏定义传递字符串
    WTL 中的常见问题汇总
    WTL 中CComboBoxEx显示不了的问题
    error C2664 转换错误汇总[转]
    LNK1123: 转换到 COFF 期间失败: 文件无效或损坏[汇总]
    python 安装mysql 客户端遇到的问题
    统计查询基本信息
    使用Log4net记录日志(非常重要)
    EntityFramework中使用sql语句
  • 原文地址:https://www.cnblogs.com/xkxf/p/7867042.html
Copyright © 2011-2022 走看看