当前位置:网站首页>[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 :
边栏推荐
- Use br to back up tidb cluster to GCS
- Storage of data
- Hongmi K40S root gameplay notes
- Unity3d can change colors and display samples in a building GL material
- Adaptive non European advertising retrieval system amcad
- Golang compresses and decompresses zip files
- Class constant pool and runtime constant pool
- SSM+jsp实现仓库管理系统,界面那叫一个优雅
- How to detect whether the MySQL code runs deadlock +binlog view
- Collection of idea gradle Lombok errors
猜你喜欢
CUDA Programming
Summer 2022 daily question 1 (1)
Ggplot facet detail adjustment summary
Antd comment recursive loop comment
【编码字体系列】OpenDyslexic字体
Gpt-3 is a peer review online when it has been submitted for its own research
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
【OA】Excel 文档生成器: Openpyxl 模块
数据的存储
[development software] tilipa Developer Software
随机推荐
web服务性能监控方案
Redis源码学习(31),字典学习,dict.c(一)
史上最全MongoDB之初识篇
ggplot 分面的细节调整汇总
DAB-DETR: DYNAMIC ANCHOR BOXES ARE BETTER QUERIES FOR DETR翻译
Unity3d can change colors and display samples in a building GL material
Create commonly used shortcut icons at the top of the ad interface (menu bar)
Gpt-3 is a peer review online when it has been submitted for its own research
2022中青杯数学建模B题开放三孩背景下的生育政策研究思路
史上最全学习率调整策略lr_scheduler
红米k40s root玩机笔记
Restore backup data on GCS with br
How to detect whether the MySQL code runs deadlock +binlog view
cuda编程
手机号国际区号JSON格式另附PHP获取
学习使用js把两个对象合并成一个对象的方法Object.assign()
Redis source code learning (30), dictionary learning, dict.h
Learn how to use js to merge two objects into one object assign()
List interview common questions
Adaptive non European advertising retrieval system amcad