美文网首页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生产消费问题

    生产者 消费者 仓库类 测试代码 打印结果

  • 操作系统知识点持续更新

    生产者消费者问题 关于生产者消费者问题可以参考这篇文章:生产者消费者问题的java实现 临界区与互斥量 临界区:保...

  • 【Java并发】如何实现非阻塞式生产者消费者?

    1.问题描述 实现Java非阻塞式生产者消费者,用来解决,生产和消费对于资源访问不同步和造成资源冗余的问题 2.实...

  • Java 实现生产者和消费者的三种方法

    Java 实现生产者和消费者的三种方法 生产者消费者模型是我们讨论同步问题里面必须要学习的,生产者和消费者公用一块...

  • spring cloud stream kafka实例

    maven 生产者配置 java代码 消费者 java代码 运行 先运行生产者,再运行消费者 doc Apache...

  • java笔记--多生产多消费问题

    单一生产者,消费者问题: 多生产多消费问题: 如果直接创建多个线程,会出现生产一次,却消费多次的冲突,或者生产多个...

  • 经典同步互斥问题

    生产者消费者问题 生产者消费者应当是最最基本的同步互斥问题了。生产者生产了之后消费者才消费,消费者消费之后,通知生...

  • Java生产者/消费者模型的一种实现

    本文主要介绍java中生产者/消费者模式的实现,对java线程锁机制的一次深入理解。 生产者/消费者模型 生产者/...

  • 生产者和消费者问题

    生产者和消费者问题 问题简述 组成 系统中有一组生产者和消费者进程 生产者生产者将产品放入缓冲区 消费者消费者进程...

  • 2018-09-04

    生产消费模型 Timer.java 多生产、单消费、支持任务优先级、周期定时 如何响应插入高优Task?queue...

网友评论

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

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