Java in the world of BIO, NIO, AIO

  As a programmer, io knowledge is essential, in fact, has been dealing in and io, either implicitly or display to the operating system, the next record on io do so. Io said it before, first introduced what Dian blocking synchronous asynchronous non-blocking

1. Dian blocking synchronous asynchronous non-blocking

1.1 refers to issue a synchronization request which does not return results until no result, when the request returns, the results obtained it. For example, I often use the kettle to boil water, staring at the kettle did not boil before (open water, etc.).

1.2 Asynchronous means that after issuing a request and immediately get a response, but no results are returned, then we can then deal with something else (sending other requests), so we need to take the initiative in this way by the state to see whether the outcome, or You can set a callback to notify the caller. For example, when boiling, water bottles and other water do not need to stare at the open, you can also do something else: playing computer every twenty-three minutes (similar to the polling task) look no open water, you can also set ringing (signal to the kettle notification, e-mail text messages), open water informed me (do not call me, I call you).

1.3 refers to blocking requests before results are returned, the current thread is suspended (blocked), the thread can not do anything, while the non-blocking refers to the request before the results are returned, the current thread is not blocked, they can still do other things.

2. IO model

Io request a read operation, the data is first copied to the operating system kernel buffer, then copied from the operating system kernel buffer to the application's address space, so the entire process can be divided into two phases: waiting for I / O the data is ready, depending on the speed IO target return data, look at the size and speed of the data itself, such as when the network IO; copies data from the kernel buffer into the process.

Model classification can be divided into the following categories: BIO, NIO, IO multiplexing, AIO, I was eleven brief introduction

2.1  BIO

BIO,全称是Blocking I/O,中文名叫:Blocking I / O, as shown in the process:

Two stage application sends a request to the kernel, and then to communicate by the kernel, the kernel is ready before the data the thread is suspended, so the two stages of the procedure are in a suspended state, which is characterized by the implementation of the IO They are the block.

2.2 NIO

NIO, stands for Non-Blocking IO, Chinese name is non-blocking IO, the process as shown:

After the first initiated the request, and the thread is not blocked, it repeatedly checks whether the data is ready, the blocking time of the original chunk can not be divided into a number of "small blocking" (check), so the process will continue to have the opportunity to be executed. This check is not ready to process data somewhat similar to the "poll." It is characterized by the need to constantly ask about the program kernel data is ready. The first stage non-blocking, blocking the second stage.

2.3 IO multiplexing (focus, this model uses a lot of middleware)

IO multiplexing ( I/O Multiplexing) has select, poll, epolletc. in different ways, it is advantageous in that a single thread can handle multiple simultaneous network IO. Unlike NIOthe polling operation of the user thread, and IO多路复用call the operating system level selector poll或epollmodels, IO state monitored by the system. select polling can monitor many IO request socket when there is a socket of the data is ready to be returned. FIG IO multiplexing process:

The difference is that with the NIO, select not just wait until all the data is ready to return, but as long as one is ready to return to its strengths is that it can handle multiple connections simultaneously. It features a user can simultaneously process multiple IO requests waiting for the system to monitor the status of IO, any of which read into the ready state, select the function can return. nio was introduced in java1.4I new java

2.4  AIO

AIO全称是Asynchronous I/O,中文名叫:Asynchronous I / O, which is introduced before Java1.7. Throughout the process, the user initiates a system thread after the call without waiting, you can handle something else. By the operating system waits to receive content, after receiving the copy of the data to the user process, and finally informs the user program is already available data, the two phases are non-blocking. The whole process following FIG.

Java can be achieved in two ways:

One is based on the "callback", we can achieve CompletionHandler interface to pass a callback function to the corresponding API to when you call;

One is to return a Future. Finished with something else, you can () to see whether the data is ready by isDone, waiting to return data via the get () method.

Well, for the time being to write so many, more exchanges to explore

reference:

1.  OReilly.Java.I.O.2nd.Edition.May.2006

2. Unix Network Programming Volume 1 (io several model)

Guess you like

Origin www.cnblogs.com/dongguangming/p/12630162.html