java多线程生产者消费者模型
利用缓冲区解决:管程法
public class TestPCDemo {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
Thread pThread = new Thread(new Producer(synContainer));
Thread cThread = new Thread(new Consumer(synContainer));
pThread.start();
cThread.start();
}
}
class Producer implements Runnable {
SynContainer container;
public Producer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.addProduction(new Production(i));
System.out.println("生产了第" + i + "个产品");
}
}
}
class Consumer implements Runnable {
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了第" + container.popProduction().id + "个产品");
}
}
}
class Production {
public int id;
public Production(int id) {
this.id = id;
}
}
class SynContainer {
Production[] productions = new Production[10];
int count = 0;
public synchronized void addProduction(Production p) {
if (count == productions.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
productions[count] = p;
++count;
this.notifyAll();
}
public synchronized Production popProduction() {
if (count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Production production = productions[count];
this.notifyAll();
return production;
}
}
利用标志位解决:红绿灯法
public class TestPCDemo2 {
public static void main(String[] args) {
Production production = new Production();
Producer producer = new Producer(production);
Consumer consumer = new Consumer(production);
new Thread(producer).start();
new Thread(consumer).start();
}
}
class Producer implements Runnable {
Production production;
public Producer(Production production) {
this.production = production;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
this.production.produce("第"+i+"个产品");
}
}
}
class Consumer implements Runnable {
Production production;
public Consumer(Production production) {
this.production = production;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
this.production.consume();
}
}
}
class Production {
public String production;
boolean flag = true;
public synchronized void produce(String production) {
if (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产者生产了" + production);
this.production = production;
this.notifyAll();
this.flag = !this.flag;
}
public synchronized void consume() {
if (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者消费了 --> " + production);
this.notifyAll();
this.flag = !this.flag;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)