


Detailed explanation of the differences between synchronous, asynchronous, blocking and non-blocking
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】
2. Geek Academy Java Video Tutorial
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
