Java NIO AIO BIO

BIO

Block-IO: InputStream and OutputStream, Reader and Writer. Belonging to the synchronous blocking model

Synchronous blocking: a request to take up a process to handle, ready to wait for the data, and then copy the data from the kernel to the process, the final processed data is returned

 

 

NIO

NonBlock-IO: Channel, Buffer, Selector. Synchronous non-blocking IO model belongs multiplexed

Non-blocking synchronization: a first process is set to a non-blocking socket is ready to wait for data in the kernel, this process is repeated polling data if the kernel is ready, finally, after processing the data ready to return

 

 

IO multiplexing: non-blocking synchronization optimized version, except that the IO multiplexing blocking system call on such a select, epoll, but there is no obstruction in the real IO system calls. In other words, the polling mechanism is optimized to notification mechanism, blocking a public target multiple connections, processes only need to wait on a blocking object no longer need to poll all connected

 

 

In the Java-NIO, and is based on the Channel Buffer operation, data is always read from the channel to the buffer, or written from the buffer to the channel. Selector event listener for a plurality of channels (for example: open connection, data arrives)

Thus, a single thread can monitor multiple channels of data, the underlying Selector implementation is epoll / poll / select the IO multiplexing model, select the method will block until the channel has an event ready:

 

 

And BIO differences are as follows:

  1. Interactions rather than the data stream by way of the buffer, the stream is no room for direct operation of the data transmission, the buffer provides a flexible data handling
  2. NIO is non-blocking, meaning that each socket connections can make the underlying operating system to help us complete without the need to open each thread to stay connected, using the channel selector to listen to all of the state to achieve
  3. NIO direct memory reproduction mode, eliminating the memory read and write operations between the JVM and system losses

AIO

Asynchronous IO: in matters of non-blocking asynchronous callback mechanism and model AIO way to get results:

  • Based callback: achieve CompletionHandler, trigger callback function when calling
  • Returns the Future: by isDone () to see if ready by get () returns the data waiting

但要实现真正的异步非阻塞IO,需要操作系统支持,Windows支持而Linux不完善

发布了142 篇原创文章 · 获赞 31 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_38905818/article/details/103803500