仓储模型
一、一个生产者,一个消费者
知识点:仓储模型
场景:多个生产者,多个消费者
需求:生产者线程不断的生产面包放入仓库,消费者线程不断的从仓库中取出面包,做到先生产的面包先卖出
分析:
面包类--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; } }