The difference between BIO, NIO and AIO (concise version)

Please indicate the original address for reprint: http://www.cnblogs.com/ygj0930/p/6543960.html

      One: Event Separator

        When IO is read and written, the IO request and the read and write operations are separated and deployed, and an event separator is required. According to the different processing mechanisms, event separators are divided into: synchronous Reactor and asynchronous Proactor.

        Reactor model:

    - 应用程序在事件分离器注册 读就绪事件 和 读就绪事件处理器
    - 事件分离器等待读就绪事件发生
    - 读就绪事件发生,激活事件分离器,分离器调用 读就绪事件处理器(即:可以进行读操作了,开始读)
    - 读事件处理器开始进行读操作,把读到的数据提供给程序使用

        Proactor model:

       - 应用程序在事件分离器注册 读完成事件 和 读完成事件处理器,并向操作系统发出异步读请求

   - 事件分离器等待操作系统完成读取

   - 在分离器等待过程中,操作系统利用并行的内核线程执行实际的读操作,并将结果数据存入用户自定义缓冲区,最后通知事件分离器读操作完成

   - 事件分离器监听到 读完成事件 后,激活 读完成事件的处理器

   - 读完成事件处理器 处理用户自定义缓冲区中的数据给应用程序使用

    

       The difference between synchronous and asynchronous is who completes the read operation: a synchronous Reactor means that after the program issues a read request, the separator detects that the read operation can be performed (the read operation conditions need to be obtained) to notify the event processor to perform the read operation, asynchronous The Proactor means that after the program issues a read request, the operating system immediately performs the read operation asynchronously. After reading, the separator is notified, and the separator activates the processor to directly access the read data.

      

        Two: Synchronous blocking IO (BIO)

        The well-known Socket programming is BIO, a socket is connected to a processing thread (this thread is responsible for a series of data transmission operations of this Socket connection). The reason for blocking is that the number of threads allowed by the operating system is limited. When multiple sockets apply to establish a connection with the server, the server cannot provide a corresponding number of processing threads, and connections that are not allocated to processing threads will block waiting or be rejected. .

 

         Three: Synchronous non-blocking IO (NIO)

         New IO is an improvement to BIO, based on the Reactor model. We know that a socket connection will only perform data transfer IO operations when it is special. Most of the time this "data channel" is idle, but it still occupies threads. The improvement made by NIO is "one thread for one request". Among the many sockets connected to the server, only those that need to perform IO operations can obtain the processing thread of the server for IO. In this way, socket access will not be limited due to insufficient threads. When the client's socket is connected to the server, an IO request event and an IO event handler are registered in the event separator. When an IO request occurs on the connection, the IO event handler will start a thread to process the IO request, and continuously try to obtain the system's IO usage rights. Once successful (ie: IO can be performed), it will notify the socket to perform IO data. transmission.

         NIO also provides two new concepts: Buffer and Channel

Buffer:
–        是一块连续的内存块。
–        是 NIO 数据读或写的中转地。
Channel:
–        数据的源头或者数据的目的地
–        用于向 buffer 提供数据或者读取 buffer 数据 ,buffer 对象的唯一接口。
–         异步 I/O 支持
Buffer作为IO流中数据的缓冲区,而Channel则作为socket的IO流与Buffer的传输通道。客户端socket与服务端socket之间的IO传输不直接把数据交给CPU使用,
而是先经过Channel通道把数据保存到Buffer,然后CPU直接从Buffer区读写数据,一次可以读写更多的内容。
使用Buffer提高IO效率的原因(这里与IO流里面的BufferedXXStream、BufferedReader、BufferedWriter提高性能的原理一样):IO的耗时主要花在数据传输的路上,普通的IO是一个字节一个字节地传输,
而采用了Buffer的话,通过Buffer封装的方法(比如一次读一行,则以行为单位传输而不是一个字节一次进行传输)就可以实现“一大块字节”的传输。比如:IO就是送快递,普通IO是一个快递跑一趟,采用了Buffer的IO就是一车跑一趟。很明显,buffer效率更高,花在传输路上
的时间大大缩短。

 

          Four: Asynchronous blocking IO (AIO)

          NIO is synchronous IO, because when a program needs IO operations, it must obtain IO permissions and perform IO operations in person before proceeding to the next step. AIO is an improvement on NIO (so AIO is also called NIO.2), which is based on the Proactor model. Each socket connection registers IO completion events and IO completion event handlers in the event separator. When the program needs to perform IO, it sends an IO request to the separator and informs the separator of the buffer area used, and the separator notifies the operating system to perform IO operations. , notify the separator after the operation is completed; when the separator detects the IO completion event, it activates the IO completion event handler, the processor will notify the program that "IO has been completed", and the program will directly read and write data from the Buffer area after knowing it.

          That is to say: AIO is that after an IO request is issued, the operating system obtains IO permissions and performs IO operations; NIO is that after an IO request is issued, the thread continuously tries to obtain IO permissions, and notifies the application to perform IO operations after obtaining it. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173111&siteId=291194637