- The I/O model is simple to understand: is what kind of channel to send and receive data, largely determines the performance of program communication
- Java supports three network programming models /IO modes :BIO, NIO, and AIO
- Java BIO : Traditional synchronous and blocks (blocking), the server implementation pattern for a link to a single thread, namely the client when a connection request server will need to start a thread for processing, if the connection is not doing anything will cause unnecessary thread overhead, BIO method is suitable for the number of connection is relatively small and fixed architecture, This approach requires a lot of server resources, the concurrency is limited to the application, the only choice before JDK1.4, but the program is simple and easy to understand.
- Synchronous non-blocking, server implementation mode for a thread to handle multiple requests (connection), namely, the client sends connection requests will note copies to the multiplexer, multiplexer polling to connect I/O request is processed, the NIO and the connection mode is suitable for the connection number is shorter (light), the architecture of the chat server, for example, barrage system, Communication between servers, etc. Programming is more complex and is supported in JDK1.4.
- Java AIO(NIO.2) : Asynchronous non-blocking, AIO introduces the concept of asynchronous channel, using Proactor mode, simplify the program writing, effective request to start the thread, its characteristics is completed by the operating system before informing the server program to start the thread to deal with, generally applicable to the number of connections and connection time is long applications. AIO mode is used in the architecture with a large number of connections and long connections (heavy operation), such as photo album server. It fully calls OS to participate in concurrent operations, and the programming is complicated, which is supported by JDK7.
Java BIO problem analysis
- Each request needs to create a separate thread, with the corresponding client for data Read, business processing, data Write.
- When the number of concurrent connections is large, a large number of threads need to be created to process connections, occupying large system resources.
- After a connection is established, if the current thread has no data to Read temporarily, the thread blocks on the Read operation, resulting in a waste of thread resources
Nio is introduced
- Java NIO stands for Java non-blocking IO, a new API provided by the JDK. Starting with JDK1.4, Java offers a New set of improved input/output features, collectively known as NIO(New IO), that are synchronous and non-blocking
- Nio-related classes are placed in the java.nio package and subpackages, and many of the classes in the original java.io package are overwritten. [Basic Case]
- NIO has three core parts: channels, buffers, and selectors.
A diagram of Selector, Channel, and Buffer
- Each channel has a Buffer
- Selector for one thread, one thread for multiple channels.
- This diagram reflects that there are three channels registered to the selector
- Which channel the program switches to is determined by events, and events are an important concept
- Selector switches between channels based on different events
- A Buffer is a block of memory with an array at the bottom
- The data is read and written through the Buffer, and this and the BIO, which is either an input stream or an output stream, can’t be two-way, However, NIO’s Buffer can be read or written. The flip method is required to switch the channel to be bidirectional, which can return to the situation of the underlying operating system, such as Linux, where the channel is bidirectional.
Buffer (Buffer)
Buffer: A Buffer is essentially a block of memory that can read and write data. You can think of it as a container object **(including arrays)** that provides a set of methods that make it easier to use a block of memory. A Buffer object has built-in mechanisms for tracking and logging changes in the state of the Buffer. Channel Provides a Channel for reading data from a file or network. However, the data read or written must go through Buffer.
channel
NIO channels are similar to streams, but with some differences as follows:
- Channels can read and write simultaneously, while streams can only read or write
- Channels can read and write data asynchronously
- A channel can either read data from the buffer or write data to the buffer:
- A STREAM in BIO is unidirectional. For example, a FileInputStream can only read data, while a Channel in NIO is bidirectional and can be read or written.
- A Channel is an interface in NIO
public interface Channel extends Closeable{}
- Common Channel classes are: FileChannel, DatagramChannel, ServerSocketChannel, and SocketChannel. ServerSocketChanne Similar to ServerSocket, SocketChannel similar to Socket
Selector schematic and feature description
- Java NIO, with non-blocking IO. You can have one thread, handle multiple client connections, and use a Selector.
- The Selector can detect if an event has occurred on multiple registered channels (note that multiple channels can be registered to the same Selector as an event), and if an event has occurred, it retrieves the event and handles each event accordingly. This allows you to manage multiple channels with a single thread, that is, multiple connections and requests.
- Reading and writing occurs only when a connection/channel actually has a read or write event, greatly reducing the overhead and eliminating the need to create a thread for each connection and maintain multiple threads
- Avoids the overhead of context switching between multiple threads
-
Netty’s NioEventLoop, an I/O thread that aggregates selectors, can process hundreds or thousands of users simultaneously
Client connection.
-
When a thread reads or writes data from a client Socket channel, it can perform other tasks if no data is available.
-
Threads typically spend the idle time of non-blocking IO performing IO operations on other channels, so a single thread can manage multiple inputs and outputs
Channel.
-
Since read and write operations are non-blocking, this can greatly improve the efficiency of THE I/O thread and avoid thread hangs due to frequent I/O blocking
Up.
-
A single I/O thread can concurrently process N client connections and read/write operations, which fundamentally solves the traditional synchronous blocking I/O connection-thread model, and greatly improves the performance, flexibility and reliability of the architecture.