In JAVA NIO

In fact, AFTER I looked at the various networking models of Linux, I began to wonder. We know that the network IO model is generally divided into two stages:

  1. Wait for the data to be transferred to the nic and copied to the kernel buffer
  2. Copy data from the kernel buffer to user space for execution

But when you look at NIO in Java, why is it non-blocking?

We all know that the underlying implementation of NIO like Java is IO multiplexing, which uses SELECT to actually listen, but as we analyzed earlier, in the first phase of IO multiplexing, which is calling select, if you don’t have ready data at this point, It blocks until the operating system replies with a message that the select can return, and then blocks to read the data into the user buffer.

Blocking is considered in the first phase, when no data reaches the kernel, and the first phase of IO multiplexing is also blocked.

So I was very confused and went to see the answer on the Internet:

Two kinds are summarized:

  1. In fact, when using SELECT, poll, and epoll for I/O multiplexing, you can set the return time to 0 to be non-blocking
  2. Non-blocking is also blocking when only the selector. Select () method is used. This non-blocking is after the select() method returns, after which we can handle our own business logic including reads. So in general it’s non-blocking. Because the BIO actually blocks all the way through until it’s done.

In conclusion, I think it would be confusing to call NIO in Java as new IO instead of nonblocking IO, just like in Java programming idea.