[java] This article lets you understand the IO model in Java thoroughly

foreword

This article only explains the IO model, let you know what the IO model is, how to distinguish the IO model, and analyzes the three IO models in Java. It does not describe the IO operation of Java. This article is purely theoretical knowledge. After reading it, it will let you With a deeper understanding of IO, you won't be afraid during the interview.

1. What is IO

IO (Input/Output), which is the abbreviation of input and output. From the perspective of computer structure, IO is to input data into the computer, and the computer outputs data to the outside of the computer. Below is a very classic von Neumann The structure diagram divides the computer into five parts: arithmetic unit, controller, memory, input setting, and output device.
insert image description here

Input devices input data to the computer, and output devices receive data output from the computer.

So, 从计算机结构的角度来看IO,IO就是描述了计算机系统和外部设备之间通信的过程.

Next, let 从应用程序的角度's take a look at IO: In the operating system, in order to ensure the stability and security of the operating system, the address space of a process is divided into 用户空间(User space)和内核空间(Kernel space).

So what is user space and what is kernel space?

  • User space is an area of ​​memory accessible to normal applications.
  • Kernel space is an area accessed by the operating system kernel, which is independent of ordinary applications and is a protected memory space.

It can be simply understood that the applications we usually run are all running in user space, and it does not have permission to perform some resource-related operations at the system level, such as file management, memory management, etc. These operations must It can only be done by relying on the kernel space, that is to say, if we want to perform IO operations, we must rely on the capabilities of the kernel space, and here we need to pay attention to one point:用户空间的程序不能直接访问内核空间。

So if you want to initiate an IO operation, what should you do?

When we need to perform an IO operation, since we do not have the permission to perform the IO operation, we can only initiate a system call to request the operating system to help complete it. Therefore, for example, 用户进程想要执行IO操作的话,必须通过系统调用来间接的访问内核空间our most common IO operations are disk IO (reading and writing files) and network IO (Network Requests and Responses).

那么从应用程序的视角去看IO的话,我们的应用程序发起对操作系统的内核发起IO调用(系统调用,注意只是调用),操作系统去负责内核执行具体的IO操作。(If this paragraph is not easy to understand, it can be understood as a switch package, such as a tool class written by someone else, we don’t need to pay attention to the implementation details inside, just call its method, here our application is to initiate A call is made, but what actually executes is our operating system)

当应用程序发起IO调用之后,会经历下面两个步骤:(这里一定要记住这两点,它和后面息息相关!)

  • The kernel waits for the IO device to be ready for data
  • The kernel copies data from kernel space to user space

2. Common IO models

There are many IO models (for example, under the UNIX system, the IO model is divided into 5 types: synchronous blocking IO, synchronous non-blocking IO, IO multiplexing, signal-driven IO, asynchronous IO), this article only describes the The IO model in Java is divided into three categories: BIO, NIO, and AIO.

BIO(Blocking IO)

BIO belongs to the synchronous blocking IO model. After the application initiates the read call, it will be blocked until the kernel copies the data from the kernel space to the user space.
insert image description here

BIO is the most traditional IO model in Java. The related classes and interfaces are under the java.io package, because few people use it now (you will know why by looking at its characteristics later), so we will briefly introduce it here. Some features would be nice.

In high-concurrency scenarios, the consumption of thread resources is high, and each connection needs to be processed separately by a thread.
There are performance issues such as frequent thread context switching when transferring small objects.

how to optimize

So how to optimize this BIO?

First of all, one of the shortcomings of BIO mentioned above is that it is a model that requires as many threads as there are connections, but for users, it is very common and frequent to open a connection and then close a connection, and the corresponding is Creating threads and destroying threads, but creating threads and destroying threads is very resource-intensive for the operating system, so the optimization that comes to mind is to use the thread pool to optimize BIO.

The launch of NIO

However, the BIO model determines its upper limit. It is always a synchronous blocking IO model. Blocking will result in the inability to use a single thread to process multiple requests. Therefore, it is necessary to modify its model at this time, and will call read(), write( ) method is no longer blocked, so that multiple requests can be processed using a single thread, and this is no longer a synchronous blocking IO model, which is the NIO we will talk about below.

三、NIO(Non-blocking/New IO)

Java's NIO was introduced by Java1.4, corresponding to the java.nio package, which provides abstractions such as channel, selector, and buffer.
NIO in Java can be regarded as an IO multiplexing model, but some people think it is a synchronous non-blocking IO model. Here we briefly introduce these two models:

Synchronous non-blocking IO model

insert image description here

Compared with the synchronous blocking IO model, the synchronous non-blocking IO model avoids constant blocking by polling.

But it can also be seen from the figure that the problem is that the application program continuously performs IO system calls to poll whether the data is ready, which consumes a lot of CPU resources (in vernacular, it means repeatedly calling the read operation).
So at this time, the IO multiplexing model was introduced.

IO multiplexing model

insert image description here

In the IO multiplexing model, the thread first initiates a select call to ask whether the kernel data is ready, waits for the kernel to prepare the data, and returns a ready call to tell you that I am ready. At this time, the user thread initiates read call. Note: The process of calling read is still blocked (data is copied from kernel space to user space during this time).

The IO multiplexing model reduces the consumption of CPU resources through invalid system calls.

NIO in Java

NIO in Java has a very important concept, which is the selector (selector), also known as a multiplexer, through which one thread can be used to manage multiple client connections (is it the same as BIO here? It's different, the biggest optimization point is here). When the client data arrives, the thread serves the client again.
A picture of JavaNIO is given below:
insert image description here

This picture can well illustrate the characteristics of NIO.

四、AIO(Asynchronous IO)

AIO, also known as NIO 2.0, is the asynchronous IO model introduced in Java7. Many people do not understand the difference between non-blocking IO and asynchronous IO. In fact, asynchronous IO is implemented based on the event and callback mechanism. That is to say, the user After calling the operation, it will return immediately without blocking. When the background processing is completed, the operating system will notify the corresponding thread to perform subsequent operations.
insert image description here

At present, the application of AIO is not very extensive, and the most widely used is NIO.

Summarize

Finally, put a picture to summarize the IO model in Java:
insert image description here

Guess you like

Origin blog.csdn.net/u011397981/article/details/130480068