Three IO modes of Netty series notes

table of Contents

I. Introduction

Second, the characteristics of IO mode

Three, IO three modes

Four, Netty supports three IO modes

Five, Java NIO core components

Six, conclusion


I. Introduction

In network programming, we often hear various terms: blocking, non-blocking, asynchronous, and synchronous. These are the characteristics of the IO mode. This article explains the three modes of IO and briefly mentions the three core components of NIO.

Second, the characteristics of IO mode

1. Blocking and non-blocking

  • Blocking: When data is not transferred, the read will block until data is transferred; when the buffer is full, the write operation is also blocked.
  • Non-blocking: When non-blocking encounters the above situation, it returns directly without waiting.

2. Synchronous and asynchronous

  • Synchronization: After the data is ready, you need to read it yourself.
  • Asynchronous: After the data is ready, read it directly and then call back to the program.

Three, IO three modes

name mode Remarks
BIO Synchronous blocking Blocking IO, started before JDK 1.4
NIO Synchronous non-blocking New IO or Non Blocking IO, since JDK 1.4
AIO asynchronous Async IO

Four, Netty supports three IO modes

BIO -> OIO(Deprecated) NIO AIO(Removed)
Common Linux MacOS / BSD
ThreadPerChannelEventLoopGroup NioEventLoopGroup EpollEventLoopGroup KQueueEventLoopGroup AioEventLoopGroup
ThreadPerChannelEventLoop NioEventLoop EpollEventLoop KQueueEventLoop AioEventLoop
OioServerSocketChannel NioServerSocketChannel EpollServerSocketChannel KQueueServerSocketChannel AioServerSocketChannel
OioSocketChannel NioSocketChannel EpollSocketChannel KQueueSocketChannel AioSocketChannel

From the table above, we can see that Netty supports all three types of IO. To be precise, it has been supported.

1. Why does Netty only support NIO?

  • BIO is called OIO in Netty, or old IO. We know that in the case of a large number of connections, blocking means low performance and resource consumption, so OIO is no longer recommended in Netty.

  • AIO was actually already supported in the Netty 5 version, and it was later deleted some time after the official website was released. The reasons are as follows:

    • The AIO implementation of the Windows system is very mature, but it is rarely used as a server.
    • Linux is often used as a server, but the AIO implementation is not mature enough.
    • Compared with NIO, the performance hint of AIO under Linux system is not obvious.
    • More maintenance costs.

2. Why does Netty have multiple NIO implementations?

NIO has a Common implementation, and there are corresponding implementations for different platforms. The general NIO implementation also uses epoll under Linux, so now that there is a Common implementation, why do we need to implement a different platform?

  • JDK has different versions on different platforms. Netty is designed in this way based on the same considerations. In contrast, Netty exposes more controllable parameters. For example, JDK's NIO implementation defaults to horizontal triggering, and Netty defaults to edge triggering and leveling. The trigger can be switched.
  • Netty achieves less garbage collection and better performance

❤ tips: About epoll level trigger and edge trigger.

In Linux network programming, the kernel uses the epoll mechanism for event triggering. The epoll_wait function is one of the three functions of the epoll interface and is used to poll the occurrence of I/O events.

In plain terms of horizontal triggering: as long as there is data, the epoll_wait function will always return; in plain terms of edge triggering: the epoll_wait function will return only when the socket status changes.

For more information about epoll, if you are interested, you can find out on Google.

3. Is NIO necessarily better than BIO?

Compared with NIO, BIO's code is simple to implement. In a scenario where the number of specific connections is small, the performance of BIO is not inferior to NIO.

Based on the above, to learn Netty, we must have a certain foundation of Java's NIO, so the following pages are a review of Java's NIO knowledge, that is, the following NIO is based on Java itself and has nothing to do with Netty. Please pay attention to friends.

Five, Java NIO core components

NIO consists of three core components:

  • Channel
  • Buffer
  • Selector

Traditional IO is based on Stream operations and is oriented towards byte streams or character streams. The biggest difference between NIO is that it is based on Channel and Buffer operations and is buffer-oriented. NIO can read data at any location through the buffer, which is much more flexible than traditional IO, and it will not block the current thread.

1、Channel

Unlike traditional IO streams, different input and output streams are required for reading and writing, and Channel is bidirectional. This means that it can be used for both read and write operations.

The main implementations of Channel in NIO are:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

2、Buffer

Buffer is a buffer, essentially a memory area. The key Buffer implementations in NIO are: ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer, corresponding to the basic data types: byte, char, double, float, int, long, short. And MappedByteBuffer, HeapByteBuffer, DirectByteBuffer, etc.

3、Selector

A Selector can register multiple Channels, and the Selector continuously performs queries and determines whether these Channels have ready IO operations, such as readable, writable, and network connection completed. Through this mechanism, the next Selector in a single thread can manage multiple Channels.

Six, conclusion

This article introduces the three IO modes, as well as some discussions on Netty's questions about the three modes, and briefly lists the core components of NIO at the end. Next, we will learn about the core components of NIO in detail and learn how to use them. 

Guess you like

Origin blog.csdn.net/qq_46388795/article/details/109036777