preface

In recent interviews, I’ve had a lot of conversations with candidates about “synchronous/asynchronous, blocking/non-blocking”, and found that everyone had a vague understanding of these concepts. Some students even asked “Aren’t they the same thing?” . So I’d like to take this opportunity to talk about my views on these concepts in as simple an example as possible, in as simple a language as possible.

The body of the

This article tries to explain these concepts through a case of Lao Wang “waiting for a bus”.

A synchronized block

After the holiday, Lao Wang returned to the countryside. Due to the poor infrastructure in the countryside, when he was waiting at the bus stop, he had to wait until the bus arrived.

For the bus, it is synchronous. Lao Wang (caller) is blocked on the platform by the bus (caller).

Asynchronous blocking

After the vacation, Lao Wang went back to the big city and began to work. He also waited at the station, but the infrastructure construction in the big city was better. When the bus arrived at the station, there would be a radio prompt to remind passengers.

In this case, the bus is “asynchronous”, notifying the caller when it arrives. However, Lao Wang (caller) is still “blocked” on the platform by the bus (caller).

Synchronous nonblocking

New Year’s day, Lao Wang holiday came back to the countryside, and began to wait for the bus, at this time he became smart, has not been waiting in the station, but to find the next door to the flower. But afraid of the car will miss the station, can only come to see the car at intervals.

So for the bus, it’s synchronous. But wang (the caller) can do other things while waiting for the bus, so he is “non-blocking”.

Asynchronous nonblocking

In rural areas, public buses have been installed to announce the arrival of vehicles. Now when Lao Wang is waiting for the bus, he can feel at ease with the floret, when he hears his need to take the vehicle to the station broadcast, just past the station to get on the bus.

At this point, the bus is “asynchronous”, broadcasting an alert when it arrives, and Lao Wang can do other things while waiting for the bus, so he is “non-blocking”

Concept to summarize

One thing we can see from the above example is that synchronous asynchronous and blocking non-blocking target objects differently. Blocking and non-blocking for the caller, synchronous and asynchronous for the called.

Synchronization: A calls B and returns only when B has A result. Asynchronous: A calls B, and B returns immediately without waiting. When B finishes processing, it will inform A of the result through notification or callback function. Block: A calls B, and A is suspended, waiting for the result of B, unable to do anything. Non-blocking: A calls B, waits for the result of B, and can do other things.

Related concepts in Java

There are three IO models in Java, namely BIO (synchronous blocking IO),NIO (synchronous non-blocking IO), and AIO (asynchronous non-blocking IO). The model of asynchronous blocking does not exist.

The emergence of NIO and AIO has solved many problems encountered in the process of using BIO, so we need to make decisions according to business scenarios when choosing which IO to use. There is no need to blindly pursue NIO and AIO, which not only increases the difficulty of coding but also increases the probability of error. The emergence of technology is to better solve problems.

conclusion

The purpose of this article is to describe the meanings and differences of these concepts through familiar scenarios. If you want to delve more deeply, you can refer to the Linux IO model. Java IO apis are also based on these basic models.


Recommended reading

“Java Exception Handling Best Practices and Trap Prevention” “On several JVM Explosion Poses and Self-Rescue Methods” “Zookeeper Distributed Lock Principles that my Girlfriend can Understand” “Supervisor of Freeing Programmer hands”

Follow the late Night programmer to share the driest stuff