当前位置:网站首页>[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 :
边栏推荐
- Antd comment recursive loop comment
- Kotlin Compose Text支持两种颜色
- 手机号国际区号JSON格式另附PHP获取
- Continuous learning of Robotics (Automation) - 2022-
- vim —- 自己主动的按钮indent该命令「建议收藏」
- map和set的实现
- [coded font series] opendyslexic font
- 【knife-4j 快速搭建swagger】
- 史上最全MongoDB之部署篇
- Do you choose pandas or SQL for the top 1 of data analysis in your mind?
猜你喜欢
Antd Comment 递归循环评论
[MySQL] row sorting in MySQL
Opencv third party Library
【刷题记录】2. 两数相加
【knife-4j 快速搭建swagger】
[coded font series] opendyslexic font
机械臂速成小指南(十):可达工作空间
Class constant pool and runtime constant pool
The most complete deployment of mongodb in history
Analysis on urban transportation ideas of 2022 Zhongqing cup C
随机推荐
手机号国际区号JSON格式另附PHP获取
[leetcode]Spiral Matrix II
AVL树插入操作与验证操作的简单实现
【编码字体系列】OpenDyslexic字体
Golang calculates constellations and signs based on birthdays
Operational amplifier application summary 1
Implementation of map and set
什么是 CGI,什么是 IIS,什么是VPS「建议收藏」
Mongo shell, the most complete mongodb in history
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
【knife-4j 快速搭建swagger】
UltraEdit-32 温馨提示:右协会,取消 bak文件[通俗易懂]
使用 Dumpling 备份 TiDB 集群数据到 GCS
Analysis on urban transportation ideas of 2022 Zhongqing cup C
史上最全MongoDB之部署篇
Unity3D在一建筑GL材料可以改变颜色和显示样本
Tflite model transformation and quantification
Antd Comment 递归循环评论
2022中青杯C题城市交通思路分析
[knife-4j quickly build swagger]