当前位置:网站首页>[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 electrician cup a question high proportion wind power system energy storage operation and configuration analysis ideas
- 一些常用软件相关
- NoSQL之Redis配置与优化
- 高薪程序员&面试题精讲系列120之Redis集群原理你熟悉吗?如何保证Redis的高可用(上)?
- Analysis on urban transportation ideas of 2022 Zhongqing cup C
- 使用切面实现记录操作日志
- Collection of idea gradle Lombok errors
- Hangzhou Electric 3711 binary number
- POJ培训计划2253_Frogger(最短/floyd)
猜你喜欢
史上最全学习率调整策略lr_scheduler
Simple implementation of AVL tree insertion and verification operations
DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
Antd comment recursive loop comment
运算放大器应用汇总1
5年自动化测试,终于进字节跳动了,年薪30w其实也并非触不可及
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
Tflite model transformation and quantification
Mongo shell, the most complete mongodb in history
Web service performance monitoring scheme
随机推荐
【刷题记录】2. 两数相加
如何编写一个程序猿另一个面试官眼前一亮的简历[通俗易懂]
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
Ggplot facet detail adjustment summary
Create commonly used shortcut icons at the top of the ad interface (menu bar)
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
Binary, octal, hexadecimal
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Hongmi K40S root gameplay notes
使用Thread类和Runnable接口实现多线程的区别
史上最全学习率调整策略lr_scheduler
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)?
List interview common questions
Hangzhou Electric 3711 binary number
POJ training plan 2253_ Frogger (shortest /floyd)
Learn how to use js to merge two objects into one object assign()
Use dumping to back up tidb cluster data to GCS
[leetcode]Spiral Matrix II
Golang compresses and decompresses zip files
pyqt5 失焦 监听无操作 定时器