当前位置:网站首页>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. .
边栏推荐
- What is the "relative dilemma" in cognitive fallacy?
- Mysql 45讲学习笔记(十)force index
- Option (024) - do all objects have prototypes?
- 抽奖系统测试报告
- leetcode 310. Minimum Height Trees
- [backpack DP] backpack problem
- Responsive - media query
- [GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
- Tar source code analysis Part 2
- The sorting in C language realizes the number sorting method from small to large
猜你喜欢
![[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph](/img/5e/7ce21dd544aacf23b4ceef1ec547fd.png)
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph

Bottom problem of figure

selenium驱动IE常见问题解决Message: Currently focused window has been closed.

what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!

P26-P34 third_ template

GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
![[problem record] 03 connect to MySQL database prompt: 1040 too many connections](/img/bb/4d8d202cf5c6e556bc860a734e5934.png)
[problem record] 03 connect to MySQL database prompt: 1040 too many connections

The most effective futures trend strategy: futures reverse merchandising

The important role of host reinforcement concept in medical industry

Mysql 45讲学习笔记(七)行锁
随机推荐
Check and display one column in the known table column
Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
Centos8 install mysql 7 unable to start up
The sorting in C language realizes the number sorting method from small to large
MySQL 45 lecture learning notes (VII) line lock
How does the inner roll break?
How notepad++ counts words
com. alibaba. nacos. api. exception. NacosException
Another company raised the price of SAIC Roewe new energy products from March 1
What is the sheji principle?
测试用例的设计
Data analysis notes 09
Uniapp custom environment variables
2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
Chapter 1 programming problems
tcp socket 的 recv 如何接收指定长度消息?
MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
selenium IDE插件下载安装使用教程
What is tweeman's law?
tars源码分析之9