当前位置:网站首页>[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 :
边栏推荐
- 使用 Dumpling 备份 TiDB 集群数据到 GCS
- Collection of idea gradle Lombok errors
- Kbone与小程序跨端开发的一些思考
- EasyUI export excel cannot download the method that the box pops up
- [leetcode]Spiral Matrix II
- Highly paid programmers & interview questions. Are you familiar with the redis cluster principle of series 120? How to ensure the high availability of redis (Part 1)?
- Unity3D在一建筑GL材料可以改变颜色和显示样本
- Kotlin Android environment construction
- Class常量池与运行时常量池
- 使用 BR 恢复 GCS 上的备份数据
猜你喜欢
[development software] tilipa Developer Software
Class constant pool and runtime constant pool
用头像模仿天狗食月
2022中青杯C题城市交通思路分析
超越Postman,新一代国产调试工具Apifox,用起来够优雅
Class常量池与运行时常量池
Summer 2022 daily question 1 (1)
Antd comment recursive loop comment
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
随机推荐
使用Thread类和Runnable接口实现多线程的区别
web服务性能监控方案
POJ training plan 2253_ Frogger (shortest /floyd)
Unity3D在一建筑GL材料可以改变颜色和显示样本
The most complete deployment of mongodb in history
PHP 实现根据概率抽奖
Ssm+jsp realizes enterprise management system (OA management system source code + database + document +ppt)
中青杯2022A题高校数学建模竞赛与课程教育思路分析
Redis source code learning (30), dictionary learning, dict.h
Hongmi K40S root gameplay notes
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
Use dumping to back up tidb cluster data to GCS
Class constant pool and runtime constant pool
tflite模型转换和量化
Operational amplifier application summary 1
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析
pyqt5 失焦 监听无操作 定时器
ABAP 動態內錶分組循環
如何编写一个程序猿另一个面试官眼前一亮的简历[通俗易懂]