Java之仓储模型

仓储模型

仓储模型也是一种多线程共享资源的解决方案,但是设计实现有别于生产者消费者模型

场景: 一个生产者线程不断的生产面包放入仓库

​ 一个消费者线程不断的从仓库中取出面包

​ 做到先生产的面包先卖出

分析: 生产者线程、消费者线程、面包类、仓库类

​ 先生产的面包先卖出–队列模式(LinkedList)

Demo类

package store;

public class Demo {

    public static void main(String[] args) {

        Store store = new Store();

        Producer p = new Producer(store);
        Consumer c = new Consumer(store);

        p.start();
        c.start();
    }
}

Product类

package store;

import java.util.Date;

public class Product {

    private String name;
    private String productionDate;

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", productionDate='" + productionDate + '\'' +
                '}';
    }

    public Product(String name, String productionDate) {
        this.name = name;
        this.productionDate = productionDate;
    }
}

Store类

package store;

import java.util.LinkedList;

public class Store {

    private static final int MAX_INIT_CAPACITY = 20;

    private int currentCapacity;
    private int maxCapacity;
    private LinkedList<Product> list;


    public Store() {
        this(MAX_INIT_CAPACITY);
    }

    public Store(int maxCapacity) {
        this.maxCapacity = maxCapacity;
        list = new LinkedList<>();
    }

    //添加产品
    public synchronized void add(Product product) {
        //判断是否生产超容量
        if (currentCapacity >= maxCapacity) {
            try {
                //如果超出容量,就进入阻塞状态
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        currentCapacity++;
        list.add(product);
        System.out.println("入库,当前容量:" + currentCapacity);
        //唤醒消费者线程
        this.notify();
    }

    //取出产品
    public synchronized Product remove() {
        //判断是否生产超容量
        if (currentCapacity <= 0) {
            try {
                //如果没有产品,就进入阻塞状态
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        currentCapacity--;
        Product product = list.removeFirst();
        System.out.println("出库,当前容量:" + currentCapacity + product);
        //唤醒生产者线程
        this.notify();
        return product;
    }
}

Producer类

package store;

import java.time.LocalDate;

public class Producer extends Thread{

    private Store store;

    public Producer(Store store) {
        this.store = store;
    }

    @Override
    public void run() {
        while(true){
            Product product = new Product("小米", LocalDate.now().toString());
            store.add(product);
        }
    }
}

Consumer类

package store;

public class Consumer extends Thread{

    private Store store;

    public Consumer(Store store) {
        this.store = store;
    }

    @Override
    public void run() {
        while(true){
            store.remove();
        }
    }
}

场景: 多个生产者线程不断的生产面包放入仓库

​ 多个消费者线程不断的从仓库中取出面包

​ 做到先生产的面包先卖出

分析: 生产者线程、消费者线程、面包类、仓库类

​ 先生产的面包先卖出–队列模式(LinkedList)

Stroe类

package store;

import java.util.LinkedList;

public class Store {

    private static final int MAX_INIT_CAPACITY = 20;

    private int currentCapacity;
    private int maxCapacity;
    private LinkedList<Product> list;


    public Store() {
        this(MAX_INIT_CAPACITY);
    }

    public Store(int maxCapacity) {
        this.maxCapacity = maxCapacity;
        list = new LinkedList<>();
    }

    //添加产品
    public synchronized void add(Product product) {
        //判断是否生产超容量
        while (currentCapacity >= maxCapacity) {
            try {
                //如果超出容量,就进入阻塞状态
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        currentCapacity++;
        list.add(product);
        System.out.println("入库,当前容量:" + currentCapacity);
        //唤醒消费者线程
        this.notifyAll();
    }

    //取出产品
    public synchronized Product remove() {
        //判断是否生产超容量
        while (currentCapacity <= 0) {
            try {
                //如果没有产品,就进入阻塞状态
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        currentCapacity--;
        Product product = list.removeFirst();
        System.out.println("出库,当前容量:" + currentCapacity + product);
        //唤醒生产者线程
        this.notifyAll();
        return product;
    }
}

仓储模型和生产者消费者模型的区别

仓储模型:将锁的逻辑放到了线程外 –> Stroe类

消费者生产者:将锁的逻辑放在了线程中 –> Producer线程类、Consumer线程类

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拖把湛屎,戳谁谁死

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值