当前位置:网站首页>[multi threading exercise] write a multi threading example of the producer consumer model.
[multi threading exercise] write a multi threading example of the producer consumer model.
2022-07-07 04:16:00 【aigo-2021】
Write a producer - Multithreading example of consumer mode .
( Maintain a balance between production and consumption resources )
public class Test {
private int count;// requirement count Stay in 10
public synchronized void add(){
// Producer thread , When count>10, Let the producer thread wait , Wake up consumer thread
while (count>10){
try {
this.wait();// wait for , The following code can only be executed after being awakened
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
this.notifyAll();
System.out.println(" Produced a commodity , Inventory surplus :"+count);
}
public synchronized void sub(){
// Consumer thread , When count<10, Let the consumer thread wait , Wake up the producer thread
while (count<10){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
this.notifyAll();
System.out.println(" Sold a commodity , Inventory surplus :"+count);
}
public static void main(String[] args) {
Test t=new Test();
ProductRunnable pr=new ProductRunnable(t);
ConsummerRunnable cr=new ConsummerRunnable(t);
new Thread(pr).start();
new Thread(pr).start();
new Thread(pr).start();
new Thread(pr).start();
new Thread(cr).start();
new Thread(cr).start();
new Thread(cr).start();
new Thread(cr).start();
new Thread(cr).start();
new Thread(cr).start();
}
private static class ProductRunnable implements Runnable{
private Test t;
public ProductRunnable(Test t){
this.t = t;
}
public void run(){// Thread method
while(true){
t.add();// Add goods to the warehouse
try {
Thread.sleep(1000);// Set thread rest 1s
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private static class ConsummerRunnable implements Runnable{
private Test t;
public ConsummerRunnable(Test t){
this.t = t;
}
public void run(){
while(true){
t.sub();
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}Running results :

边栏推荐
猜你喜欢
![[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis](/img/82/8f5b6f388d5676cb7ff902ba80d9d2.jpg)
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis

Kbone与小程序跨端开发的一些思考

【刷题记录】2. 两数相加

idea gradle lombok 报错集锦

2022中青杯C题城市交通思路分析

SSM+JSP实现企业管理系统(OA管理系统源码+数据库+文档+PPT)

【写给初发论文的人】撰写综述性科技论文常见问题

Web service performance monitoring scheme

Continuous learning of Robotics (Automation) - 2022-

你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
随机推荐
Food Chem|深度学习根据成分声明准确预测食品类别和营养成分
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
Implementation of binary search tree
Web service performance monitoring scheme
红米k40s root玩机笔记
使用Thread类和Runnable接口实现多线程的区别
5年自动化测试,终于进字节跳动了,年薪30w其实也并非触不可及
[coded font series] opendyslexic font
NoSQL之Redis配置与优化
2022中青杯C题城市交通思路分析
Storage of data
Enter the rough outline of the URL question (continuously updated)
機器人(自動化)課程的持續學習-2022-
UltraEdit-32 温馨提示:右协会,取消 bak文件[通俗易懂]
2022中青杯数学建模B题开放三孩背景下的生育政策研究思路
杭州电 3711 Binary Number
ABAP 动态内表分组循环
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
史上最全学习率调整策略lr_scheduler
HW notes (II)