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

边栏推荐
- 你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
- Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
- Triple half circle progress bar, you can use it directly
- 用头像模仿天狗食月
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
- 运算放大器应用汇总1
- 使用 Dumpling 备份 TiDB 集群数据到 GCS
- Antd Comment 递归循环评论
- 接口自动化测试实践指导(中):接口测试场景有哪些
- [MySQL] row sorting in MySQL
猜你喜欢

Antd Comment 递归循环评论

SSM+jsp实现仓库管理系统,界面那叫一个优雅

Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
![[hcie TAC] question 3](/img/d2/38a7286be7780a60f5311b2fcfaf0b.jpg)
[hcie TAC] question 3

MySQL data loss, analyze binlog log file

Class常量池与运行时常量池

Tflite model transformation and quantification
![[MySQL] row sorting in MySQL](/img/97/8a451fa62796838e11242c86eecd8d.png)
[MySQL] row sorting in MySQL

Summer 2022 daily question 1 (1)

5年自动化测试,终于进字节跳动了,年薪30w其实也并非触不可及
随机推荐
cuda编程
HW notes (II)
[OA] excel document generator: openpyxl module
2022 electrician cup question B analysis of emergency materials distribution under 5g network environment
手机号国际区号JSON格式另附PHP获取
The JSON format of the international area code of the mobile phone number is obtained with PHP
The first introduction of the most complete mongodb in history
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
Use br to back up tidb cluster to GCS
UltraEdit-32 warm prompt: right association, cancel bak file [easy to understand]
Some common software related
力扣------路径总和 III
Quick completion guide of manipulator (10): accessible workspace
Redis source code learning (31), dictionary learning, dict.c (1)
NanopiNEO使用开发过程记录
【编码字体系列】OpenDyslexic字体
PHP 实现根据概率抽奖
PHP implements lottery according to probability
如何编写一个程序猿另一个面试官眼前一亮的简历[通俗易懂]
[coded font series] opendyslexic font