当前位置:网站首页>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. .
边栏推荐
- Fundamentals of SQL database operation
- Software keywords and process information intercepted by Golden Shield video player
- What is the use of cloud redis? How to use cloud redis?
- 【FPGA教程案例7】基于verilog的计数器设计与实现
- A new understanding of how to encrypt industrial computers: host reinforcement application
- Lottery system test report
- What is the sheji principle?
- Shopping malls, storerooms, flat display, user-defined maps can also be played like this!
- Mysql 45讲学习笔记(六)全局锁
- [Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
猜你喜欢
云Redis 有什么用? 云redis怎么用?
MySQL 45 lecture learning notes (VII) line lock
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
关于IDEA如何设置快捷键集
Flink memory model, network buffer, memory tuning, troubleshooting
2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
The final week, I split
响应式——媒体查询
随机推荐
How to input single quotation marks and double quotation marks in latex?
notepad++如何统计单词数量
MySQL 45 lecture learning notes (12) MySQL will "shake" for a while
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
How does the inner roll break?
金盾视频播放器拦截的软件关键词和进程信息
抽奖系统测试报告
Mysql 45讲学习笔记(十二)MySQL会“抖”一下
移动适配:vw/vh
颈椎、脚气
ADC voltage calculation of STM32 single chip microcomputer
Lottery system test report
selenium IDE插件下载安装使用教程
《国民经济行业分类GB/T 4754—2017》官网下载地址
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
内卷怎么破?
Tar source code analysis 8
【FPGA教程案例8】基于verilog的分频器设计与实现
What is a spotlight effect?
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?