Synchronous and asynchronous, blocking and non-blocking

1.1 Synchronous and asynchronous actually refer to whether the requesting party actively initiates the acquisition of the message result or waits for passive notification.

If it is initiated by the requester, it has been waiting for the response result (synchronous blocking), or you can handle other things first, but you must keep polling to see if the initiated request has a response result (synchronous non-blocking) because it needs to be no matter what The initiator takes the initiative to obtain the message result, so it is still a synchronous operation in form. If it is notified by the server, that is, after the requester sends the request, it will either be waiting for the notification (asynchronous blocking) or doing its own thing first (asynchronous non-blocking). When the processing is completed, the server will Actively notify the requester that its request has been completed, which is asynchronous. Asynchronous notification is generally done through state changes, message notifications, or callback functions. Most of the time, callback functions are used.

1.2 Blocking and non-blocking in the world of computers usually refer to IO operations, such as network IO and disk IO. So what is blocking and non-blocking? Simply put, after we call a function, before waiting for the function to return the result, is the current thread in a suspended state or running state, if it is in a suspended state, it means that the current thread cannot do anything, just wait To get the result, this is called synchronous blocking. If it is still running, it means that the current thread can continue to process other tasks, but from time to time to see if there is a result, this is synchronous non-blocking.

1.3 Examples of scenarios

Synchronous, asynchronous, blocking, and non-blocking will combine into the four results mentioned above: For example, if we go to a photo studio to take photos after the photos are taken, the business says it takes about 30 minutes to develop the photos.

a. Synchronization + blocking At this time, if we have been doing nothing in the store, waiting for the business to finish washing the photos, this process is called synchronization blocking.

b. Synchronous + non-blocking Of course, most people rarely do this. Most people pick up their mobile phones and start watching TV. After watching for a while, they will ask the boss if they have finished washing. The boss says that they have not finished washing. Then we watch and then ask again. Until the photo is washed, this process is called synchronous non-blocking.

c. Asynchronous + blocking Because the business in the store is so good, more and more people come to take pictures, and there is almost no place to sit in the store. The boss said that you left your phone number, and I will call you when I wash it and tell you to come and pick it up. You go outside to find a bench and start to lie down and sleep waiting for the boss to call, do nothing. This process is called asynchronous blocking.

d. Asynchronous + non-blocking Of course, the actual situation is that people may go shopping or eat and do other activities first, and the shop owner will pick up the photos when they call so that there is no delay in the two things. This process is called asynchronous non-blocking.