IO model BIO/NIO

preface

IO

I/O problem is a problem that no programming language can avoid. I/O problem is the core of the whole human-computer interaction, because I/O is the main channel for the machine to obtain and exchange information. IO (InputStream/OutputStream) character stream (Writer/Reader) according to the data format. According to the data mode, it is divided into File File OPERATION IO and Socket network operation IO. IO is essentially the process of moving data in and out of memory.Copy the code

Thread block

The essence of Thread blocking is that the Thread releases CPU resources and waits for the operating system to schedule the running Thread again under certain conditions. The causes of Thread blocking are as follows: 1.Thread.sleep() 2. Synchronized /Lock 3. A thread performing IO operations or remote operations will block waiting for related resources (system.in.read () if the user has not entered data into the console, the thread will wait for data input before returning from the read() method) 5. In remote communication, when a request is made to establish a connection with a server, that is, when the thread executes the Socket's parameterized constructor or the Socket's connect() method, it will block until the connection succeeds 6. A thread reading data from a Socket's input stream will block if it doesn't have enough data until it reads enough data, or reaches the end of the input stream, or an exception occurs. How much data is enough to return from the input stream's read() method or abort the input stream? It depends on the type of the read() method that the thread executes int read(); Just one byte in the input stream is enough. int read(byte[] buff); As long as the number of bytes in the input stream is the same as the length of the parameter buff array, it is sufficient. String readLine(); As long as there is a string in the InputStream, it is sufficient. InputStream does not have a readLine method. BufferedReader does. A thread writing a batch of data to the Socket's output stream may block until all data is output or an exception occurs before returning or aborting from the write() method of the output streamCopy the code

Computer Knowledge

A computer consists of IO input and output devices, memory, controller and arithmetic unit. Network adapter (network adapter) is the interface between the host and the network. It is used to coordinate the sending and receiving of data, instructions and information between the host and the network. Each nic is delivered with a hexadecimal network node address, commonly known as a MAC address. The operating system operates on hardware, so it operates on files, essentially it operates on file descriptors, which might be a normal file, A FIFO, a pipe, a terminal, a keyboard, a monitor, even a network connection. Network connections are also defined as a similar IO operation, similar to files, with file descriptors. The return value of the socket() is the file operator when a socket is used to communicate and establish a network connection. With this file operator, data can be transferred using normal file manipulation functions. A network input operation usually consists of two phases: 1. Wait for network data to arrive at the nic --> read into the kernel buffer, and prepare data 2. Copy data from kernel buffer to process spaceCopy the code

The BIO/NIO model figure

BIO

In traditional BIO, socket.read() blocks until the TCP RecvBuffer contains no data, and returns the read data. There are several common defects in the BIO. 1. Establishing a connection is time-consuming and the server IO (input stream) blocks until the client sends data to the server, and then blocks until the server sends the return value back. That is to say in an RPC call before the start to finish, this connection is taken up by the network communication lock, but in the process of the actual call, really need only in the middle of the transmission process of network connection, write on the client and the server read and execute a remote method of these two time points, actually a network connection is free, thus wasting the network resources. 2. Large number of connections Due to the blocking problem in the BIO, each RPC call consumes one connection, and if left unchecked, connections are frequently created and destroyed. Further, thread pooling was introduced to manage this frequent creation, but it didn't solve the root of the problem. If there are far more service consumers than providers, there will be a large number of service connections. A large number of connections cause the operating system to frequently switch context time slices, which also consumes a large amount of resources and causes resource waste. BIO, if you think of it as a model of life, is a cook who cooks a dish, cuts another dish, cooks another dish, and so on.Copy the code

NIO

NIO, called New IO, is a synchronous non-blocking IO model. For NIO, if the TCP RecvBuffer has data, it reads the data from the network card into memory and returns it to the user. Otherwise, it returns 0 directly and never blocks. NIO really solves the blocking problem. Here's a quick look at some of the common roles in NIO: Selector /SocketChannel /Buffer BIO is socket-oriented, and NIO is channel-oriented. A SocketChannel is the equivalent of a Socket in the BIO. It is a real conduit for establishing and transmitting data. This conduit can be used by multiple client threads, which greatly improves the per-connection per-thread problem in the BIO. A Buffer is an object that a SocketChannel communicates with. In essence, it is a memory area. Because SocketChannel does not support reading data directly, all read and write operations must be implemented through the Buffer. Selector is used to monitor SocketChannel events, which is the core of the non-blocking implementation. There are four event states in SocketChannel, op_read is triggered when the write is complete, op_write is triggered when the read is complete, and when the client and server establish a connection, The client receives the OP_CONNECT event, and the server triggers the OP_ACCEPT event. Selector essentially polls each SocketChannel, and if no event is emitted, the polling thread blocks, and if an event is emitted, the corresponding SocketChannel is returned for subsequent processing. If NIO corresponds to a concrete model of life, you can imagine a cook cooking food, only checking the cut and placed on the cutting board each time, and then cookingCopy the code