Interpretation of the three core components of JAVA NIO in two thousand words

Before explaining the use of NIO to realize the communication architecture, we need to understand the basic characteristics and use of NIO.

1.1 Basic introduction to Java NIO

  • Java NIO (New IO), also known as java non-blocking IO, is a new IO API introduced since Java 1.4, which can replace the standard Java IO API. NIO has the same function and purpose as the original IO, but it is used in a completely different way. NIO supports buffer- oriented , channel -based IO operations. NIO will perform file read and write operations in a more efficient manner. NIO can be understood as non-blocking IO. The read and write of traditional IO can only block execution, and the thread cannot do other things during reading and writing IO. For example, when socket.

  • NIO-related classes are placed under the java.nio package and sub-packages, and many classes in the original java.io package are rewritten.

  • NIO has three core parts: Channel (channel), Buffer (buffer), Selector (selector)

  • The non-blocking mode of Java NIO enables a thread to send a request or read data from a channel, but it can only get the currently available data. If no data is currently available, nothing will be obtained, instead of keeping the thread blocked, so 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 need to wait for it to be completely written. The thread can do other things at the same time.

  • Popular understanding: NIO can handle multiple operations with one thread. Suppose there are 1000 requests, depending on the actual situation, 20 or 80 threads can be allocated for processing. Unlike the previous blocking IO, 1000 have to be allocated.

1.2 Comparison of NIO and BIO

  • The IO stream processes one or more bytes at a time, which is very slow (the character stream also processes bytes, and only encodes and decodes the bytes). The NIO stream is processed in units of data blocks, and the buffer is the data block used for reading and writing. The IO operation of the buffer is implemented by the underlying operating system, and the efficiency is very fast.
  • BIO is synchronously blocking, while NIO is synchronously non-blocking.
  • BIO operates based on byte stream and character stream, while NIO operates based on Channel (channel) and Buffer (buffer). Data is always read from the channel to the buffer, or written from the buffer to the channel.
  • Selector (selector) is used to monitor events of multiple channels (for example: connection request, data arrival, etc.), so multiple client channels can be monitored using a single thread
NIO BIO
Buffer-oriented (Buffer) Stream-oriented (Stream)
Non-blocking (Non Blocking IO) Blocking IO (Blocking IO)
Selectors

1.3 Schematic diagram of the three core principles of NIO

NIO has three core parts: Channel (channel), Buffer (buffer), Selector (selector).

A channel represents an open connection to an IO device (eg: file, socket). If you need to use the NIO system, you need to obtain the channel used to connect the IO device and the buffer used to hold the data. Then operate the buffer and process the data. In short, Channel is responsible for transmission and Buffer is responsible for storage

Buffer buffer

Buffer itself is a container, called a buffer. A buffer is essentially a block of memory to which data can be written and then read from.

This piece of memory is packaged as a NIO Buffer object, and a set of methods are provided for easy access to this piece of memory.

Compared with direct operations on arrays, the Buffer API is easier to operate and manage.

The difference between stream-oriented and buffer-oriented

Traditional IO stream

image.png

Explain the above picture:

①We need to read the data in the disk file or network file into the program. We need to build a pipeline for data transmission. It turns out that what we transmit data directly is the flow of byte data in the pipeline (we made a byte array to transfer data back and forth), so the original IO faces a data flow in the pipeline, so we say that the original IO is stream-oriented

② Another characteristic of traditional IO is that it is one-way. The explanation is: if we want to read the data of the target location into the program, we need to build a pipeline, which we call the input stream. Correspondingly, if there is data in our program that we want to write to the target location, we also have to create another pipeline, which we call the output stream. So we say that the traditional IO flow is unidirectional

NIO

image.png

① We said that as long as it is IO, it is for the purpose of completing data transmission.

②Even if you use NIO, it is also for data transmission, so if you want to complete data transmission, you have to build a channel for data transmission. You can’t understand this channel as the previous water flow, but you can understand it as a railway. The railway itself cannot complete the transportation. The railway must rely on the train to complete the transportation. So pay attention that the channel itself cannot transmit data. If you want to transmit data, you must have a buffer. You can completely understand this buffer as a train. So we say that the original IO is unidirectional and the current buffer is bidirectional. This method of transmitting data is also called buffer-oriented. To sum up, the channel is only responsible for the connection, and the buffer is responsible for storing data.

Channel

The channel of Java NIO is similar to the stream, but there are some differences: data can be read from the channel, and data can be written to the channel. But stream (input or output) reading and writing are usually one-way.

Channels can be read and written non-blockingly, and channels can support reading or writing buffers, as well as asynchronous reading and writing.

Selector selector

Selector is a Java NIO component that can inspect one or more NIO channels and determine which channels are ready for reading or writing. In this way, a single thread can manage multiple channels, thereby managing multiple network connections and improving efficiency

image.png

  • Each channel will correspond to a Buffer
  • A thread corresponds to a Selector, and a Selector corresponds to multiple channels (connections)
  • Which channel the program switches to is determined by the event
  • Selector will switch on each channel according to different events
  • Buffer is a memory block, and the bottom layer is an array
  • The reading and writing of data is done through Buffer. The BIO is either an input stream or an output stream, which cannot be bidirectional, but the NIO Buffer can be read or written.
  • The core of the Java NIO system lies in: channel (Channel) and buffer (Buffer). A channel represents an open connection to an IO device (eg file, socket). If you need to use the NIO system, you need to obtain the channel used to connect the IO device and the buffer used to hold the data. Then operate the buffer and process the data. In short, Channel is responsible for transmission, and Buffer is responsible for accessing data

Guess you like

Origin blog.csdn.net/wdj_yyds/article/details/131826685