Java Miscellaneous 10 - The difference between BIO, BIO and NIO

foreword

When I explained NIO in the previous article, I mentioned BIO. BIO is blocking IO, which appeared in java 1.4, mainly through streams to perform related IO operations.

Before explaining BIO, let's first clarify some concepts, synchronous, asynchronous, blocking and non-blocking.

Let’s talk about synchronization and asynchrony first. The object is the object that executes the task . The difference between them is what state it is in after the task is executed. If you keep waiting for the result, it is synchronous; if you return immediately, do something else Things get results through other methods, which is asynchronous.

  1. In the past when technology was not developed, banks used to queue up to withdraw money. If you want to withdraw money, you have to queue up until it is your turn, this is synchronization
  2. Now go to the bank and call the number directly, then go to the rest place to rest and play games. When it is your turn to log in, there will be a notification. This is asynchronous

The so-called blocking and non-blocking are aimed at the CPU, blocking blocking, non-blocking non-blocking, the state of the CPU in the process of waiting for the result, such as doing nothing in the process of queuing is blocking; while queuing, play the glory of the king again is non-blocking. For our java, we use threads to schedule IO operations, so it is actually threads that block when using BIO.

BIO

BIO

BIO can be said to be a knowledge point that every java developer is familiar with, so I will not explain too much. Next, I will mainly explain the difference between BIO and NIO.

The difference between BIO and NIO

The difference between BIO and NIO

The above are some theoretical differences between BIO and NIO, but in fact, for file operations (such as file copying, etc.), there is no difference between BIO and NIO, whether it is the idea or the basic processing flow, the difference is not very big, It can be said that it is just a different set of APIs.

The place where the difference between BIO and NIO can be reflected is in the field of network programming.

We know that the BIO stream is blocking, so it means that when a thread calls read() or write(), the thread is blocked until some data is read, or the data is completely written, the thread is in Can't do anything during this time. In network programming, BIO is the operation of a thread corresponding to a stream, which means that if the stream does not end, the thread needs to wait all the time.

But NIO doesn't need it, the most important reasons are selectors and non-blocking.

The so-called non-blocking IO is divided into read and write:

  1. A thread reads data from a channel. If no data is currently available, it will not get anything, and will not keep the thread blocked until the data becomes available for reading, the thread can continue to do other things.
  2. A thread writes some data to a channel, but does not need to wait for it to be completely written, the thread can do other things at the same time

When NIO is in use, threads usually use the idle time of non-blocking IO to perform IO operations on other channels. A single thread can manage multiple input and output channels (channels). In this case, the selector is needed. Also known as multiplexing.

NIO's selector

Java NIO's selectors allow a single thread to monitor multiple input channels. You can register multiple channels to use a selector, and then use a single thread to "select" channels: those channels already have inputs that can be processed, Or select a channel that is ready to write. This selection mechanism makes it easy for a single thread to manage multiple channels.


I can't guarantee that every place is right, but I can guarantee that every sentence, every line of code has been scrutinized and considered. I hope that behind every article is my attitude of pursuing a purely technical life.

Always believe that good things are about to happen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326096371&siteId=291194637