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

边栏推荐
- idea gradle lombok 报错集锦
- Using thread class and runnable interface to realize the difference between multithreading
- 运算放大器应用汇总1
- 使用 BR 恢复 GCS 上的备份数据
- golang 压缩和解压zip文件
- [written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
- 【系统管理】清理任务栏的已删除程序的图标缓存
- [OA] excel document generator: openpyxl module
- Pyqt5 out of focus monitoring no operation timer
- 科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
猜你喜欢

【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析

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

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

Food Chem|深度学习根据成分声明准确预测食品类别和营养成分

Implementation of map and set

Operational amplifier application summary 1

机械臂速成小指南(十):可达工作空间

2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析

Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)

Simple implementation of AVL tree insertion and verification operations
随机推荐
Food Chem|深度学习根据成分声明准确预测食品类别和营养成分
Golang calculates constellations and signs based on birthdays
一些常用软件相关
如何编写一个程序猿另一个面试官眼前一亮的简历[通俗易懂]
Tflite model transformation and quantification
别样肉客联手德克士在全国部分门店推出别样汉堡
[record of question brushing] 2 Add two numbers
二进制、八进制、十六进制
Use dumping to back up tidb cluster data to GCS
2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析
termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
pyqt5 失焦 监听无操作 定时器
Pyqt5 out of focus monitoring no operation timer
Enter the rough outline of the URL question (continuously updated)
Analysis on urban transportation ideas of 2022 Zhongqing cup C
【系统管理】清理任务栏的已删除程序的图标缓存
opencv第三方库
機器人(自動化)課程的持續學習-2022-
Restore backup data on GCS with br
Some thoughts on cross end development of kbone and applet