当前位置:网站首页>Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
2022-07-04 06:54:00 【JDSYDWR】
One 、 Producer consumer model
Producer consumer pattern is a common pattern in multithreading , What he mainly realizes is how much resources the producer produces , How many resources can consumers consume , When resources are insufficient, consumers need to wait and inform producers to consume , And the producer after the production resources reach the specified limit , We must stop production and wait for consumers to consume , As long as the resource limit is not reached , Producers can produce at any time .
Two 、 Why? wait() Use while Make judgments instead of if Do judgment
1.wait Characteristics of the method
First , We need to know wait() The lock will be released after execution , It can be understood as resetting the lock , That is, the current thread is still waiting , But you can let the next thread enter the lock .
2. Why use while Make judgments instead of if Do judgment
First , If we use if Do judgment , When the first thread enters, it passes if Execute after judgment wait() Wait and release the lock , Then the next thread may also enter and pass if Execute after judgment wait Method waits and releases the lock , Then we will have multiple threads waiting in the same place , When we wake up the thread , Because all threads are wait() It has passed before if Judge , So all threads can execute directly down , This makes us unable to appear One production, one consumption , So there's a problem .
But when we use while When making judgments , When we wake up all threads , Then the first one out while The thread of the loop will modify while Judgment condition , Other threads want to start from while If you come out of the cycle, you need to judge again , Because the condition has been modified by the first thread , Then naturally, I can't get out , So other threads will continue to execute wait() Waiting for the next wake up .
3. My other way of thinking
Personal understanding may not be correct , Please comment and testify if there is any mistake .
I think if I wake up the thread , Don't use this.notifyAll() Wake up all threads , It's just using this.notify() Wake up a thread randomly , So actually use while and if It can be achieved by making judgments One production, one consumption The request has been made. .
边栏推荐
- The important role of host reinforcement concept in medical industry
- Mysql 45讲学习笔记(七)行锁
- Mysql 45讲学习笔记(十四)count(*)
- 【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
- Is the insurance annuity product worth buying? Is there a hole?
- Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
- Another company raised the price of SAIC Roewe new energy products from March 1
- tars源码分析之8
- 在已经知道表格列勾选一个显示一列
- uniapp 自定義環境變量
猜你喜欢
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
【网络数据传输】基于FPGA的百兆网/兆网千UDP数据包收发系统开发,PC到FPGA
GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
Wechat applet scroll view component scrollable view area
A new understanding of how to encrypt industrial computers: host reinforcement application
MySQL 45 lecture learning notes (VII) line lock
Uniapp applet subcontracting
Selenium ide plug-in download, installation and use tutorial
图的底部问题
uniapp小程序分包
随机推荐
tars源码分析之4
Selection (021) - what is the output of the following code?
Another company raised the price of SAIC Roewe new energy products from March 1
Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
[backpack DP] backpack problem
The sorting in C language realizes the number sorting method from small to large
Chapter 1 programming problems
selenium IDE插件下载安装使用教程
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
The final week, I split
About how idea sets up shortcut key sets
How does the recv of TCP socket receive messages of specified length?
【FPGA教程案例7】基于verilog的计数器设计与实现
centos8安装mysql.7 无法开机启动
How to input single quotation marks and double quotation marks in latex?
tars源码分析之6
Flink memory model, network buffer, memory tuning, troubleshooting
Shopping malls, storerooms, flat display, user-defined maps can also be played like this!
Deep profile data leakage prevention scheme
Common usage of time library