当前位置:网站首页>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. .
边栏推荐
- 关于IDEA如何设置快捷键集
- centos8安装mysql.7 无法开机启动
- tars源码分析之2
- Tar source code analysis Part 10
- Background and current situation of domestic CDN acceleration
- tars源码分析之10
- Selection (021) - what is the output of the following code?
- 2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?
- Latex中的单引号,双引号如何输入?
- 同一个job有两个source就报其中一个数据库找不到,有大佬回答下吗
猜你喜欢
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
Crawler (III) crawling house prices in Tianjin
uniapp 自定義環境變量
Introduction to spark core components
Google Chrome Portable Google Chrome browser portable version official website download method
List of top ten professional skills required for data science work
Cervical vertebra, beriberi
Deep profile data leakage prevention scheme
Responsive mobile web test questions
uniapp小程序分包
随机推荐
What is tweeman's law?
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
js 常用时间处理函数
CORS is not intended to protect API endpoints - nikofischer
Chapter 1 programming problems
2022年,或許是未來10年經濟最好的一年,2022年你畢業了嗎?畢業後是怎麼計劃的?
Background and current situation of domestic CDN acceleration
Industrial computer anti-virus
测试用例的设计
If there are two sources in the same job, it will be reported that one of the databases cannot be found. Is there a boss to answer
The most effective futures trend strategy: futures reverse merchandising
GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
Data analysis notes 09
leetcode 310. Minimum Height Trees
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?
Lottery system test report
Mysql 45讲学习笔记(六)全局锁
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native