Detail the differences between synchronous asynchronous blocking and non blocking in java

  • 2020-06-23 00:32:51
  • OfStack

Detail the differences between synchronous, asynchronous, blocking, and non-blocking in java

To put it simply:

Block is not allowed to come back, 1 is in waiting, until the completion of the matter will not return;

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

Let's take the two most commonly used send and recv functions...

For example, if you call send function to send 1-specified Byte, what send does inside the system is to transfer data (Copy) to the output buffer of TCP/IP protocol stack. The success of its execution does not mean that the data has been successfully sent out. If the TCP/IP protocol stack does not have enough available buffer to hold the data of your Copy... The difference between blocking and non-blocking is that the socket send function in blocking mode will not return until there is enough space in the system buffer to send the data you want to send Copy, while the non-blocking socket will immediately return WSAEWOULDDBLOCK and tell the caller :" The send operation is blocked!! Do something about it..."

For the recv function, in the same way, the internal mechanism of the function is waiting for the receive buffer of the TCP/IP stack to tell it, "Hey, your data is coming." For socket in blocking mode, if the receive buffer of the TCP/IP stack does not inform it of a result, it simply does not return: it consumes system resources... For socket in non-blocking mode this function returns immediately and tells you :WSAEWOULDDBLOCK- "No data at the moment, check back."

Extension:

In network programming, we often see synchronous, asynchronous, blocking and non-blocking calls. These ways are not easy to understand each other's concepts. Here's my take on these terms.

1, synchronization,

Synchronization means that when a function call is made, it does not return until the result is obtained. By this definition, most functions are called synchronously (sin, isdigit, etc.). But generally, when we say synchronous or asynchronous, we mean those tasks that require other parts to work together or that require 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 message has been processed. The function does not return the LRESULT value returned by the message handler to the caller until the caller has finished processing.

2, asynchronous

The concept of asynchrony is the opposite of synchronization. When an asynchronous procedure call is issued, the caller does not get an immediate result. The part that actually handles the call notifies the caller via status, notifications, and callbacks when it is complete. In the case of the CAsycSocket class (note that CSocket is derived from CAsyncSocket, but has been converted from asynchronous to synchronous), the caller thread can immediately run down when a client makes a connection request by calling the Connect function. When the connection is actually established, the socket bottom layer sends a message notifies the object. It is mentioned that the executor and caller return results in three ways: status, notification, and callback. Which of these can be used depends on the implementation of the execution part, which is not controlled by the caller unless the execution part provides multiple choices. If the executor is notified by state, then the caller needs to check every 1 fixed time, which is inefficient (some beginners in multithreaded programming tend to use a loop to check the value of a variable, which is actually a serious mistake). In the case of notifications, this is more efficient because the execution widget does little extra work. As for callbacks, they're not that different from notifications.

3, blocking

A blocking call is one in which the current thread is suspended before the result of the call is returned. The function returns only after the result is obtained. One might equate blocking calls with synchronous calls, but they are actually different. In the case of synchronous calls, many times the current thread is still active, just not logically returning from the current function. For example, when we call the Receive function in CSocket, if there is no data in the buffer, the function waits until it returns. At this point, the current thread continues to process the various messages. If the main window and the calling function are in the same thread, the main interface should refresh unless you call it from a special interface function. The other function recv receives data is an example of a blocking call. When socket is working in blocking mode, if it is called without data, the current thread is suspended until there is data.

4. Non-blocking

Nonblocking corresponds to the notion of blocking, which means that the function does not block the current thread, but returns immediately, until the result is not available immediately.

Object's blocking mode and blocking function calls

There is a strong correlation between whether an object is in blocking mode and whether a function is blocking a call, but not an 11. Blocking objects can have non-blocking calls, we can poll the state through 1 fixed API, call blocking functions when appropriate, to avoid blocking. For non-blocking objects, calls to special functions can also enter blocking calls. The function select is an example of this.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: