当前位置:网站首页>This time, let's clear up: synchronous, asynchronous, blocking, non blocking
This time, let's clear up: synchronous, asynchronous, blocking, non blocking
2022-07-07 22:53:00 【Yes' level training strategy】
Hello everyone , I am a yes.
Following last article Speak thoroughly I/O Model after , Let's talk about the Internet I/O Several confusing concepts often accompany : Sync 、 asynchronous 、 Blocking 、 Nonblocking differences .
After writing this article, the groundwork is almost done , Then it officially begins Netty In depth analysis of , Coming soon , Hee hee .
Don't talk much , Start !
Sync & asynchronous
Synchronous and asynchronous refer to : Whether the current thread needs to wait for the method call to complete .
For example, you call a method to carry 100 stones :
- Synchronization refers to calling this method , Your thread needs to wait for these 100 stones to be lifted , Then get the result of moving , Then continue to execute the remaining code logic .
// Synchronization mode
result = Lift a hundred stones ();
// You need to wait for the result of moving , To execute the following logic
if(result) {
Pay when the stone is finished ();
}
Calculate the next stone lifting task ();
- Asynchrony refers to calling this method , Immediately return directly , Don't wait for these 100 stones to be moved , You can immediately execute the following code logic , Then use callback or event notification to get the result that the stone has been moved .
// Asynchronous way
Lift a hundred stones ({
// Callback
Pay when the stone is finished ();
});
// Don't wait for the stone to be lifted , Immediately execute the following logic
Calculate the next stone lifting task ();
It can be seen directly , Synchronization and asynchrony are the differences in the calling methods , This makes our coding method different .
The code logic under asynchronous invocation is relatively less intuitive , You need to use callback or event notification , This requires high coding ability in complex logic . Synchronous calls are straightforward , Wait for the execution to finish, then get the result, and then execute the following logic , The requirements for coding ability are low , It's also less likely to make mistakes .
So you will find that there are many methods, which are called asynchronously , But the final use is asynchronous to synchronous .
For example, you submit a task to the thread pool , Get one future, At this time, it is asynchronous , Then you call in the code immediately future.get()
, It becomes waiting for the task to be completed , This is called asynchronous to synchronous , image Dubbo RPC Call synchronization to get the return result, which is achieved in this way .
Blocking & Non blocking
Blocking and non blocking refer to : When the current interface data is not ready , Whether the thread is blocked and suspended .
What is blocking pending ? That is, the current thread is still in CPU In the time slice , Blocked method called , Because the data is not ready , Then give up before the time slice CPU.
So blocking and synchronization seem to be waiting , But essentially they are different , I didn't give up when synchronizing CPU.
Non blocking is when the current interface data is not ready , The thread will not be blocked and suspended , You can constantly poll the request interface , See if the data is ready .
So far, we can draw a conclusion :
- Sync & Asynchronous refers to : When the data has not been processed , The logical processing of the code is different .
- Blocking & Non blocking finger : When the data has not been processed ( Not ready ), State of thread .
So synchronization & Asynchrony is actually viewed from the high-level dimension of framework , And jam & Non blocking is often chosen for the underlying system call , In other words, the two are considered from different dimensions .
combining I/O Look at
Premise : There is an operating system between the program and the hardware , And for the sake of safety ,Linux The system is divided into : User mode and kernel mode
Under this premise , Let's be clear I/O The operation has two steps :
- launch I/O request
- actual I/O Reading and writing , That is, data is copied from kernel cache to user space
Blocking I/O And non blocking I/O. As above , It actually refers to whether the user thread is blocked , Here refers to the steps 1( launch I/O request ).
- Blocking I/O, It refers to that the user thread initiates I/O On request , If the data is not ready ( For example, there is no network data reception ), Will block the current thread , Give up CPU.
- Non blocking I/O, It refers to that the user thread initiates I/O On request , If the data is not ready ( For example, there is no network data reception ), It will not block the current thread , You can continue to perform subsequent tasks .
You can find , Blocking and non blocking here actually refer to whether the user thread will be blocked .
Sync I/O And asynchronous I/O. As above , We can know that this is the basis I/O Different response modes .
- Sync I/O, It refers to that the user thread initiates I/O On request , There are data , Then go to step 2( actual I/O Reading and writing , That is, data is copied from kernel cache to user space ), In this process, the user thread waits for the copy to complete .
- asynchronous I/O, It refers to that the user thread initiates I/O On request , There are data , Then go to step 2( actual I/O Reading and writing , That is, data is copied from kernel cache to user space ), There is no need for user threads to wait in the process of copying , User threads can execute other logic , Wait until the kernel copies the data from kernel space to user space , The user thread will get a “ notice ”.
Think again , stay I/O In this scenario, synchronization and asynchrony are actually the implementation of the kernel , Because the executor of the copy is the kernel , One is to synchronously copy data to user space , User threads need to wait . One is through asynchronous way , User threads don't have to wait , After copying , The kernel will call the specified callback function .
If you don't understand the above , Just remember :
- Sync I/O: It means that the user thread will need to wait for the step 2 completion of enforcement .
- asynchronous I/O: It means that the user thread does not need to wait for the step 2 perform .
Okay , If you have understood the above concepts , So what we call synchronization blocking in ordinary days I/O, Synchronous nonblocking I/O Wait is actually to combine the above two steps , It shouldn't be hard to understand .
Let me briefly summarize , About I/O The block 、 Non blocking 、 Sync 、 asynchronous :
- Blocking and non blocking refer to initiating I/O After the request , Different user thread states , Blocking I/O The current user thread will be blocked when the data is not ready , Instead of blocking I/O Will immediately return an error , Does not block the current user thread .
- Synchronous and asynchronous refer to , Kernel I/O Copy implementation , When the data is ready , You need to copy kernel space data to user space , If it's synchronization I/O Then the user thread will wait for the copy to complete , The asynchronous I/O Then the user thread of this copy process can do whatever it should do , When the kernel is copied, it will “ notice ” User threads .
Last
it is to be noted that , The same noun may have different meanings in different scenes . My article about synchronization 、 asynchronous 、 Blocking 、 The concepts of non blocking are based on I/O In the scene .
Limited personal ability , I don't know if I have made it clear , If you have any questions, you can leave a message in the message area .
Okay , About Netty The series is almost ready , The next chapter begins to deepen Netty On a journey !
Links to the first two foreshadowing articles are as follows :
Why the Internet I/O Will be blocked
Let us , From a to Z , Be transparent I/O Model
I am a yes, From a little bit to a billion , See you next time !
边栏推荐
- Remove the default background color of chrome input input box
- Cataloger integrates lidar and IMU for 2D mapping
- 行测-图形推理-1-汉字类
- Revit secondary development - Hide occlusion elements
- Write in front -- Talking about program development
- Blender exchange group, welcome to the water group ~
- Two methods of calling WCF service by C #
- Revit secondary development - get the thickness / length / height of the beam
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- Pyqt GUI interface and logic separation
猜你喜欢
0-5VAC转4-20mA交流电流隔离变送器/转换模块
How to judge whether the input content is "number"
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
行测-图形推理-5-一笔画类
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
Leetcode interview question 02.07 Linked list intersection [double pointer]
Redis official ORM framework is more elegant than redistemplate
Quick sort (diagram +c code)
随机推荐
Leetcode94. Middle order traversal of binary trees
行测-图形推理-1-汉字类
Ligne - raisonnement graphique - 4 - classe de lettres
Take full control! Create a "leading cockpit" for smart city construction
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Force deduction - question 561 - array splitting I - step by step parsing
Use partial derivatives to display normals in unity
不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程
ASEMI整流桥KBPC1510的型号数字代表什么
Ren Qian code compilation error modification
php 获取图片信息的方法
Debezium系列之:支持 mysql8 的 set role 语句
Firefox browser installation impression notes clipping
Debezium series: binlogreader for source code reading
Ueeditor custom display insert code
[environment] pycharm sets the tool to convert QRC into py file
Welcome to CSDN markdown editor
Install mxnet GPU version
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
Debezium series: support the use of variables in the Kill Command