当前位置:网站首页>Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
2022-07-02 04:17:00 【Mingmingruyue senior】
One 、 background
In the evening, a friend in the technology group asked ,“ What is the difference between synchronization and blocking color ?”
Many friends gave their opinions , Some of the contents are as follows :
Among them, a friend gave a screenshot , The figure shows the comparison “ vivid ” Example .
However , After reading it, I seem to understand , I don't seem to understand .
Many people think about these common concepts , It seems familiar and strange .
Two 、 understand
2.1 Leading edge
If a classmate had seen mine before 《 In my eyes Java Daniel's lonely teacher 》 This article , You will find that one or two have been mentioned in it , Unfortunately, most students only want to ask and see when they encounter problems .
2.2 visual angle
For this question , I prefer to see it from a perspective .
Synchronous and asynchronous are the perspectives of function calls
That is, after synchronously executing function calls , You must wait for the function to return to continue ; Asynchrony does not need to wait for return to continue to perform other tasks .
For example, the leader arranges you to print an e-book with hundreds of pages , Synchronization is equivalent to printing yourself , While printing, I have been waiting at the side of the printing machine for printing to complete , Do other things ; Asynchronously, we click Print and then do other things , For example, click Print to brush a play first, or we ask another classmate to print it for us , Then do other things by yourself .
Go back to specific business , Suppose we need to send a text message to the user after the system login is successful , After we log in , Use the thread pool to start the thread or send a message queue message to notify the downstream to send SMS , The login method can return directly without waiting for the execution of the SMS program .
Blocking and non blocking are thread perspectives
in my opinion , Blocking and non blocking describe thread state .
Blocking means that the current thread is suspended after a call is executed ( As in the production consumer model , When there is no consumer content wait), Release CPU , Until the result is awakened ( When there is consumable content adopt notifyAll Wake up consumer thread ); Non blocking is when a call is executed , Even if you can't get the results immediately , The current thread will not be suspended .
Like taxi drivers , After receiving the passengers , The front sign will turn to [ someone ], We can't continue to receive guests at this time , Know that the passengers get off and end the order , This is it. [ Blocking ]; The taxi driver does not turn the sign to [ someone ], Even if a passenger is not delivered to the destination , You can continue to take orders , That was [ Non blocking ].
Usually [ Blocking ] It will cause the thread to enter the suspended state ( The effect is like calling wait Method ),[ Sync ] When called, the thread is still running , Executing function call ( The effect is like calling sleep Method ), It's just that the call hasn't returned .
Parallel and serial are CPU visual angle
Parallel tasks can usually be split into multiple steps , Multiple CPU Execute together with the core , There is usually no dependency between such tasks , Completely orthogonal ; Each step of a serial task can only be executed by one core at the same time , There are usually upstream and downstream dependencies .

We can compare multiple people to multiple people CPU The core . For example, we want to put elephants in the refrigerator , You have to open Refrigerator door , Then put the elephant in , Then close the refrigerator door , Even if we have more than one person , But the steps cannot be performed at the same time . That is, you can't directly install elephants before the refrigerator door is opened , You can't just close the refrigerator door before the elephant goes in , This is it. [ Serial ].
If we want to publish a Book , The book can be divided into different chapters , Then each person is divided into a chapter , Let's write together , At this time, many people are doing tasks together , And it's helpful to the progress , This is it. [ parallel ].
Follow this example , We want to [ parallel ] and [ Concurrent ] The difference between , There's quite a lot of concurrency. There's only one of us , You can write the first chapter later , Write the second chapter later , Write chapter three later , Then write the first chapter , Although you can switch to do different things , But only one person .
3、 ... and 、 summary
For these concepts , I hope you can grasp the essence , Don't be confused by various examples .
Synchronous and asynchronous are the perspectives of function calls , The judgment is based on whether it is necessary to Waiting to return .
Blocking and non blocking are thread perspectives , That is, whether the current thread is called Hang up .
Parallel and serial are CPU visual angle , I.e. Yes No meanwhile There are multiple cores undertaking the same task together .
This article just talks about my understanding , If there are omissions , You can comment and communicate with me , You are also welcome to give a more understandable way of understanding .
It's not easy to create , If this article helps you , Welcome to thumb up 、 Collection and attention , Your support and encouragement , It's the biggest driving force of my creation .
边栏推荐
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Pytorch-Yolov5从0运行Bug解决:
- Handling of inconsistency between cursor and hinttext position in shutter textfield
- Go variables and constants
- Installation et utilisation du lac bleu
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
- Thinkphp6 limit interface access frequency
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- 【leetcode】74. Search 2D matrix
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
猜你喜欢

10 minutes to understand CMS garbage collector in JVM
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe

WiFi 5GHz frequency

Introduction to vmware workstation and vSphere

Raspberry pie GPIO pin controls traffic light and buzzer

Yyds dry inventory compiler and compiler tools

Pytoch --- use pytoch to predict birds

【c语言】动态规划---入门到起立

Opencv learning example code 3.2.4 LUT

Www 2022 | rethinking the knowledge map completion of graph convolution network
随机推荐
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
Feature Engineering: summary of common feature transformation methods
cookie、session、tooken
FAQ | FAQ for building applications for large screen devices
Go language naming specification
Go language introduction
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Federal learning: dividing non IID samples according to Dirichlet distribution
Qt插件之Qt Designer插件实现
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
Use of go package
C language guessing numbers game
Go branch and loop
JVM knowledge points
How much is the tuition fee of SCM training class? How long is the study time?
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
Go function
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
office_ Delete the last page of word (the seemingly blank page)
