Conceptual understanding of synchronous, asynchronous, blocking, non-blocking

Source: Internet
Author: User
Tags socket blocking

The synchronous and asynchronous mechanisms in network communication describe communication patterns. Synchronization mechanism, refers to the sender after sending a request, waiting to receive a response sent back by the receiver before starting to send the next request, the asynchronous mechanism, in contrast, refers to the sender after sending a request, do not wait for the receiver to respond to the request to continue to send write a request. All requests from the sender form a queue, and the receiver notifies the sender after the request has been processed.

Blocking and non-blocking are used to describe the process of processing calls, in fact, the network sockets socket blocking and non-blocking mode, and the essence of the socket is IO operation. The blocking invocation of the socket is, before the call IO results are returned, the current thread is suspended from the running state, waiting until the call results are returned before being woken up, going into the ready state, getting the CPU to continue execution, and non-blocking calls in a non-blocking way, if the result of the call cannot be returned immediately , the current thread is not suspended, but returns immediately to the next call.

These four concepts 22 combinations can produce different treatments in four ways:

Synchronous blocking Mode:

Send to the receiver after sending a request, waiting for a response, the receiver when processing the request if the IO operation does not get the result immediately, it will wait until the return result in the response to the sender, during the period of no other work.

For example: In the supermarket line to pay the time, the customer (sender) to the salesperson (receiver) payment (send the request) after waiting for the cashier change, during the period can not do other things, and the cashier waiting for the cash register to return the results

(IO operation) before the change can be taken out to the customer (in response to the request), the period can only wait, can not do other things. This approach is simple to implement, but inefficient.

Synchronous non-blocking mode:

Send direction after the receiver sends the request, waits for a response, the IO operation performed by the receiving party if it cannot get the result immediately, returns to do other things, but does not respond due to the processing result of the request.

Sender, the sender has been waiting. Until the IO operation is complete and the receiver obtains the request result in response to the sender's request, the sender makes the next request process. This method is not actually used.

Asynchronous non-blocking mode:

Send direction the receiver sends a request, does not wait for a response, can continue to other work, the receiver processing the request when the IO operation if the result can not be obtained immediately, after waiting for the return result, in response to the sender,

Cannot do other things during the period. This approach is not used in practice.

Asynchronous non-blocking mode:

Send direction after the receiver makes a request, do not wait for a response, you can directly make the next request or other work, the receiver processing the request for the IO operation if the result is not immediately available, do not wait, but return to do its

him things. When the IO operation is complete, the receiver is notified of the status and results of the completion, and the receiver responds to the sender. Before you proceed to the supermarket payment example: the customer (sender) pays the salesperson (the receiver) (sending the request)

There are other things you can do while you're waiting for a change, such as making a phone call, chatting, and a cashier waiting for a cash register to process a transaction (IO request) to help the customer pack the product, and then collect it when the cash register returns

Member to pay the customer (in response to the request). In four ways, this is one of the most efficient communication between sender and receiver.

Conceptual understanding of synchronous, asynchronous, blocking, non-blocking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.