简述 BIO、NIO、AIO 的区别

IO - two phases

  • data - preparing / ready(kernel wait data to arrive)
  • real - data operation: copy data from kernel to user process.

    BIO:Blocking - IO

    two phases are both blocking until data copy to process.

    NIO: Non-Blocking IO

    Just second phases is asynchronized, when data is ready in kernel, first phases is same to BIO.
    So the user process need to actively ask whether the data in kernel is ready or not.

The difference between BIO & NIO is the state of a programm waiting for the result of a call. BIO refers to the current thread will be suspended(hang up) until the response returns. NIO will not block the current thread while data is not ready. 阻塞与非阻塞的重点在于进/线程等待消息时候的行为,也就是在等待消息的时候,当前进/线程是挂起状态,还是非挂起状态。


阻塞调用在发出去后,在消息返回之前,当前进/线程会被挂起,直到有消息返回,当前进/线程才会被激活.
非阻塞调用在发出去后,不会阻塞当前进/线程,而会立即返回。
BIO, NIO, IO reuse, Signal - Driven IO are Synchronized IO due to the real data operation(copy data from kernel to user process is blocking).

同步与异步的理解

同步与异步的重点在消息通知的方式上,也就是调用结果通知的方式。
同步: 当一个同步调用发出去后,调用者要一直等待调用结果的通知后,才能进行后续的执行。
异步:当一个异步调用发出去后,调用者不能立即得到调用结果的返回。
异步调用,要想获得结果,一般有两种方式:

  1. 主动轮询异步调用的结果;
  2. 被调用方通过callback来通知调用方调用结果。

AIO: Asynchronized IO

When data in kernel is ready, then copy data to process.When all this is done, kernel send a signal to process.

IO 和 NIO 的区别,NIO 优点

下表总结了Java IO和NIO之间的主要区别:

IO NIO
面向流Stream - 读一个/段处理一个,不能缓冲,不能前后移动,不灵活 面向缓冲Buffer - 有缓冲,可以前后移动,更灵活
阻塞IO - process没有返回前会suspend挂起 非阻塞IO - 没有返回前可以做其他事,因而可以多条输入通道, 于是有了selector
选择器Selector

NIO优点:

  • 数据量大
  • 数据高并发

Refer