Common IO/NIO models in JAVA
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.

#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;
}
}
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!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1793
16
1737
56
1588
29
267
587
Tips for Writing PHP Comments
Jul 18, 2025 am 04:51 AM
The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.
PHP Development Environment Setup
Jul 18, 2025 am 04:55 AM
The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.
PHP Comparison Operators
Jul 18, 2025 am 04:57 AM
PHP comparison operators need to pay attention to type conversion issues. 1. Use == to compare values only, and type conversion will be performed, such as 1=="1" is true; 2. Use === to require the same value as the type, such as 1==="1" is false; 3. Size comparison can be used on values and strings, such as "apple"
PHP Commenting Syntax
Jul 18, 2025 am 04:56 AM
There are three common ways to use PHP comments: single-line comments are suitable for briefly explaining code logic, such as // or # for the explanation of the current line; multi-line comments /*...*/ are suitable for detailed description of the functions or classes; document comments DocBlock start with /** to provide prompt information for the IDE. When using it, you should avoid nonsense, keep updating synchronously, and do not use comments to block codes for a long time.
Go for Audio/Video Processing
Jul 20, 2025 am 04:14 AM
The core of audio and video processing lies in understanding the basic process and optimization methods. 1. The basic process includes acquisition, encoding, transmission, decoding and playback, and each link has technical difficulties; 2. Common problems such as audio and video aberration, lag delay, sound noise, blurred picture, etc. can be solved through synchronous adjustment, coding optimization, noise reduction module, parameter adjustment, etc.; 3. It is recommended to use FFmpeg, OpenCV, WebRTC, GStreamer and other tools to achieve functions; 4. In terms of performance management, we should pay attention to hardware acceleration, reasonable setting of resolution frame rates, control concurrency and memory leakage problems. Mastering these key points will help improve development efficiency and user experience.
Using the Translator facade for Localization in Laravel.
Jul 21, 2025 am 01:06 AM
TheTranslatorfacadeinLaravelisusedforlocalizationbyfetchingtranslatedstringsandswitchinglanguagesatruntime.Touseit,storetranslationstringsinlanguagefilesunderthelangdirectory(e.g.,en,es,fr),thenretrievethemviaLang::get()orthe__()helperfunction,suchas
Documenting PHP with Block Comments
Jul 18, 2025 am 04:53 AM
Writing PHP block annotations can improve the readability and maintenance of the code. It should include information such as @param, @return, @throws, etc., and explain "why" and "how to use", avoid meaningless repetition, keep updating synchronously with the code, and the IDE can automatically recognize prompts.
Go for Image Manipulation Libraries
Jul 21, 2025 am 12:23 AM
Common Go image processing libraries include standard library image packages and third-party libraries, such as imaging, bimg, and imagick. 1. The image package is suitable for basic operations; 2. Imaging has a complete function and a simple API, which is suitable for most needs; 3. Bimg is based on libvips, has strong performance, which is suitable for large images or high concurrency; 4. Imagick binds ImageMagick, which is powerful but has heavy dependencies. Quickly implement image scaling and cropping. You can use the imaging library to complete it through a few lines of code in Resize and CropAnchor functions, and support multiple parameter configurations. Adding filters or adjusting tones can be achieved through the color transformation function provided by imagination, such as Graysc


