当前位置:网站首页>[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 :

边栏推荐
- 2022 middle school Youth Cup mathematical modeling question B fertility policy research ideas under the background of open three children
- [OA] excel document generator: openpyxl module
- UltraEdit-32 温馨提示:右协会,取消 bak文件[通俗易懂]
- Learn how to use js to merge two objects into one object assign()
- termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
- 再AD 的 界面顶部(菜单栏)创建常用的快捷图标
- 使用 TiDB Lightning 恢复 GCS 上的备份数据
- Implementation of binary search tree
- 使用 BR 备份 TiDB 集群到 GCS
- PHP implements lottery according to probability
猜你喜欢

map和set的实现
![[system management] clear the icon cache of deleted programs in the taskbar](/img/cc/7aff85f1a33ef390623652eadb6f03.png)
[system management] clear the icon cache of deleted programs in the taskbar

Gpt-3 is a peer review online when it has been submitted for its own research

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

tflite模型转换和量化

机器人(自动化)课程的持续学习-2022-

Food Chem|深度学习根据成分声明准确预测食品类别和营养成分
![[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

史上最全学习率调整策略lr_scheduler

Some common software related
随机推荐
golang 根据生日计算星座和属相
[system management] clear the icon cache of deleted programs in the taskbar
Force buckle ----- path sum III
Class常量池与运行时常量池
【编码字体系列】OpenDyslexic字体
[hcie TAC] question 3
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
Summer 2022 daily question 1 (1)
什么是 CGI,什么是 IIS,什么是VPS「建议收藏」
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
Redis源码学习(30),字典学习,dict.h
Operational amplifier application summary 1
Hangzhou Electric 3711 binary number
Web service performance monitoring scheme
Redis configuration and optimization of NoSQL
termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
接口自动化测试实践指导(中):接口测试场景有哪些
opencv第三方库
How to detect whether the MySQL code runs deadlock +binlog view
Ggplot facet detail adjustment summary