Home  >  Article  >  Java  >  Detailed explanation of the differences between synchronous, asynchronous, blocking and non-blocking

Detailed explanation of the differences between synchronous, asynchronous, blocking and non-blocking

Y2J
Y2JOriginal
2017-05-06 13:03:451569browse

This article mainly introduces relevant information on the detailed explanation of the differences between synchronization, asynchronous, blocking and non-blocking in java. Friends in need can refer to

Synchronization, asynchronous, blocking and non-blocking in java Detailed explanation of the difference

To put it simply:

Blocking means that you are not allowed to come back until you finish the work, and you are always waiting until the matter is processed before returning;

Non-blocking means that you do it first, and I will check if there is anything else to do first. If you find that something is stuck, report it to the leader immediately.

Let’s take the two most commonly used functions, send and recv, as an example...

For example, if you call the send function to send a certain Byte, the work done by send within the system is actually It just transfers (Copy) the data to the output buffer of the TCP/IP protocol stack. Its successful execution does not mean that the data has been successfully sent. If the TCP/IP protocol stack does not have enough available buffers to save the data you copied, As for data... this is where the difference between blocking and non-blocking comes into play: the socket send function in blocking mode will not return until the system buffer has enough space to copy the data you want to send. For non-blocking sockets, send will immediately return WSAEWOULDDBLOCK to tell the caller: "The send operation is blocked!!! You can find a way to deal with it..."

For the recv function, the same principle applies. The internal working mechanism of the function is actually waiting for the receiving buffer of the TCP/IP protocol stack to notify it: Hi, your data is coming. For sockets in blocking mode, if the receiving buffer of the TCP/IP protocol stack does not notify a As a result, it never returns: consuming system resources.... For non-blocking mode sockets, the function will return immediately, and then tell you: WSAEWOULDDBLOCK---"There is no data now, check back later"

Extension:

When doing network programming, we often see four calling methods: synchronous, asynchronous, blocking and non-blocking. These methods are not easy to understand each other. Below is my understanding of these terms.

1. Synchronization

The so-called synchronization means that when a function call is issued, the call will not return until the result is obtained. According to this definition, in fact, most functions are called synchronously (such as sin, isdigit, etc.). But generally speaking, when we talk about synchronous and asynchronous, we specifically refer to tasks that require the cooperation of other components or take a certain amount of time to complete. The most common example is SendMessage. This function sends a message to a window and does not return until the other party has processed the message. After the other party has finished processing, the function returns the LRESULT value returned by the message processing function to the caller.

2. Asynchronous

The concept of asynchronous is relative to synchronization. When an asynchronous procedure call is issued, the caller does not get the result immediately. The component that actually handles the call notifies the caller through status, notifications, and callbacks when it is completed. Take the CAsycSocket class as an example (note that CSocket is derived from CAsyncSocket, but its function has been converted from asynchronous to synchronous). When a client issues a connection request by calling the Connect function, the caller thread can immediately run downward. When the connection is actually established, the bottom layer of the socket will send a message to notify the object. It is mentioned here that the execution component and the caller return results through three channels: status, notification and callback. Which one can be used depends on the implementation of the executive and is not under the caller's control unless the executive provides multiple choices. If the execution component uses status to notify, then the caller needs to check it every certain time, which is very inefficient (some people who are new to multi-thread programming always like to use a loop to check a certain variable value, this is actually a very serious mistake). If notification is used, the efficiency is very high because the execution component requires almost no additional operations. As for the callback function, it is actually not much different from the notification.

3. Blocking

Blocking call means that the current thread will be suspended before the call result is returned. The function only returns after getting the result. Some people may equate blocking calls and synchronous calls, but in fact they are different. For synchronous calls, in many cases the current thread is still active, but logically the current function does not return. For example, we call the Receive function in CSocket. If there is no data in the buffer, this function will wait until there is data before returning. At this time, the current thread will continue to process various messages. If the main window and the calling function are in the same thread, unless you call it in a special interface operation function, the main interface should still be refreshed. Recv, another function used by the socket to receive data, is an example of a blocking call. When the socket is working in blocking mode, if this function is called without data, the current thread will be suspended until there is data.

4. Non-blocking

The concepts of non-blocking and blocking correspond to each other, which means that the function will not block until the result cannot be obtained immediately. current thread and will return immediately.

The blocking mode of the object and the blocking function call

There is a strong correlation between whether the object is in blocking mode and whether the function is blocking calling, but there is no one-to-one correspondence. There can be non-blocking calling methods on blocking objects. We can poll the status through a certain API and call the blocking function at the appropriate time to avoid blocking. For non-blocking objects, calling special functions can also enter blocking calls. The function select is such an example.

【Related Recommendations】

1. Java Free Video Tutorial

2. Geek Academy Java Video Tutorial

3. Java Tutorial Manual

The above is the detailed content of Detailed explanation of the differences between synchronous, asynchronous, blocking and non-blocking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn