The difference between synchronous, asynchronous, blocking, and non-blocking

The concepts of synchronization, asynchrony, blocking, and non-blocking are used more in concurrent programming and architecture design. Here is a small note about their understanding, and first explain the difference between them:

 

Synchronization : Multiple tasks or events must be executed sequentially, and the next line of tasks cannot be performed until the previous task is completed.

Implementation: common sequential programming, serial execution

Scenario: HTTP request-response mode of B/S architecture, OA process.

 

Asynchronous : Multiple tasks or things can be executed in parallel, and the execution of any one task will not block the execution of another task. After the asynchronous call completes, the result is delivered to the receiver via a notification or callback. Asynchronous is often used in situations where the operation time is relatively time-consuming. In order not to block the current workflow, asynchronous is used.

Implementation: create a new thread, or put tasks into the message queue for processing and parallel processing by consumers

Scenarios: send text messages, write letters, write emails, send WeChat, do telecommunication services online (such as applying for credit cards), node.js, ajax.

 

Synchronization and asynchrony focus on whether the execution of one task will cause the entire process to block during the execution of multiple tasks .

 

Blocking : When no result is returned from the call, the thread is in a suspended state (the thread enters a non-executable state, in which the CPU will not allocate a time slice to the thread, that is, the thread is suspended), waiting for the result to return.

Scenario: Line up to enter the train ticket gate, line up to do business.

 

Non-blocking : When the call does not return a result, it immediately returns a message to inform that the condition is not met and will not wait there forever.

Scenario: When buying a train ticket online, when the ticket is not booked, use the timing monitoring software to see if there is a train ticket.

 

 

While blocking and non-blocking focus on issuing a request operation, if the conditions for the operation are not satisfied, a flag message will be returned to inform that the conditions are not satisfied .

 

 

easy to confuse

Sometimes blocking and synchronization are easy to confuse, but they are actually different. For synchronous calls, many times the current thread is still active, but logically the current function does not return.

 

summary

If you want to build high-performance services, you should consider whether you can use asynchronous non-blocking architectures, such as node.js, netty, nio, etc.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326351270&siteId=291194637