生产者消费者模型是Java基础中非常经典的一个多线程程序。在Java初级工程师的面试中也会经常被问到或者会在面试题出现。之前实现了一个没有带缓冲区的生产者消费者模型。缺点就是只能生产一个立马消费一个。没有仓库进行储存。今天无事,就要实现了一个带仓库的生产者消费者模型。
import java.util.LinkedList;
/**
* @author Hiseico
* @create 2018-05-08 16:10
* @desc 带缓冲区的生产者消费者模型
**/
class Stronge {
//仓库最大容量
private final int MAX_SIZE = 100;
// 仓库载体
LinkedList<Object> list = new LinkedList<Object>();
// 生产方法
public void product(int num) {
//同步代码块
synchronized (list) {
//判断生产后是否会爆仓
while (list.size() > num) {
System.out.println("仓库超载!现仓库库存:" + this.list.size() + ",若生产后仓库库存" + this.list.size() + num);
try {
//若满足条件,线程等待
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始生产
System.out.print("先仓库库存:" + this.list.size() + "开始生产====>");
for (int i = 0; i < num; i++) {
list.add(new Object());
}
System.out.println("生产" + num + "个,先仓库总量:" + this.list.size());
//唤醒所有等待的线程
list.notifyAll();
}
}
//消费方法
public void custom(int num) {
// 同步代码块
synchronized (this.list) {
// 如果仓库中的数量不足
while (this.list.size() < num) {
System.out.println("库存剩余:" + this.list.size() + ",预计取出:" + num + "个,库存不足");
//不满足消费条件,线程等待
try {
this.list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 判读消费条件,消费个数小于仓库库存数量
for (int i = 0; i < num; i++) {
list.remove();
}
System.out.println("消费数量:" + num + ",现在剩余库存:" + this.list.size());
// 唤醒其他等待线程
this.list.notifyAll();
}
}
}
//生产者
class Producter implements Runnable {
//仓库对象
private Stronge stronge;
//生产的数量
private int num;
public void setNum(int num) {
this.num = num;
}
//有参构造方法,传入仓库对象,之后可在该对象中对仓库中的生产方法进行操作
public Producter(Stronge stronge) {
this.stronge = stronge;
}
//出入生产数量,调用仓库对象中的生产方法
private void prodcut(int num) {
this.stronge.product(num);
}
//实现多线程的run()方法
public void run() {
//调用该类中的生产方法
this.prodcut(num);
}
}
//消费者
class Customered implements Runnable {
// 仓库对象
private Stronge stronge;
// 消费数量
private int num;
//有参构造方法, 传入仓库对象,之后可在该对象中对仓库中的消费方法进行操作
public Customered(Stronge stronge) {
this.stronge = stronge;
}
//出入消费数量,调用仓库对象中的消费方法
private void customer(int num) {
this.stronge.custom(num);
}
//实现多线程的run()方法
public void run() {
//调用该类中的消费方法
this.customer(num);
}
public void setNum(int num) {
this.num = num;
}
}
public class P_C_Demo {
public static void main(String[] args) {
//实例化仓库对象
Stronge stronge = new Stronge();
//实例化生产者对象
Producter producter = new Producter(stronge);
//设置该生产者的生产数量
producter.setNum(100);
//实例化生产者对象
Producter producter1 = new Producter(stronge);
//设置该生产者的生产数量
producter1.setNum(100);
//实例化消费者对象
Customered customered = new Customered(stronge);
//设置该消费者的消费数量
customered.setNum(90);
//启动多线程
new Thread(producter).start();
new Thread(customered).start();
new Thread(producter).start();
}
}