Make writing a habit together! This is the 11th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Hanshan Temple outside Gusu, midnight bell to the passenger ship.

1 introduction

The importance of IO is self-evident in development. From traditional industries, games, the Internet, to the future era of everything connected, the cognition and understanding of IO can solve many practical complex problems, from BIO to NIO/AIO, all knowledge involves network communication and basic knowledge. Netty’s encapsulation of NIO is currently the most useful tool, especially for high-concurrency scenarios.

2 NIO

NIO refers to non-blocking at the operating system level and new IO at the Java level, both of which are core non-blocking. In an operating system, or in a traditional BIO, the operation is blocked. After the operating system kernel makes a change, the socket function supports non-blocking. Previously, it was blocked before fetching data and waiting for results.

In Java code, the operations on BIO are as follows:

Serversocket server = new ServerSocket(8986); While true # block the Socket client = server.accept(); InputStream input = client.getinputStream ();Copy the code

The NIO operation is a bit more complicated than the BIO code:

ServerSocketChannel Server = ServerSocketChannel.open(); Server.bind () # select Selector = Selector. Open (); Accept event server.register(selector, 'accept'); Int wt = select.select (); if wt == 0 continue; Set<SelectionKey> keys = selectedKeys();Copy the code

In terms of core functionality, Accept is a blocking method, select is non-blocking, and there are some differences in the underlying socket methods.

Selecter.accept (), a channel selector buffer, uses event registration states to process request information.Copy the code

In Linux, use the man socket to view the parameters passed by the socket in the operating system, as shown in the following figure:

The functions of the operating system are written in C language. Java is also a C-like language socket() # creates a file descriptor for communication creates an endpoint for communication and returns a descriptor.... SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open file description extra calls to fcntl(2) to achieve the same result.Copy the code

3 The difference between blocking and non-blocking

NIO can handle more connections per thread than BIO, so why can a single thread handle more connections? Because there’s a Selector. When the connection is connected there are two operations to do:

  • 1 Wait for the kernel to complete data processing.
  • 2 Data is copied from kernel space to user space.

The BIO is blocked while waiting for data from the client, so that only one request can be processed per thread, and the maximum number of threads the machine can support is limited, which is why the BIO does not support high concurrency. In NIO, when a socket is set up, instead of blocking to receive the socket, the thread registers the request with the selector, and the selector keeps iterating over the request, Once the data is ready, threads are notified to process it. This process is non-blocking, so NIO is more powerful than BIO single-thread processing. Blocking IO, non-blocking IO, multiplexing, signal-driven and asynchronous IO, comparison of 5 I/O models:

Here to be sure, blocking and non-blocking said the data preparation phase, and synchronous and asynchronous process is to copy the data from the kernel space to user space, the process of synchronization if user threads need to oneself active trigger the data retrieval, the kernel is in preparation for the data processing after the completion of the notice, and the kernel will complete is the concept of asynchronous data replication process, The user thread is then notified that the data replication process is complete.

4 summarizes

NIO and blocking concepts are covered in this article, and multiplexing and Reactor concepts will be covered in subsequent articles.