Network IO - BIO and NIO

BIO (synchronous blocking): Each IO request, there will be a thread to handle, if the data is not ready, the thread will wait. Until the data has been read threads will be released during this time, the process does not go back and do any other task, this model will waste some of the thread resources.

NIO (non-blocking synchronization): NIO advantage is that the first read-write file-based caching, batch operation is possible, and then to read and write data bi-directional channel, each open breaking the flow reducing resource consumption. Selecore introduce the concept of using a thread to manage multiple channels, greatly reducing the overhead of thread.

Multiplexing: If each IO request requires a process to deal with, if the number of clients have no control, then how many how many processes will need to deal with IO requests, so-called multi-process concurrent processing. The problem is that this mode of consumption of process resources. If you open up a process in which multiple channels, then the process is ready to go back to the polling channel, the selected channel to read and write, this is the multiplexing. And the selector is a NIO be multiplexed. There are a variety of other implementations in operating system design, select, poll, epoll.

Channel: different from the stream, Channel is the concept of a channel. Write general unidirectional flow, the Channel is bidirectional. Channel asynchronously read and write data. Channel carrier must be read Buffer, the stream data can not be read directly.

Read processing of documents: FileChannel

DatagramChannel: UDP write data network

SocketChannel: TCP read data network

ServerSocketChannel: for the server, monitor TCP connections, create new for SocketChannel

Selector: Selector main idea is multi-channel multiplexing techniques mentioned above, this design has several advantages. First, a process can manage multiple channels, channel poll selected ready for processing, which avoids blocking thread to wait, better use of resources. Second, the cost can be reduced among the plurality of threads where the thread context switch.

Creating Selector: Selector.open ();

Sign Channel to Selecter: channer.register (selector, Selectionkey.OP_READ), pay attention to where you need to Channel is set to non-blocking mode, channel.configureBlocking (false).

Interested in acquiring interest of all collections: selectionkey.interestOps ().

Get ready pre-operation set: selectionkey.readyOps ().

获取Channel:selectionkey.channel()。

获取Selector:selectionkey.selector()。

Close Selecter: close ().

Wake Selector: wakeUp ().

Select the channel select (): Returns the event of interest-ready channel.

Buffer: To solve a thread IO request needs have been waiting for the data stream, thereby blocking the thread in question. Buffer is designed to be written when the flow reaches the cache, threads unnecessary obstruction in the meantime, until the read and write operations when the thread again ready to read data from the buffer quantities.

Buffer principle: a memory for reading and writing data on Buffer nature, by a batch process stream buffer to achieve efficient purpose.

capacity: Capacity memory block, to be determined by how much the block buffer memory capacity data. Once full capacity, capacity needs to be emptied before writing the data again.

position: position, location of the memory address of the current operation unit, the initial value of 0 and a maximum capacity-1. When the read and write data, position will move backward a Buffer unit.

limit: limit values, in write mode equal to the capacity limit capacity. In switching to the write mode, limit equal to the memory address currently being written, i.e. position.

Buffer approach

Buffer分配:ByteBuffer.allocate()。

Buffer Write Data: channel.read (buf) from channel writes buffer, buf.put (1) written directly to buffer.

Buffer Read Data: channel.write (buf) read data from the buffer to the buffer, buf.get () to read data from the buffer inside.

Switch to read mode: flip (), the position is set to 0, and to set the limit position. Start reading data.

Clear Data: clear (), position set back to 0, limit capacity is set to the value.

compact (): read data is not copied to the beginning of the buffer, position data set after a read address has not, then writing the write data will never start the read data does not overwrite previous data.


 

Guess you like

Origin blog.csdn.net/yuhaibao324/article/details/93151135