Some superficial understandings of Java BIO, NIO, AIO

Source: Internet
Author: User

1. Blocking and non-blocking

The "blocking" and "non-blocking" concepts are often confused with "synchronous" and "asynchronous". In Java programs, many threads are usually in a blocking (blocking) state, while synchronization (not the synchronized of multithreaded synchronization) is not the case, and synchronization usually refers to steps that need to be done in a step-by-step manner, like regular code execution. But async can execute the next line of code without executing the current line of code, like many JS code, UI controls, background startup threads, and so on.

In relation to blocking, the synchronized program thread should be in the running state. The thread is in the blocking state almost as if it were sleep, that is, it can do nothing, only to wait until a signal is generated to wake up. Threads in the running state are active, and in this case you can do a lot of other things and you can always check to see if something is done.


2. BIO (Blocking I/O)

Blocking I/O (blocking I/O), this is the oldest Java communication mechanism in which an application sends a request to the kernel (Kernel) through system call, and then the kernel goes through the network communication, if the application initiates a system invocation that is read operation, Before the kernel prepares the data, the thread will be suspended, waiting until the returned data is ready in the kernel, or the timeout is awakened after setting the SoTimeout.

If the function of the timeout mechanism in the network wait mechanism is thrown aside, the process of initiating the read () operation to wait for the remote return data consists of the following two phases.

Phase 1: waits for I/O to return data, depending on the speed at which the target of the I/O request returns data. For example, in a network I/O request, some processing is required to return data before the data is returned remotely, and the remote output data will depend on the speed of the network itself and the size of the data itself.

Phase 2: The return data is first populated into the kernel (Kernel) buffer, and then the data is copied from the kernel area to the process internally (copy data from Kernel to user). Once this process is complete, the application will continue to execute downward.

Depending on the process above, the process of interacting with the kernel in the bio process is as follows:



2. NIO (New I/O)

Java supports NIO (New IO) from version 1.4, which enables non-blocking I/O. The obvious difference between nonblocking I/O and blocking I/O is that when the first system call request occurs, the thread is not blocked, but it does not do anything else, but is constantly initiating the system call request.

Such an operation seems to wasting the CPU, giving the impression that it is not as good as hanging up waiting to return, which at least yields CPU resources. In fact, every time system call just look at the data is ready to do not, usually its time is very short, so that the action is only one thread to complete the monitoring of many events. In other words, other threads can do other things, only need a thread regularly to do the next test, in the design to pay attention to the detection frequency, you can achieve high efficiency and resource saving purposes.



3. AIO (asynchronous I/O)

JDK 1.7 introduces NIO 2.0, or AIO, also known as asynchronous IO. The following is a "logistics delivery" such as the IO model in Java:

①bio: You need to go to the logistics station to wait for the goods and can not leave the transfer station, if the goods do not arrive, other things do not want to do.

②nio: Every day to check whether the goods arrived, this action is very simple, you can let each community send 1 people to see if there is a community of goods, if there is to bring back, or this person inform you to the logistics station to take goods.

③aio: When the goods arrive at the door, in other words, the road to get the goods is not long, but by others to help you bear.



Using AIO to read a file does not mean that the read is faster, and we cannot use AIO to read a large file and compare the speed with bio, which makes no sense. Because AIO is designed to do other things in the I/O process, which means that less resources can do more things in the process of concurrency, not to see who is faster in I/O, nor does it, because I am able to do I/O faster only by increasing the speed of the disk or the network itself, and these I/O models are just scheduling i/ O's mechanism.

Aio relative to NiO, in a sense more improved resource utilization, but this is only relative process itself, for the entire server, but also to see the actual situation, because the process does not want to do things to the kernel to do. AIO should be more suitable for use in I/O intensive systems, and bio is not worthless, it makes interaction easier, and in many cases it is not necessary to use asynchronous and non-blocking.


Some superficial understandings of 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.