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