当前位置:网站首页>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 .
边栏推荐
- [untitled]
- The difference between vectorresize and reverse.
- Homework of the 16th week
- Typescript practice for SAP ui5
- Websites that it people often visit
- Pytoch --- use pytoch for image positioning
- okcc为什么云呼叫中心比传统呼叫中心更好?
- Wechat applet JWT login issue token
- Pytorch-Yolov5從0運行Bug解决:
- [C language] basic learning notes
猜你喜欢
![[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification](/img/77/4df7a1439ff1a53f94d409a19a47d6.png)
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification

Li Kou interview question 02.08 Loop detection

BiShe cinema ticket purchasing system based on SSM

Déchirure à la main - tri

Fluent icon demo

Lei Jun wrote a blog when he was a programmer. It's awesome

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
![[tips] use Matlab GUI to read files in dialog mode](/img/51/6d6051836bfc9caa957d0275245bd3.png)
[tips] use Matlab GUI to read files in dialog mode

Websites that it people often visit

文档声明与字符编码
随机推荐
Shutdown procedure after 60
Go language introduction
Okcc why is cloud call center better than traditional call center?
Wechat applet JWT login issue token
Monkey测试
Jetpack's livedata extension mediatorlivedata
C语言:逻辑运算和判断选择结构例题
Document declaration and character encoding
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Pytoch --- use pytoch to predict birds
Go language naming specification
PIP installation of third-party libraries
The difference between vectorresize and reverse.
[tips] use Matlab GUI to read files in dialog mode
Pytorch---使用Pytorch进行鸟类的预测
C语言猜数字游戏
向数据库中存入数组数据,代码出错怎么解决
Recyclerview add header
手撕——排序
BGP experiment the next day
