Netty (3)-Java NIO Programming

1. Basic introduction to NIO

1) Java NIO stands for java non-blocking IO, which is a new API provided by JDK. Starting from JDK1.4, Java provides a series of improved input/output new features, collectively referred to as NIO (New IO), which is synchronous and non-blocking .

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

3), NIO has three core parts: Channel (channel) , Buffer (buffer) , Selector (selector)
Three cores of NIO

4) NIO is buffer-oriented or block-oriented programming. The data is read into a buffer that it processes later, and it can be moved back and forth in the buffer when needed, which increases the flexibility in the processing process and uses it to provide a non-blocking high-scalability network.

5) Java NIO's non-blocking mode, a thread sends a request to read data from a channel, but it can only get the currently available data. If there is currently no data available, it will not get anything, instead of keeping the thread blocked , So until the data becomes readable, the thread can continue to do other things. 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. This thread can do other things at the same time.

6) Popular understanding: NIO can handle multiple operations with one thread. Assuming there are 10,000 requests coming, according to the actual situation, 50 or 100 threads can be allocated to process. Unlike the previous blocking IO, 10,000 must be allocated.

7) HTTP2.0 uses multiplexing technology to process multiple requests concurrently with the same connection, and the number of concurrent requests is several orders of magnitude larger than HTTP1.1.

Two, Buffer code

public class BasicBuffer {

    public static void main(String[] args) {
        // 举例说明 Buffer 的使用(简单说明)

        // 创建一个 Buffer,大小为10,即可以存放10个int
        IntBuffer intBuffer = IntBuffer.allocate(10);

        // 向 Buffer 中存放数据
        /*intBuffer.put(10);
        intBuffer.put(11);
        intBuffer.put(12);
        intBuffer.put(13);
        intBuffer.put(14);
        intBuffer.put(15);
        intBuffer.put(16);
        intBuffer.put(17);
        intBuffer.put(18);
        intBuffer.put(19);*/
        for(int i = 0; i < intBuffer.capacity(); i++){
            intBuffer.put( i * 2 );
        }

        // 从 Buffer 读取数据
        intBuffer.flip(); // 将 Buffer 转换,读写切换(!!!)

        while (intBuffer.hasRemaining()){
            System.out.println(intBuffer.get());
        }
    }
}

3. Comparison of NIO and BIO

1) BIO processes data in a stream mode, while NIO processes data in a block mode. The efficiency of block I/O is much higher than that of stream I/O.

2) BIO is blocking, while NIO is non-blocking.

3) BIO operates based on byte stream and character stream, while NIO operates based on Channel and Buffer. Data is always read from the channel to the buffer, or written from the buffer to the channel in. Selector is used to monitor events of multiple channels (such as connection request, data arrival, etc.), so multiple client channels can be monitored using a single thread.

Fourth, the relationship between the three core components of NIO

The relationship between the three core components of NIO
Description of the relationship diagram of Selector, Channel and Buffer:

  1. Each Channel will correspond to a Buffer
  2. Selector corresponds to one thread, and one thread corresponds to multiple Channels (connections)
  3. The figure reflects that there are three channels registered on the Selector
  4. Which Channel the program switches to is determined by the event, which is a very important concept
  5. Selector will switch on each channel according to different events
  6. Buffer is a memory block, and there is an array at the bottom layer
  7. Data reading and writing are achieved through Buffer. This is essentially different from BIO. BIO can be an input stream or an output stream. It cannot be bidirectional, but NIO's Buffer can be read or can be Write, need to use flip function to switch
  8. Channel is two-way, which can reflect the situation of the underlying operating system. For example, the channel of the underlying operating system of LINUX is two-way

Guess you like

Origin blog.csdn.net/yangxshn/article/details/113198154