Java實現簡易生產者消費者模型過程解析
一、概述
一共兩個線程,一個線程生產產品,一個線程消費產品,使用同步代碼塊方法,同步兩個線程。當產品沒有時,通知生產者生產,生產者生產后,通知消費者消費,并等待消費者消費完。
需要注意的是,有可能出現,停止生產產品后,消費者還沒未來得及消費生產者生產的最后一個產品,就結束消費,導致最后一個產品沒有被消費。
本例使用synchronize以及wait()、notify()實現簡易版的線程者消費者模型。
二、測試用例
這里的產品用筆來演示,每只筆都有其編號code
一共有四個類:分別是生產者類,產品類,消費者類,測試類

產品
package test.exception.producer_consumer_model;
/*
假設為產品為筆
*/
public class Production {
private String type = "";
private String color = "";
private long code = 0; // 產品編號
private boolean isProduced = false; // 是否生產完成 初始狀態(tài)為未生產狀態(tài)
private boolean isContinueProduce = true; // 是否停產該產品
public void setContinueProduce(boolean continueProduce) {
isContinueProduce = continueProduce;
}
public void setCode(long code) {
this.code = code;
}
public Production(){
}
public boolean isContinueProduce() {
return isContinueProduce;
}
public void setType(String type) {
this.type = type;
}
public void setColor(String color) {
this.color = color;
}
public void setProduced(boolean produced) {
isProduced = produced;
}
public boolean isProduced() {
return isProduced;
}
@Override
public String toString() {
return color + type + "-" + code;
}
}
生產者
package test.exception.producer_consumer_model;
public class Producer implements Runnable {
private final Production pen; // 產品
public Producer(Production pen) {
this.pen = pen;
}
// 生產
public void produce() {
long code = 0;
while (this.pen.isContinueProduce()) {
synchronized (this.pen) {
if (this.pen.isProduced()) {
try {
this.pen.wait(); // 等待消費者消費
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 開始生產
this.pen.setType("鉛筆");
this.pen.setColor("藍色");
this.pen.setCode(code++);
this.pen.setProduced(true);
System.out.println(this.pen + " is produced");
this.pen.notify();
}
}
System.out.println("finish producing");
}
@Override
public void run() {
produce();
}
}
消費者
package test.exception.producer_consumer_model;
public class Consumer implements Runnable {
private final Production pen;
public Consumer(Production pen) {
this.pen = pen;
}
// 持續(xù)消費
public void consumer() {
while (this.pen.isContinueProduce()) {
synchronized (this.pen) {
if (!this.pen.isProduced()) {
try {
this.pen.wait(); // 等待生產者生產
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.pen + " is consumed"); // 使用
this.pen.setProduced(false); // 使用完后更新狀態(tài)
this.pen.notify();
}
}
// 確保停止生產后,能夠使用最后生產的一支筆
if (this.pen.isProduced()) {
System.out.println(this.pen + " is consumed");
}
System.out.println("finish using");
}
@Override
public void run() {
consumer();
}
}
主線程測試
package test.exception.producer_consumer_model;
public class Demo {
public static void main(String[] args) throws InterruptedException {
Production pen = new Production();
Consumer consumer = new Consumer(pen);
Producer producer = new Producer(pen);
new Thread(producer).start(); // 開啟生產者線程
new Thread(consumer).start(); // 開啟消費者線程
Thread.sleep(10000);
pen.setContinueProduce(false); // 10s后停止生產該類型的筆
}
}
運行結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯
這篇文章主要為大家介紹了Spring?Security密碼解析器PasswordEncoder自定義登錄邏輯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Java動態(tài)代理的兩種實現方式詳解【附相關jar文件下載】
這篇文章主要介紹了Java動態(tài)代理的兩種實現方式,結合實例形式分析了java動態(tài)代理的相關原理、實現方法與操作技巧,并附帶相關jar文件供讀者下載,需要的朋友可以參考下2019-03-03
SpringBoot集成EasyExcel實現Excel導入的方法
這篇文章主要介紹了SpringBoot集成EasyExcel實現Excel導入的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
java集合PriorityQueue優(yōu)先級隊列方法實例
這篇文章主要為大家介紹了java集合PriorityQueue優(yōu)先級隊列方法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

