Java BIO, NIO, AIO

Source: Internet
Author: User
Tags epoll

Synchronous vs. asynchronous

同步与异步The concept of the message communication mechanism is concerned

    • 同步is to issue a request that does not return a result until the result is obtained, and the result is obtained when the request returns.

such as washing clothes, put clothes in the washing machine, not washed before we have been watching, until the washing is ready to take out to dry.

    • 异步Refers to a request that is immediately answered, but does not return a result. We can then deal with something else (sending another request), so this approach requires us to proactively see if there is a result, or we can set a callback to notify the caller.

For example, when washing clothes, put clothes in the washing machine, we can do other things, later to see if there is no washing (through the status of the query);
Or we set the washing machine after washing the bell to notify us to wash it (callback notification)

Blocking and non-blocking

阻塞与非阻塞It is easy and 同步与异步 confusing, but the two concerns are not the same. 阻塞与非阻塞 Focus on the state of the program as it waits for the result to be called

    • 阻塞The current thread is suspended (blocked) until the request results are returned, and the thread cannot do anything
    • 非阻塞This means that the current thread is not blocked until the request results are returned, and can still do other things.

阻塞One obvious feature is that the thread is usually in BLOCKED the state (in the case of the bio read() , the thread is blocked by the JVM with the OS, and the Java gets to the thread's state RUNNABLE but it is actually blocked)

If you want 同步 to compare, a thread in the synchronous mode of communication waits for the result after sending the request, the process should be in RUNNABLE a state, the synchronization must be done step-by-step, as if the code must execute a line to execute the next line, so must wait for the request to return before the next request, Even if the time to wait for the result is long, it is in the process of executing the request. Instead 异步 of waiting for the last one to execute, you can execute other code first, and then get the result after the request has a result.

IO model

The IO operation in Java is done by the JVM with the operating system. For a read operation of an IO, the data is first copied into the buffer of the operating system kernel and then copied from the buffer of the operating system kernel to the application's address space. So the whole process can be divided into two stages:

    1. Wait for I/O data to be ready. This depends on the speed at which the IO target returns data, such as network IO and the size of the data itself.
    2. The data is copied from the kernel buffer into the process.

According to these two phases, several different IO models are produced: BIO , NIO , IO多路复用 and AIO .

BIO

BIOThat is Blocking I/O (blocking I/O), the entire bio process such as:

The program sends the request to the kernel, which is then communicated by the kernel, and the thread is suspended before the kernel prepares the data, so the program is in a suspended state in both phases.

    • Bio is characterized by the block of both phases of IO execution.
Nio

NIOi.e. Non-Blocking I/O (non-blocking I/O), the entire process of NIO such as:

The obvious difference from bio is that, after initiating the first request, the thread is not blocked, it repeatedly checks to see if the data is ready, divides the blocking time that was not used in chunks into a lot of "small blocks" (checks), so the process continues to have an opportunity to be executed. The process of preparing the data for this check is somewhat similar to "polling".

    • NIO is characterized by the need for the program to constantly proactively ask if the kernel data is ready. First stage non-blocking, second stage blocked
IO multiplexing

The advantage of IO multiplexing ( I/O Multiplexing ) is select that poll , in epoll different ways, a single thread can handle multiple network IO at the same time.

NIOThe polling operation is performed by the user thread, and if the task is given to another thread, the user thread does not have to be so laborious to query the state. The system IO多路复用 -level select or model is called to monitor the poll IO status by the system. Select polling monitors the IO requests of many sockets and can be returned when the data for a socket is ready.

    • Select: The register event is managed by the array, the array is of length, 32-bit machine upper limit 1024, 64-bit machine upper limit 2048. The array needs to be traversed when polling for lookups.
    • Poll: The array of select is implemented with the list, so there is no maximum limit
    • Epoll mode: Based on the event callback mechanism, the callback directly notifies the process, no need to use a certain way to view the state.

multiplexed IO process diagram:

The user thread is blocked for a while, from the point of view, and NIO much like, but unlike NiO, select is not waiting until all the data is ready to return, but as long as one is ready to return, it has the advantage of being able to handle multiple connections at the same time. If the connection is not many, its efficiency is not high, It may be worse.

Java 1.4Starting with support NIO(New IO) , this is the way to provide a selection mechanism on a socket selector that, when initiated, select() blocks waiting for at least one event to return.

    • Multiplexing IO is characterized by the ability of the user process to wait for multiple IO requests simultaneously, the system to monitor the IO status, any one of which enters the read-ready state, and the Select function can be returned.
Aio

AIOThat Asynchronous I/O is, (asynchronous I/O), which is Java 1.7 NIO 2.0 used in the introduction. Throughout the process, the user thread initiates a system call without waiting to handle anything else. The operating system waits for the received content, and then copies the data to the user process. Finally notifies the user that the program is ready to use the data, and that both phases are non-blocking. The whole process of aio such as:

AIOis an asynchronous model, the user thread can handle something else at the same time, how do we further process the results? Java provides two methods in this model:

    1. One is based on the "callback", we can implement the CompletionHandler interface, the callback function is passed to the corresponding API when called
    2. The other is to return one Future . After you have done something else, you can isDone() wait for the data to be returned through the method by checking to see if the data is ready get() .
Summary

Above these modes, the BIO whole process waits for the return, NIO and IO多路复用 in the second phase waits for the return, so from the whole process, these three modes are synchronous mode. AIO There is no wait for the whole process to return, which is an asynchronous way.

Java BIO, NIO, AIO

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.