“This is the second day of my participation in the November Gwen Challenge. Check out the details: The Last Gwen Challenge 2021


In order to fully understand NIO, we need to master some basic concepts, such as what is IO, the difference between the five IO models, what is blocking and non-blocking, etc. We are much more comfortable with the NIO talent.

This article starts with blocking vs. non-blocking, synchronous vs. asynchronous.

Synchronous and asynchronous

What are synchronous and asynchronous? Baidu Encyclopedia defines it as follows:

Synchronization refers to the relative relationship between two or more quantities that change over time.

Asynchrony as opposed to synchrony (this explanation leaves me speechless)

So, we need to make it clear that ** synchronous and asynchronous are for two or more things **.

For synchronization, the completion of one task (caller) depends on the completion of another person’s task (caller), and the dependent task will only continue until the dependent task completes, keeping pace with each other.

Asynchronous? It doesn’t have to wait for the dependent task to complete. It just calls the dependent task and tells it you’re ready to work. It doesn’t matter when you’re done.

Therefore, the essential difference between synchronous and asynchronous is the difference between the caller and the called result message notification mechanism.

  • Synchronization: The caller needs to alwaysTake the initiative to waitThe result of the called.
  • Asynchronous: After the caller calls the caller, the caller does not get the result immediately. After the caller initiates the call, the caller lets the caller know the result through status, notification, or callback function

Therefore, synchronous and asynchronous one is active waiting for the result, the other is passive knowing the result.

Take a simple example: to buy milk tea, we have two ways to get the milk tea we bought

  • Choose to wait in line. This way is synchronous waiting for the notification, we need to always wait in front of the bar for our milk tea
  • scan the code This way, you can keep checking your cell phone to see if it’s your turn, or you can stay there and play with your cell phone until the waiter calls number 88 and milk tea is ready.

As mentioned above, asynchronous calls can be notified to the caller by status, notification, or callback functions.

  • Status: The caller needs to make a status query request to the caller at regular intervals. This approach is inefficient. Generally, when we call the payment interface, if the service side tells us that the payment status is unknown, we need to query the payment status of this order at intervals. It’s inefficient, but it’s reliable.
  • Notification: This way, the caller doesn’t need to do any extra work, he just needs to wait for the caller to tell him the result. But this approach is a bit unreliable, when does it call, and what if it doesn’t? These are the questions we need to consider.
  • Callback function: similar to notification mechanism.

Blocking and non-blocking

What is synchronous and asynchronous, but what is blocking and non-blocking?

The so-called obstruction is that there are obstacles and can not pass, can not be unblocked.

So, blocking means that the thread is suspended until the result of the call is returned, waiting for the result and not continuing, and the function will return only after it gets the result.

Some people might equate blocking with synchronization because they are both stuck waiting for execution results, but there is a difference:

  • Synchronization, for two processes, where one (caller) is stuck waiting for the execution result of the other (called). Blocking, on the other hand, is for one because it is itself stuck waiting for the result of an execution in the current thread.
  • For synchronization, the current thread is still active, but logically (sensibly) it is stagnant, and the current thread may be doing something else. Blocking, on the other hand, suspends the current thread, giving up the CPU.

Non-blocking, as opposed to blocking, means that the function does not block execution of the current thread until the result is not immediately available, but returns immediately.

As for the above example of buying milk tea, whether you wait in line for milk tea or scan the code to wait for milk tea, as long as you do nothing else in the process of waiting for milk tea, you will be blocked. If you’re talking to your girlfriend (if you have one) or on your phone while you’re waiting, it’s non-blocking because you’re not waiting for milk tea, you’re doing something else while you’re waiting.

Synchronous & asynchronous, blocking & non-blocking

The combination of synchronous and asynchronous and blocking and non-blocking is synchronous blocking, synchronous non-blocking, asynchronous blocking, and asynchronous non-blocking. Take the example of milk tea above.

A synchronized block

You can do nothing but wait while you wait in line for milk tea. I asked if you were bored or embarrassed. Most inefficient.

Synchronous nonblocking

In the process of waiting for milk tea, you can do other things, such as brushing Douyin, playing king of Glory, but you need to constantly see whether milk tea has come to you, you are bound to be distracted and lead to lose king of Glory, become a pit. Pay attention to waiting in line for milk tea, playing King of Glory is two things, you need two things constantly switch back and forth, efficiency is not high to where to go.

Asynchronous blocking

After you scan the code and get the number, you don’t have to wait in line, you just need to wait for the waiter to tell you that the milk tea is ready to take, but during the waiting process, you can do nothing but wait. Apparently you are stuck waiting for the waiter to tell you that the milk tea is ready. It is important to note that asynchrony does not block. Asynchrony can block, but it is blocked not while processing messages, but while waiting for message notifications.

Asynchronous nonblocking

After you sweep the code to take the number, go directly to the side to play king of Glory, midway you concentrate on playing king of Glory, do not need to be distracted to pay attention to your milk tea is done, you just need to wait for the waiter to tell you the milk tea is done (message notice) to take it. The most efficient.

The resources

  • www.cnblogs.com/xiaoQLu/p/1…
  • zhuanlan.zhihu.com/p/385202919