The difference between Java Nio:io and NIO

Source: Internet
Author: User

first, the concept

NiO is the new IO, which was introduced in JDK1.4. NiO and IO have the same function and purpose, but the way of implementation is different, NIO is mainly used in blocks, so the efficiency of NIO is much higher than IO.     Two sets of NiO are available in the Java API, one for the standard input and output NIO, and the other for network programming NiO.     New IO successfully solves the above problem, how is it solved? The minimum unit of IO Processing client requests is the thread and NIO uses a unit that is smaller than the thread: Channel can say that only one thread in NiO can do all the receiving, reading, and writing operations.

Ii. major differences between NIO and IO

The following table summarizes the main differences between Java IO and NiO:

1. Flow-oriented and buffer-oriented

The first major difference between Java io and NiO is that IO is stream-oriented and NIO is buffer-oriented. The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read, and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth. If you need to move the data read from the stream before and after it, you need to cache it to a buffer first. Java NiO has a slightly different buffer-oriented approach. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process. However, you also need to check if the buffer contains all the data that you need to process. Also, make sure that when more data is read into the buffer, do not overwrite the data that has not been processed in the buffer.

2. Blocking and non-blocking IO

The various streams of Java Io are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. The non-blocking mode of Java NIO enables a thread to send a request to read data from a channel, but it can only get the data currently available, and if there is no data available, nothing is fetched, rather than keeping the thread blocked, so that 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).

3. Selector (selectors)

The Java NiO selector allows a single thread to monitor multiple input channels, you can register multiple channels using a selector, and then use a separate thread to "select" the channel: these channels already have inputs that can be processed, or select the channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.

Iii. how NiO and Io affect the design of applications

Whether you choose IO or the NIO toolbox, you may have an impact on the following aspects of your application design:

1. API calls to NiO or IO classes.
2. Data processing.
3. The number of threads used to process the data.

The difference between Java Nio:io and 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.