Knowledge knowledge reserve

What IO methods does Java provide?

Synchronous blocking I/O: Traditional Java I/O methods, such as InputStream and OutputStrea.

2. Multiplexed, synchronous non-blocking IO (NIO) : The NIO framework (java.niO package) was introduced in Java 1.4, providing new abstractions such as channels, Selector, Buffer, and so on to build multiplexed, synchronous non-blocking IO programs.

3. Asynchronous non-blocking IO(AIO) : In Java 7, NIO took a further step forward, also known as NIO 2, introducing Asynchronous non-blocking IO, also known as AIO (Asynchronous IO).

Four main IO models

Blocking IO

In the blocking I/O model, the application blocks from the IO system call until the system call returns. On success, the application process starts processing user-space cached data and makes a blocking socket read to the operating system.

Step 1: When the user thread invokes the READ system call, the kernel begins the first stage of IO: preparing data. In many cases, data has not arrived in the first place (i.e., a complete Socket packet has not been received), and the kernel waits for enough data to arrive.

Step 2: When the kernel waits until the data is ready, it copies the data from the kernel buffer to the user buffer (user memory), and the kernel returns the result.

Step 3: The user thread blocks from the start of the READ system call to the IO read. The user thread is not released from the block state until the kernel returns the result.

So, the characteristic of blocking IO is that during both phases of IO execution in the kernel, the user thread is blocked

Advantages: The BIO program is simple, while blocking waiting for data, the user thread hangs. User threads consume very little CPU resources.

Disadvantages: In general, there will be a separate thread for each connection, or a thread to maintain the read and write of a connected IO stream. In high concurrency scenarios, when a large number of threads are required to maintain a large number of network connections, the overhead of memory and thread switching can be huge. Therefore, basically, the BIO model is not available in high concurrency scenarios.

Synchronizing non-blocking NIO (None Blocking IO)

Distinguish between blocking and non-blocking:

When I/O is blocked, the current thread is blocked and cannot perform other tasks. Non-blocking, on the other hand, returns directly regardless of whether the IO operation is finished, and the corresponding operation continues in the background.

The process is as follows:

** Step 1: ** Immediately returns when the user thread makes an IO request in the stage where the kernel data is not ready. The user thread needs to make IO system calls constantly (possibly countless times).

Step 2: When kernel data arrives, the user thread makes a system call, and the user thread blocks. The kernel starts copying data. It copies the data from the kernel buffer to the user buffer (user memory), and the kernel returns the result.

Step 3: The user thread removes the block state and runs again. The user thread finally actually reads the data and continues to execute.

NIO advantage: Each IO system call is returned immediately while the kernel is waiting for data. User thread will not block, real-time performance is good.

NIO disadvantages: You need to repeatedly initiate IO system calls. This kind of continuous polling will constantly ask the kernel, which will occupy a lot of CPU time, and the system resource utilization is low.

Java NIO (New IO) is not the NIO model of the IO model, but another model called IO multiplexing.

IO multiplexing (I/O multiplexing)

The IO multiplexing model avoids the problem of polling wait in the synchronous non-blocking NIO model above.

The IO multiplexing model is based on the select/epoll system call which can be provided by the kernel. Multiplexing IO requires two system calls, a SELECT /epoll query call and a read call to the IO.

The basic principle of the IO multiplexing model is the select/epoll system call, a single thread polling the select/epoll system call is responsible for hundreds of socket connections. Pre-registered to the select/epoll queryable socket list), returns read-write connections when data arrives for one or more socket network connections. Therefore, with a single SELECT /epoll system call, one or even hundreds of network connections can be queried to read or write, greatly improving performance.

IO multiplexing starts with a select/epoll call. After a select/epoll call, we know which sockets have received data, and then we can use the READ system to read IO data.

Call flow:

Step 1: Make a SELECT /epoll system call to query for readable connections. The kernel queries the list of all selectable sockets, and select returns when the data in any socket is ready.

When the user process calls select, the entire thread is blocked.

Step 2: After the user thread acquires the target connection, it issues a read system call and the user thread blocks. The kernel starts copying data. It copies the data from the kernel buffer to the user buffer (user memory), and the kernel returns the result.

Step 3: The user thread releases the block state, and the user thread finally reads the data and continues executing.

Advantages of multiplexing IO: The advantage of using SELECT /epoll is that it can handle thousands of connections at the same time. The biggest advantage of I/O multiplexing over maintaining a connection with one thread is that the system does not have to create or maintain threads, which greatly reduces the overhead of the system.

Java NIO (New IO) technology, the use of IO multiplexing model. On Linux systems, the epoll system call is used.

Disadvantages of multiplexing IO: In essence, select/epoll system calls, which are synchronous IO, are also blocking IO. Both need to be responsible for reading and writing after the read and write event is ready, that is, the read and write process is blocked.

Asynchronous IO model

Synchronous or asynchronous:

Synchronous call: the user thread keeps asking the kernel whether the IO operation is ready. (including data preparation and replication). The kernel does not actively tell users whether threads are ready for I/O operations.

Asynchronous invocation: In asynchronous invocation, the kernel notifies the user thread of I/O operations (including data preparation and data replication) that the user can perform subsequent service operations.

The basic flow of AIO is as follows: The user thread informs the kernel to start an I/O operation through a system call, and the user thread returns. Kernel After I/O operations (including data preparation and replication) are complete, the kernel notifies the user of the program and enables the user to perform subsequent operations

Kernel data preparation: Read data from network physical devices (nics) to the kernel buffer.

Kernel data replication: Copying data from the kernel buffer to the buffer of user program space.

Call flow:

Step 1: When the user thread invokes the READ system call, it can immediately start doing something else. The user thread does not block.

Step 2: The kernel begins the first stage of IO: preparing data. When the kernel waits until the data is ready, it copies the data from the kernel buffer to the user buffer (user memory).

Step 3: The kernel sends a signal to the user thread or calls back to the callback interface registered by the user thread to tell the user thread that the read operation is complete.

Step 4: The user thread reads data from the user buffer to complete subsequent business operations.

Advantages of asynchronous IO model:

User threads are not blocked in either the waiting or copying phases of the kernel. The user thread needs to receive the kernel’s IO completion event, or register the IO completion callback function, into the operating system kernel. So asynchronous IO is sometimes called signal-driven IO.

Disadvantages of the asynchronous IO model:

Need to complete the event registration and transfer, here side needs the underlying operating system to provide a lot of support, to do a lot of work.

Currently, true asynchronous I/O is implemented on Windows systems through IOCP. However, in its current form, Windows is rarely used as a server operating system for millions or more concurrent applications.

In Linux, the asynchronous IO model was introduced in version 2.6 and is not perfect at present. Therefore, this is also under Linux, the implementation of high concurrency network programming is based on IO reuse model mode.

The test analysis

This question mainly investigates

(1) Java several IO models,

(2) Understanding of NIO, what is multiplexing

(3) if asked in-depth general will also ask Netty principle and source code (Netty knowledge is more, not introduced in this chapter). Netty is a Nonblocking I/O (NIO) network communication framework that is widely used in projects. NIO is a non-blocking I/O network communication framework that is widely used in projects.

`

The article continues to update every week, wechat search “Java interview open class”, the first line of large factory interview information free to share! Information is not much, is fine!