zoukankan      html  css  js  c++  java
  • 生产者消费者问题

    public class Depot {
        
        int capacity ;
        int size;
        public Depot(int capacity){
            this.capacity = capacity;
            this.size = 0;
        }
        
        public synchronized void produce(int val){
            try{
                int left = val;//val可能 > capacity,因此需要多次生产
                while(left > 0){//
                    while(size >= capacity){//当库存已满时,生产者需要将自己挂起,并唤醒消费者,让消费者来消费。
                        wait();
                    }
                    int inc = (size + left) > capacity ? (capacity - size) : left;//每次生产的数量与capacity有关
                    size += inc;
                    left -= inc;
                    System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d
    ", Thread.currentThread().getName(), val, left, inc, size);
                    notifyAll();//唤醒消费者来消费
                }
            }catch(Exception e){
            }
        }
        
        public synchronized void consum(int val){
            try{
                int left = val;
                while(left > 0){
                    while(size <= 0){//当库存为0时,消费者将自己挂起,通知生产者生产
                        wait();
                    }
                    int dec = (size > left) ? left : size;//同上
                    size -= dec;
                    left -= dec;
                    System.out.printf("%s consume(%3d) --> left=%3d, dec=%3d, size=%3d
    ", Thread.currentThread().getName(), val, left, dec, size);
                    notifyAll();
                }
            }catch(Exception e){}
        }
        
    }

    生产者& 消费者对工厂的访问:

    public class ConsumerAndProducer {
        public static void main(String[] args) {
            Depot depot = new Depot(100);
            Producer pro = new Producer(depot);
            Consumer con = new Consumer(depot);
            pro.produce(120);
            pro.produce(150);
            con.consum(110);
            con.consum(120);
        }
    }
    
    class Consumer {
        Depot depot;
        public Consumer(Depot depot){
            this.depot = depot;
        }
        public void consum(final int val){
            new Thread(){
                public void run(){
                    depot.consum(val);
                }
            }.start();
        }
    }
    class Producer{
        Depot depot;
        public Producer(Depot depot){
            this.depot = depot;
        }
        public void produce(final int val){
            new Thread(){
                public void run(){
                    depot.produce(val);
                }
            }.start();
        }
    }

    多线程相关

  • 相关阅读:
    .NETframework的EF框架学习报错之datetime 数据类型
    String...的用法
    存储过程从入门到熟练(c#篇)
    售前如何做好产品演示
    华为演讲培训售前人员重点学习
    report services 报表开发和部署,集成到解决方案中 全解析
    在Asp.net用C#建立动态Excel(外文翻译)
    NET(C#)连接各类数据库集锦
    在SourceForge.net上如何使用TortoiseCVS
    用C#实现在线升级
  • 原文地址:https://www.cnblogs.com/huntfor/p/3986266.html
Copyright © 2011-2022 走看看