BIO, 与 NIO AIO

This article explains NIO

BIO (Blocking I / O) - blocking IO

1. Whether or OutputStream is used to read or write data from the network using the InputStream from local, are likely to be blocked off, and thus lose the right to use the CPU, when faced with large-scale traffic and to higher performance requirements of the time, and in this way show insufficient.

2. Of course, also possible to use multiple threads to process multiple requests, even thread pool may be used to improve the performance of the server. But this is the case, would create many problems, such as we have to be achieved by setting the thread priority would be difficult to complete higher priority services.

3. In addition, these threads will inevitably involves the issue of access to competition for resources, to handle multiple threads competing resource problem is more complicated than processing a single thread. => So NIO came into being.

NIO (non-blocking I / O) - nonblocking IO

NIO Overview

NIO substantially comprises three main components Selector, Channel, Buffer.

1. Channel is equivalent to a channel for transmitting data, Buffer equivalent channel specific carrier used to transmit data, Selector equivalent to a scheduler management channel manager schedule.

2. In the conventional BIO, the programmer can see two things and the Socket Stream, but they can not manipulate the internal data transmission.

3. And in NIO, Buffer Stream like concrete, allowing the programmer to control the data transmission / receiving length, such as when using BIO, sendQ using the write methods for writing data, when the data length of the write-once exceeds sendQ the length, but also incoming data divides, this data is the most time-consuming of time, the data need to switch from user space to kernel space, the switching process is beyond the control of the programmer. However, to control the use of the NIO Buffer Buffer capacity expansion whether, how to effectively avoid the expansion costs from data space consumption.

* Where sendQ do not know how you can go and see the computer network to accept the TCP / transmit queue

4. As the package Socket information, we do not know how much data is transmitted in the transmission process, not the number of transmission, but we know that Channel by channel number can continue transmitting data. In addition we can also monitor all selector channel has been registered to the scheduler.

Examples of the use of NIO - meaning also respective portions comments in the code

Examples of the chiefs wrote it very clear example

NIO in focus - Buffer works

1. The four key indexes:

image

2. In practical use, such as:


//创建一个24个的byte数组缓冲区,初始状态 position指向数组起始位置0,limit与capacity都是数组长度位置

ByteBuffer.allocate(24);

//当需要将10个字节写入channel信道时,就使用如下,position就会移动到10的数组位置,limit和capacity不变。

byteBuffer.put(strings.getBytes());  

//然后使用如下方法,这个时候操作系统就能够从缓冲区里面将这10个字节发送出去了。position重回到0,limit变为之前position位置即10,capacity不变。

byteBuffer.flip();

//然后客户端write Buffer就发送了数据

client.write(byteBuffer);  

// ps:在下次写数据之前调用一下clear(),Buffer索引回归起始状态

// 使用mark(),可以将当前position的前一个位置记录下来,当调用reset的时候position会恢复mark记录下来的值

复制代码

Better than traditional file access method NIO file access method - FileChannel.transferTo / transferFrom and FileChannel.map

FileChannel.transferTo/transferFrom:

1. Why is better than the traditional, such as the traditional file access requires data file is read from disk into kernel space, and then read from kernel space to user space, some custom actions in user space, then the operation after the data is read into the kernel space, saved to disk.

2. And transferTo / transferFrom need only read data from the disk to the kernel space, and then directly operate in the kernel space, and then written to disk on the line. Which one is better, obviously.

##### FileChannel.map

1. The map is then how to operate it? It shows the file mapping in a certain size to a memory area, when the program access data directly operating this memory area, but this approach is only suitable for large files read-only operation. Such as MD5 checksum.

AIO (asynchronous I / O) - non-blocking asynchronous IO

When you need to achieve the thread when the asynchronous call can be used AIO

To sum up their scenarios:

1. BIO method is applicable to a relatively small number of connections and fixed infrastructure, limited concurrent applications.

2. NIO connection number suitable for multi-mode and connected to short (light operation) architecture, such as chat server, limited concurrent applications.

3. AIO way to use more than the number of connections and the connection is relatively long (heavy operation) architecture, such as the album server, call the OS to fully participate in concurrent operation.

Reference books - "In-depth analysis of Java Web Technology Insider"

Guess you like

Origin juejin.im/post/5d6a0673f265da03b8107695