How to understand the difference between BIO, NIO, AIO's?

Many articles talking to BIO, NIO, AIO when just throw a bunch of definitions, as well as vivid examples. Seemingly well understood. But not the essence of the most basic principles emerge, if not, then there is no departure from the principle of IO is difficult to understand the difference between these three. So this article is to analyze how Java is beginning from IO operation.

IO principle in Java

First in Java IO are dependent on the operating system kernel, IO read and write our program actually calls the operating system kernel read & write two system calls.

That is how the kernel IO interact with it?

  1. NIC receive data through the network cable coming from, and network data is written to memory.
  2. When data is written to the memory card, cpu card issued to an interrupt signal, the operating system will tell you whether there is new data arrives, and then to process the data through the network card interrupt program.
  3. The network data is written to the receive buffer memory in the corresponding socket.
  4. After data is written to the receive buffer, data processing application starts.

abstract socket corresponds to the code java simple example as follows:

public class SocketServer {
  public static void main(String[] args) throws Exception {
    // 监听指定的端口
    int port = 8080;
    ServerSocket server = new ServerSocket(port);
    // server将一直等待连接的到来
    Socket socket = server.accept();
    // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
    InputStream inputStream = socket.getInputStream();
    byte[] bytes = new byte[1024];
    int len;
    while ((len = inputStream.read(bytes)) != -1) {
      //获取数据进行处理
      String message = new String(bytes, 0, len,"UTF-8");
    }
    // socket、server,流关闭操作,省略不表
  }
}

复制代码

This process can be seen that the underlying kernel and network IO is very similar, mainly in the accept () wait for a request from the network and bytes [] array after waiting to be processed as a buffer filled with data. The difference between BIO, NIO, AIO is that these operations are synchronous or asynchronous, blocking or non-blocking.

So we elicit synchronous asynchronous, non-blocking and blocking concept.

Synchronous and asynchronous

Synchronous and asynchronous refers to whether a process execution method must rely on each after the completion of the previous method can continue. We assume that the execution process: turn is a method and a second method.

Synchronization means that once you start calling, the caller must wait until after the method call returns, in order to continue the follow-up actions. That second method must wait until after the completion of the implementation of a method can be performed.

Asynchronous refers to the call to return immediately, without waiting for the caller code execution method in the end, we can continue with the subsequent behavior. (Code within the specific method referred to another thread execution is completed, may be callbacks). That method of performing a time, directly to other threads, are not the main thread of execution, it will not block the main thread, the second method does not have to wait until the completion of a method to start with.

Synchronous and asynchronous concerned about the method of execution is a main thread or other thread, the main thread then need to wait for the completion of the implementation of the method, other threads, then return immediately without waiting for the method call, the main thread can execute the following code directly.

Synchronous and asynchronous from coordination between multiple threads to achieve efficiency differences.

Why do we need asynchronous it? I believe that the essence is to solve the asynchronous block the main thread, so a lot of discussion online to synchronize asynchronous, non-blocking has been blocked four combinations, one of which had asynchronous blocking this situation, if asynchronous is blocked? So why does it specifically asynchronous?

Blocking and non-blocking

Blocking and non-blocking refers to the synchronous waiting encountered within a single thread, whether or not in place does nothing.

After blocking refers to the experience synchronization waits until synchronization method in place waiting for processing is complete.

Non-blocking refers to the encounter synchronization wait, not wait and to do other operations, cut off time again observe synchronization method is complete.

Blocking and non-blocking concern is whether the thread to wait in place.

I believe that blocking and non-blocking can only be carried out simultaneously with the combination. The natural is asynchronous non-blocking, non-blocking and this is the main thread concerned. (Some people may think that the asynchronous method, then placed inside blocking operation is asynchronous blocking, but think about, precisely because it is a blocking operation will put it in an asynchronous method, do not block the main thread)

Examples explain

Sea fishing to eat well, but often have to queue. We are in this life examples to explain.

  • A customer eat sea fishing, so sit and waited an hour, then began to eat hot pot. (BIO)
  • B customers to eat sea fishing, he saw quite a long time to wait, then go shopping, visiting each one will be routed back to look at him there. So he not only purchased last thing, and eat a sea fishing. (NIO)
  • C customers to eat sea fishing, because he is a senior member, the manager said, you go to the mall just play, and so on down the position, I immediately call you. So do not sit and C customers, etc., do not look back to every once in a while would not have to wait until, finally eat the sea fishing (AIO)

Which way is more efficient? Is not it clear?

BIO

BIO stands for Blocking IO, IO is the traditional model before JDK1.4, itself a synchronous blocking mode. After the thread initiated IO requests, it has been blocking IO, until the buffer data ready after, and then enter the next step. For network communications are a way to request a response, although the upper simplified application development, but there is a huge bottleneck in performance and reliability, Imagine if every request needs to create a new thread to deal with, then high concurrent scenes, machine resources will soon be exhausted.

NIO

NIO also known as Non-Blocking IO IO model is synchronous non-blocking. After initiating io request thread returns immediately (non-blocking io). Synchronization refers to the need to wait for the data in the IO buffer zone in place, while non-blocking means that the user does not place a thread waiting for IO buffers, can do some other operations, but to regularly check the polling data IO buffer is ready . Java NIO is the new IO meaning. Plus the fact NIO IO multiplexing technology. NIO is a common thread to view a polling IO buffer is ready, but the new IO in Java refers to a polling thread to go see a bunch of IO buffer which is ready, it is a thought IO multiplexed . IO multiplexing model, check whether the IO data ready task to select or epoll system-level model, monitored by the system, reduce the burden on the user thread.

NIO main buffer channel integration,, Selector three techniques, by zero-copy data acquisition buffer, for each channel by a client registered on the selector (multiplexer). Continuously polls the server channel to obtain information of the client. There connect the channel, accept (blocking), read (read), write (write) four states identified. To subsequent operations according to the identification. So a server can receive an unlimited number of channel. You do not need to open a new thread. Greatly enhance the performance.

AIO

AIO is an asynchronous non-blocking IO model in the true sense. NIO to achieve the above, the user thread needs regular polling to check whether the IO buffer data ready, thread resource-intensive applications, in fact, the equivalent of polling still blocked, the current thread is not real liberation, because it still needs to find out which IO ready. The real ideal of asynchronous non-blocking IO should let the system kernel is complete, users only need to tell the kernel thread, when the buffer is ready, inform me or execute me over to your callback function.

AIO can be truly asynchronous operation, but the implementation is more complex, asynchronous IO support pure operating system is very small, there are also windows IOCP technology, while on Linux, the underlying or is used epoll implementation.

I personally understand summary, if error correction urge Reviews.

Transfer from my personal blog vc2x.com

Guess you like

Origin juejin.im/post/5dbba5df6fb9a0204a08ae55