Knowledge points: Synchronous and asynchronous, blocking and non-blocking

1 Introduction

In some technical blogs or scenarios, there are often synchronous, asynchronous, blocking and non-blocking. The following briefly introduces the difference.

2 Synchronous blocking

2.1 Basic solution

The client sends a request to the server. At this time, the server takes a long time to process the task, and the client is blocked by the server, so the client will always wait for the response from the server. At this time, the client cannot do anything, and the server will not accept it. Requests from other clients. This communication mechanism is relatively simple and rude, but it is not efficient.

2.2 Examples

Synchronous blocking is often said BIO.

I go to the toilet. At this time the pits are full. I have to wait for the pits to be released before I can go, right? ! At this time, I was not doing anything, standing in the toilet and staring at it. Someone would come out after a while, so I squatted quickly.

3 Synchronous non-blocking

3.1 Basic solution

The client sends a request to the server. At this time, the server takes a long time to process the task. At this time, although the client will always wait for a response, the server can process other requests and will return after a while. This method is very efficient. A server can handle many requests without being blocked because the task is not processed, so it is non-blocking.

3.2 Examples

Synchronous non-blocking is often said NIO.

I went to the toilet. At this time, the pits are full. It’s okay. Brother is not in a hurry. I will go out and smoke a cigarette. I will come back to see if there is any space. If there is any, I will squat. .

4 Asynchronous blocking

4.1 Basic solution

The client sends a request to the server. At this time, the server takes a long time to process the task, but the client does not wait for the server to respond, it can do other tasks, wait for the server to process the result, and then process the client after the callback is received The response from the server. This method can avoid the client's waiting state all the time, and optimize the user experience. In fact, it is similar to the ajax asynchronous request initiated in the web page.

4.2 Examples

I went to the bathroom, and the pits were full at this time. I was waiting for nothing. When there is a new vacancy, let him notify me. If he informs me, I will squat up.

5 Asynchronous non-blocking

5.1 Basic answers

The client sends a request to the server. At this time, the server takes a long time to process the task. Although the task will take a long time to process at this time, the client can do other tasks because it processes the response in the asynchronous callback function; at the same time, the server is right or wrong. Blocking, so the server can handle other tasks, so this mode is very efficient.

5.2 Examples

Asynchronous non-blocking is often said AIO.

I went to the toilet. At this time, the pits are full. It’s okay. I’m not in a hurry. I go to the toilet and smoke a cigarette before playing with my phone. When a new pit is released, someone will notify me and notify me. Yes, I'll come over.

6 Summary

It can be seen from this life example:

  • Synchronization means that I need to take a rotation training to see if there are any vacancies;
  • Asynchronous means that someone will notify you after pulling the pit, and you will go back to squat after the notification;
  • Blocking is in the process of waiting, you don't do anything else, just wait;
  • Non-blocking means you can do other things while you wait, such as smoking, drinking, ironing your hair, playing with your mobile phone.

异步The advantages are obvious, greatly improving the user experience, 非阻塞making the system resource overhead is much smaller than the 阻塞mode, because the system does not need to create new processes (or threads), greatly saves system resources can be more out of the system to other middleware to serve the .

7 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/113307392