Home > Java > javaTutorial > body text

Common IO/NIO models in JAVA

(*-*)浩
Release: 2019-08-23 15:39:37
forward
2750 people have browsed it

Our common IO models include: blocking IO model, non-blocking IO model, multiplexed IO model, signal-driven IO model, asynchronous IO model; below we will briefly introduce the above IO models.

Common IO/NIO models in JAVA

#1. Blocking IO model

The most traditional IO model, that is, blocking occurs during the process of reading and writing data. Phenomenon. When the user thread issues an IO request, the kernel will check whether the data is ready. If not, it will wait for the data to be ready, and the user thread will be blocked and the user thread will hand over the CPU. When the data is ready, the kernel will copy the data to the user thread and return the result to the user thread, and then the user thread will release the block state. A typical example of a blocking IO model is: data = socket.read(); if the data is not ready, it will always be blocked in the read method.

2. Non-blocking IO model

When the user thread initiates a read operation, there is no need to wait, but a result is obtained immediately. If the result is an error, it knows that the data is not ready yet, so it can send the read operation again. Once the data in the kernel is ready and a request is received from the user thread again, it immediately copies the data to the user thread and then returns. So in fact, in the non-blocking IO model, the user thread needs to constantly ask the kernel whether the data is ready, which means that non-blocking IO will not hand over the CPU, but will always occupy the CPU. The typical non-blocking IO model is generally as follows:

while(true){
        data = socket.read();
        if(data!= error){
        处理数据
        break;
    }
}
Copy after login

But there is a very serious problem with non-blocking IO. In the while loop, you need to constantly ask whether the kernel data is ready, which will cause the CPU usage to be very high. High, so generally the while loop is rarely used to read data.

3. Multiplexed IO model

The multiplexed IO model is the most commonly used model at present.

Java NIO is actually multiplexed IO. In the multiplexed IO model, there will be a thread that continuously polls the status of multiple sockets. Only when the socket actually has read and write events, the actual IO read and write operations are actually called.

Because in the multiplexed IO model, only one thread can be used to manage multiple sockets. The system does not need to create new processes or threads, nor does it need to maintain these threads and processes, and only in real IO resources will only be used when there are socket read and write events, so it greatly reduces resource usage.

In Java NIO, selector.select() is used to query whether there is an arrival event for each channel. If there is no event, it will always be blocked there, so this method will cause the user thread to block.

Multiplexing IO mode, multiple sockets can be managed through one thread. Only when the socket actually has read and write events, resources will be occupied for actual read and write operations. Therefore, multiplexed IO is more suitable for situations where the number of connections is large.

In addition, the reason why multiplexed IO is more efficient than the non-blocking IO model is because in non-blocking IO, the socket status is constantly inquired through the user thread, while in multiplexed IO , polling the status of each socket is performed by the kernel, and this efficiency is much higher than that of user threads.

However, it should be noted that the multiplexed IO model uses polling to detect whether an event has arrived, and responds to the arriving events one by one. Therefore, for the multiplexed IO model, once the event response body is large, subsequent events will not be processed for a long time, and new event polling will be affected.

4. Signal-driven IO model

In the signal-driven IO model, when the user thread initiates an IO request operation, a signal function will be registered for the corresponding socket. Then the user thread will continue to execute. When the kernel data is ready, a signal will be sent to the user thread. After receiving the signal, the user thread will call the IO read and write operations in the signal function to perform the actual IO request operation.

5. Asynchronous IO model

The asynchronous IO model is the most ideal IO model. In the asynchronous IO model, when the user thread initiates a read operation, it immediately You can start doing other things.

On the other hand, from the kernel's perspective, when it receives an asynchronous read, it will return immediately, indicating that the read request has been successfully initiated, so no block will be generated for the user thread.

Then, the kernel will wait for the data preparation to be completed, and then copy the data to the user thread. When all this is completed, the kernel will send a signal to the user thread to tell it that the read operation is completed. In other words, the user thread does not need to know how the entire IO operation is actually performed. It only needs to initiate a request first. When it receives the success signal returned by the kernel, it means that the IO operation has been completed and the data can be used directly.

That is to say, in the asynchronous IO model, neither phase of the IO operation will block the user thread. Both phases are automatically completed by the kernel, and then a signal is sent to inform the user thread that the operation has been completed. There is no need to call the IO function again in the user thread for specific reading and writing.

This is different from the signal-driven model. In the signal-driven model, when the user thread receives the signal, it indicates that the data is ready, and then the user thread needs to call the IO function to perform the actual read and write operations; in asynchronous IO In the model, receiving a signal indicates that the IO operation has been completed, and there is no need to call the IO function in the user thread to perform actual read and write operations.

Note that asynchronous IO requires the underlying support of the operating system. In Java 7, Asynchronous IO is provided.

The above is the detailed content of Common IO/NIO models in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template