"Java" Java NIO

Source: Internet
Author: User

Tag: COM generates transmission class www. Why is the device operating system log

Why should NIO use NIO?

NIO was created to enable Java programmers to implement high-speed I/O without having to write custom native code. NIO transfers the most time-consuming I/O operations (that is, the fill and fetch buffers) back to the operating system, which can greatly improve speed.

Flow vs. block comparison

The most important difference between the original I/O library (in java.io.*) and NIO is the way data is packaged and transmitted. As mentioned earlier, the original I/O processes the data in a streaming way, and NIO processes the data in chunks.
The stream-oriented I/O system processes data one byte at a time. An input stream produces one byte of data, and one output stream consumes one byte of data. Creating filters for streaming data can be very easy. It is relatively straightforward to link several filters so that each filter is only responsible for a single part of the complex processing mechanism. On the downside, flow-oriented I/O is usually quite slow.
A block-oriented I/O system processes data as a block. Each operation generates or consumes a block of data in one step. Processing data by block is much faster than processing data by (streaming) bytes. But block-oriented I/O lacks the elegance and simplicity of some stream-oriented I/O.

The buffer mechanism of NIO

The advantage of NiO's performance comes from the buffering mechanism, which needs to be written to the buffer in chunks, either read or write. NiO actually allows us to operate the IO more closely to the actual operating system.
All system I/O is divided into two phases: waiting for readiness and operation. For example, read functions, which are divided into waiting systems to be readable and true to read;
Take the socket as an example:
The buffer of the kernel is fetched from the application layer and then copied from the kernel's buffer to the buffer of the process. So actually the underlying mechanism is constantly using buffers to read and write data. Even though the traditional IO abstraction is the direct reading of data from the stream, it is still essentially using buffers to read and write data.
So, to better understand nio, we need to know the underlying mechanism of IO, which lays the groundwork for us to understand channel and buffer in the future. Briefly here, we can understand the bufffer as the kernel buffer, so regardless of read and write, Nature will pass through this area, read, first from the device to read the data to the kernel, then read the process buffer, write, first from the process buffer to the kernel, then write back from the kernel device.

Non-blocking mechanism of NIO

NIO's nonblocking mode allows a thread to send a request to read data from a channel, but it can only get the data that is currently available and will not get anything if no data is available. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable. The same is true for non-blocking writes. A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).

is a comparison of several common I/O models:

Take Socket.read () as an example:

The traditional bio inside Socket.read (), if there is no data in the TCP Recvbuffer, the function will block until the data is received and the data is read back.

For NIO, if the TCP Recvbuffer has data, it reads the data from the network card into memory and returns it to the user, whereas the reverse returns 0 and never blocks. So we can implement the NIO to listen to multiple IO channels at the same time, and then constantly poll for devices that can read and write.

The IO model of NIO can be understood as an IO multiplexing model and a non-blocking model, as well as an event-driven model.
One thing to know here is that IO multiplexing is a must to implement non-blocking.

Summary

Advantages of NiO relative to Io flow:

    • Non-blocking
    • Buffer mechanism
    • Stream substitution block

Reference:

      • Https://tech.meituan.com/nio.html
      • Http://www.importnew.com/19816.html
      • Https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html

"Java" Java NIO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.