仓 储 模 型

仓储模型

一、一个生产者,一个消费者

知识点:仓储模型

场景:多个生产者,多个消费者

需求:生产者线程不断的生产面包放入仓库,消费者线程不断的从仓库中取出面包,做到先生产的面包先卖出

分析:

面包类--Cake

仓库类--Store

生产者线程类 -- Producer

消费者线程类 -- Consumer

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

public static void main(String[] args) {
        
        Store store = new Store();
        
        Producer p = new Producer(store);
        Consumer c = new Consumer(store);
        
        p.start();
        c.start();
    }
​

面包类--Cake

​
    public class Cake {
​
    private String brand;
    private String datetime;
    
    public Cake(String brand, String datetime) {
        this.brand = brand;
        this.datetime = datetime;
    }
​
    public String getBrand() {
        return brand;
    }
​
    public void setBrand(String brand) {
        this.brand = brand;
    }
​
    public String getDatetime() {
        return datetime;
    }
​
    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }
​
    @Override
    public String toString() {
        return brand + " -- " + datetime;
    }
    
    
}
​

仓库类--Store

​
public class Store {
    
    private static final int MAX_INIT_CAPACITY = 20;
​
    private int curCapacity;
    private int maxCapacity;
    private LinkedList<Cake> list;
    
    public Store() {
        this(MAX_INIT_CAPACITY);
    }
​
    public Store(int maxCapacity) {
        this.maxCapacity = maxCapacity;
        list = new LinkedList<>();
    }
​
    //入库 - 生产者线程不断的调用
    public synchronized void push(Cake cake){
        if(curCapacity>=MAX_INIT_CAPACITY){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        curCapacity++;
        list.add(cake);
        System.out.println("入库,当前容量为:" + curCapacity + " -- " + cake);
        notify();
    }
​
    //出库 - 消费者线程不断的调用
    public synchronized Cake pop(){
        if(curCapacity<=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        curCapacity--;
        Cake cake = list.removeFirst();
        System.out.println("出库,当前容量为:" + curCapacity + " -- " + cake);
        notify();
        return cake;
    }
}
​
​

生产者线程类 -- Producer

​
public class Producer extends Thread{
    
    private Store store;
    
    public Producer(Store store) {
        this.store = store;
    }
​
    @Override
    public void run() {
        while(true){
            Cake cake = new Cake("好利来", LocalDateTime.now().toString());
            store.push(cake);
        }
    }
}
​

消费者线程类 -- Consumer

​
public class Consumer extends Thread{
    
    private Store store;
    
    public Consumer(Store store) {
        this.store = store;
    }
​
    @Override
    public void run() {
        while(true){
            store.pop();
        }
    }
}
​
​

二、多个生产者多个消费者

public static void main(String[] args) {
​
        Store store = new Store();
​
        Producer p1 = new Producer(store);
        Producer p2 = new Producer(store);
        Consumer c1 = new Consumer(store);
        Consumer c2 = new Consumer(store);
​
        p1.start();
        p2.start();
        c1.start();
        c2.start();
    }

仓库类--Store

public class Store {
    
    private static final int MAX_INIT_CAPACITY = 20;
​
    private int curCapacity;
    private int maxCapacity;
    private LinkedList<Cake> list;
    
    public Store() {
        this(MAX_INIT_CAPACITY);
    }
​
    public Store(int maxCapacity) {
        this.maxCapacity = maxCapacity;
        list = new LinkedList<>();
    }
​
    //入库 - 生产者线程不断的调用
    public synchronized void push(Cake cake){
        while(curCapacity >= maxCapacity){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        curCapacity++;
        list.add(cake);
        System.out.println("入库,当前容量为:" + curCapacity);
        
        notifyAll();
    }
​
    //出库 - 消费者线程不断的调用
    public synchronized Cake pop(){
        while(curCapacity <= 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        curCapacity--;
        Cake cake = list.removeFirst();
        System.out.println("出库,当前容量为:" + curCapacity + " -- " + cake);
        notifyAll();
        return cake;
    }
}
​

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值