Fun Java in three say they get to know what is the IO model synchronous / asynchronous / blocking / non-blocking

Point of attention, do not get lost; Java-related technologies and continuously updated information! ! !

In recent interviews, the candidates talked with a lot more related to the topic of "synchronous / asynchronous, blocking / non-blocking" and found we are more vague for the understanding of these concepts, and even some students will ask, "is that they do not the same thing?. " And so through such an opportunity, I would like to try to use some simple examples, simple language as much as possible to talk about your views on these concepts.

Synchronous blocking

Holiday, Wang returned to the country, due to the country's infrastructure is relatively poor, when he was waiting at the station, waiting only been dry until the bus station.

This time for the bus (to be called a person), it is the "synchronize" the. Pharaoh (the caller) is the bus (the caller) "blocking" on the platform.

Blocking asynchronous

Done and fake, Wang returned to the big cities to work in the same station waiting, waiting, like dry at the station, but the big city infrastructure better, when the bus arrival, there will be prompt to remind broadcasting passenger.

So this time for the bus (to be called a person), it is "asynchronous", and the caller will be notified after arriving. But this time Pharaoh (the caller) or by bus (the caller) "blocking" on the platform.

Synchronous non-blocking

New Year, Pharaoh holiday back to the countryside, but also waiting to start, and this time he was smarter, not waiting at the station has been dry, but look for flowers next to catch up. But he feared the car arrived and they will be missed, can only come from time to time to look at the car yet.

So this time for the bus (to be called a person), it is the "synchronize" the. But this time Pharaoh (the caller) can shop and go to other things while waiting for the bus, so he is "non-blocking" of.

Asynchronous non-blocking

Montreal reform in spring, new rural construction is hot at this time of the countryside, the bus also installed inside the vehicle reminder station broadcast. Now when Pharaoh waiting, you can feel at ease with the florets reminisced, when they heard they need to take the vehicle to the station broadcast only the last car Station.

This time for the bus (to be called a person), it is "asynchronous" after the station will broadcast a reminder, this time Pharaoh (the caller) can shop and go to other things in the waiting time, so he It is a "non-blocking" of

The concept summary

From the above example, we can understand one thing, synchronous asynchronous, non-blocking objects blocking for them is not the same. For the caller is blocked with a non-blocking, the caller is synchronous with asynchronous.

Synchronization: A calls B, B, etc. In this case only have the results before returning.

Asynchronous: A calls B, B returns immediately, without waiting. When B processed results will tell A notification callback function or mode.

Blocking: A calls B, A will be is suspended, has been waiting for the results of B, what is not capable.

Non-blocking: A calls B, their use is suspended awaiting the results of B, you can shop and go to other things.

Java related concepts

IO model in Java, there are three, namely BIO (synchronous blocking IO), NIO (synchronous non-blocking IO), AIO (Asynchronous non-blocking IO). This time we will find, blocking asynchronous model does not exist.

NIO appear with the AIO solve many problems encountered in the course of BIO, so we choose what kind of IO to use when you need to make a decision based on business scenarios, with no need to blind pursuit of NIO AIO, not only increased the difficulty of coding also increase the probability of error, the emergence of technology in order to better solve the problem.
Here Insert Picture Description

Epilogue

This article mainly want to describe the meaning of these concepts and distinguished by the familiar scene, if you want more in-depth to delve into it, you can go to access Linux IO model relevant information, Java's IO API is based on these basic models to encapsulate of.

Concerns, comments java share knowledge every day, giving Spring source code analysis, Dubbo, Redis, Netty, zookeeper, Springcloud, distributed data

Guess you like

Origin blog.csdn.net/XingXing_Java/article/details/90759959