1. What is IO?
From the perspective of computer architecture, I/O describes the process of communication between a computer system and external devices.
From an application perspective, to ensure the stability and security of the operating system, the address space of a process is divided into user space and kernel space. Applications like ours run in user space, and only kernel space can perform system-level resource-related operations such as file management, process communication, memory management, and so on. In other words, if we want to do IO operations, we must depend on the capacity of the kernel space.
Our application makes IO calls (system calls) to the operating system’s kernel, and the operating system’s responsible kernel performs specific IO operations.
Common IO models in Java include BIO (synchronous blocking IO model), NIO (synchronous non-blocking IO model), and AIO (asynchronous IO model).
2.BIO (Synchronous blocking IO)
In the synchronous blocking IO model, after an application makes a read call, it blocks until data is copied to user space in the kernel.
The BIO model can only handle scenarios with a small number of connections. When faced with hundreds of thousands or even millions of connections, the traditional BIO model is helpless.
3.NIO (Synchronous non-blocking IO)
In the synchronous non-blocking IO model, applications make read calls and wait for data to be copied from kernel space to user space, while threads remain blocked until data is copied from kernel space to user space.
Compared with synchronous blocking IO model, synchronous non-blocking IO model avoids continuous blocking through polling operation.
4.AIO (Asynchronous IO model)
Asynchronous IO is implemented based on events and callbacks, meaning that an action is applied and returned, not blocked, and when the background processing is complete, the operating system notifies the appropriate thread to proceed with the subsequent action.