Netty (7)-Selector

1. Basic introduction to Selector

1) Java's NIO uses non-blocking IO mode. You can use one thread to handle multiple client connections, and then the Selector will be used

2) Selector can detect whether events have occurred on multiple registered channels (note: multiple Channels can be registered to the same Selector in the form of events), if an event occurs, get the event and then perform corresponding actions for each event deal with. In this way, you can use a single thread to manage multiple channels, that is, manage multiple connections and requests.
Selector execution principle
3). Only when the connection/request actually has a read and write event, will the read and write be performed, which greatly reduces the system Overhead, and there is no need to create a thread for each connection, and there is no need to maintain multiple threads

4) Avoid the overhead caused by context switching between multiple threads

Two, Selector feature description

Selector execution principle

1) Netty's IO thread NioEventLoop aggregates Selector (selector, also called multiplexer), which can handle hundreds of client connections concurrently.

2) When a thread reads and writes data from a client Socket channel, if no data is available, the thread can perform other tasks.

3) Threads usually use the idle time of non-blocking IO to perform IO operations on other channels, so a single thread can manage multiple input and output channels.

4) Since the read and write operations are non-blocking, this can fully improve the operating efficiency of the IO thread and avoid thread suspension caused by frequent IO blocking.

5) One IO thread can concurrently process N client connections and read and write operations, which fundamentally solves the traditional synchronous blocking IO one connection one thread model, and the performance, elastic scalability and reliability of the architecture have been greatly improved The promotion.

Three, the Selector class and related methods

The Selector class is an abstract class public abstract class Selector implements Closeable, the common methods and descriptions are as follows:

method description
public static Selector open() Get a selector object
public abstract int select(long timeout) Monitor all registered channels. When there are IO operations available, add the corresponding SelectionKey to the internal collection and return. The parameter is used to set the timeout period.
public abstract Set<SelectionKey> selectedKeys() Get all SelectionKey from the internal collection

Note:
1) The function of ServerSocketChannel in NIO is similar to ServerSocket, and the function of SocketChannel is similar to Socket.

2), selector related method description
selector.select(); block
selector.select(1000); block for 1000 milliseconds, return
selector.wakeup() after 1000 milliseconds ; wake up selector
selector.selectNow(); do not block, return immediately

Guess you like

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