仓储模型
仓储模型也是一种多线程共享资源的解决方案,但是设计实现有别于生产者消费者模型
场景: 一个生产者线程不断的生产面包放入仓库
一个消费者线程不断的从仓库中取出面包
做到先生产的面包先卖出
分析: 生产者线程、消费者线程、面包类、仓库类
先生产的面包先卖出–队列模式(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线程类