What is NiO
Non Blocking Io is referred to as NIO, which is non-blocking IO.
The difference between NIO and traditional IO
The first major difference between NIO and traditional io (abbreviated IO) is that IO is stream-oriented and NIO is buffer-oriented. The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read, and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth. If you need to move the data read from the stream before and after it, you need to cache it to a buffer first. The buffering guidance method of NIO is slightly different. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process. However, you also need to check if the buffer contains all the data that you need to process. Also, make sure that when more data is read into the buffer, do not overwrite the data that has not been processed in the buffer.
The various streams of Io are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. NIO's nonblocking mode allows a thread to send a request to read data from a channel, but it can only get the data that is currently available and will not get anything if no data is available. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable. The same is true for non-blocking writes. A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).
NIO has three major core
Channel (channels), buffer (buffers), Selector. Traditional IO operates on byte streams and character streams, and NiO operates on channel and buffer (buffers), where data is always read from the channels into the buffer or written to the channel from the buffer. The Selector (selection zone) is used to listen for events on multiple channels (e.g., connection open, data arrival). Therefore, a single thread can listen to multiple data channels.
Channel
First of all, the channel, most of the domestic translation into "channels." The stream (stream) in the channel and IO is almost one level. Only the stream is one-way, for example: InputStream, OutputStream. The channel is bidirectional and can be used for both read and write operations.
The main implementations of the channel in NIO are:
- FileChannel
- Datagramchannel
- Socketchannel
- Serversocketchannel
Here's a name to guess why: File io, UDP, and TCP (server and client), respectively. The cases shown below are basically stated around these 4 types of channel.
Buffer
The key buffer implementations in NIO are: Bytebuffer, Charbuffer, DoubleBuffer, Floatbuffer, Intbuffer, Longbuffer, Shortbuffer, respectively, corresponding to the basic data types: Byte, char, double, float, int, long, short. Of course, there are mappedbytebuffer in NiO, Heapbytebuffer, Directbytebuffer and so on not to make a statement here first.
Data can be read from the channel to buffer, or it can be written from buffer to the channel. Here's a diagram:
Selector
Selector runs a single-threaded process for multiple channels, and if your app has more than one channel open, but the traffic is low for each connection, it's convenient to use selector. For example, in a chat server. To use selector, you have to register the channel with selector and then call its select () method. This method will always block to a registered channel with event readiness. Once this method returns, the thread can handle these events, with examples of events such as new connections coming in, data receiving, and so on.
This is the illustration of using a selector to process 3 channel in a single thread:
Java Network Programming-nio