美文网首页Java笔记
Java生产消费问题

Java生产消费问题

作者: 妍倩倩 | 来源:发表于2016-09-23 14:47 被阅读43次

    生产者

    public class Producer {
        
        private Depot depot;
        
        public Producer(Depot depot) {
            this.depot = depot;
        }
        
        public void produce(final int val) {
            new Thread() {
                public void run() {
                    depot.produce(val);
                }
            }.start();
        }
    
    }
    

    消费者

    public class Consumer {
        
        private Depot depot;
        
        public Consumer(Depot depot) {
            this.depot = depot;
        }
        
        public void consume(final int val) {
            new Thread() {
                public void run() {
                    depot.consume(val);
                }
            }.start();
        }
    
    }
    

    仓库类

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Depot {
        
        private int capacity;//仓库容量
        
        private int size;//仓库当前存量
        
        private Lock lock;//独占锁
        
        private Condition fullCondition;//生产条件
        
        private Condition emptyCondition;//消费条件
        
        public Depot() {
            this.capacity = 100;
            this.size = 0;
            this.lock = new ReentrantLock();
            this.fullCondition = lock.newCondition();
            this.emptyCondition = lock.newCondition();
        }
        
        public Depot(int capacity) {
            this.capacity = capacity;
            this.size = 0;
            this.lock = new ReentrantLock();
            this.fullCondition = lock.newCondition();
            this.emptyCondition = lock.newCondition();
        }
        
        public void produce(int val) {
            lock.lock();
            try{
                int left = val;
                while(left > 0) {
                    //库存已满,等待生产
                    while(size >= capacity) {
                        fullCondition.await();
                    }
                    //仓库当前容量+要生产的数量>仓库容量,则需要生产的数量为仓库容量-当前仓库的库存量
                    //否则,需要生产的数量为传入的生产量
                    int inc = (size + left) > capacity? capacity - size : left;//实际生产的数量
                    size += inc;//当前库存
                    left -= inc;//还需要生产的数量
                    System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d\n", 
                             Thread.currentThread().getName(), val, left, inc, size);
                    emptyCondition.signal();//通知消费
                }
    //          emptyCondition.signal();//放在此处当size=capacity时,生产线程一直等待,且无法唤醒消费线程
            }catch(InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
        
        public void consume(int val) {
            lock.lock();
            try{
                int left = val;
                while(left > 0) {
                    //当没有库存量时,等待消费
                    while(size <= 0) {
                        emptyCondition.await();
                    }
                    //库存量大于需要消费的数量时,则消费需要消费的数量
                    //否则,消费当前的库存量
                    int dec = size > left? left : size;//消费的数量
                    size -= dec;//消费后的库存
                    left -= dec;//还需消费的数量
                    System.out.printf("%s consume(%3d) <-- left=%3d, dec=%3d, size=%3d\n", 
                            Thread.currentThread().getName(), val, left, dec, size);
                    fullCondition.signal();//通知生产
                }
    //          fullCondition.signal();//放在此处当size=0时,消费一直等待,且无法唤醒生产线程
            }catch(InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();  
            }
        }
    
    }
    

    测试代码

    public static void main(String[] args) {
            Depot depot = new Depot(100);
            Producer producer = new Producer(depot);
            Consumer consumer = new Consumer(depot);
            
            producer.produce(50);
            consumer.consume(70);
            producer.produce(120);
            consumer.consume(30);
            
        }
    

    打印结果

    Thread-0 produce( 50) --> left=  0, inc= 50, size= 50
    Thread-1 consume( 70) <-- left= 20, dec= 50, size=  0
    Thread-2 produce(120) --> left= 20, inc=100, size=100
    Thread-1 consume( 70) <-- left=  0, dec= 20, size= 80
    Thread-2 produce(120) --> left=  0, inc= 20, size=100
    Thread-3 consume( 30) <-- left=  0, dec= 30, size= 70
    

    相关文章

      网友评论

        本文标题:Java生产消费问题

        本文链接:https://www.haomeiwen.com/subject/zmmtyttx.html