The difference between Java IO and NiO

Source: Internet
Author: User

The main differences between Java NiO and IO

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

io NIO oriented stream oriented buffer blocking IO  non-blocking IO no                selector
stream-oriented and buffer-oriented

The first major difference between Java NiO and Io 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.

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 requests 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).

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.

Here is an example to illustrate:

In traditional socket IO, you need to create a thread for each connection, and when the number of concurrent connections is huge, the overhead of the stack memory and CPU thread switching that the thread consumes will be huge. With NIO, you no longer need to create a separate thread for each thread, you can use a thread pool with a limited number of threads, or even a thread for any number of connection services. Because the number of threads is less than the number of connections, each thread does not block the IO operation, and if blocked, some connections are not processed, and NIO provides this non-blocking capability.

The answer to a small number of threads at the same time for large numbers of connections is a ready choice. This is like to eat in the restaurant, every table guests, there is a waiter dedicated to serve you, from you to the restaurant to checkout, so the benefits of the way is good service quality, one-to-one service, VIP Ah, but the shortcomings are also very obvious, high cost, if the restaurant business, at the same time to 100 table guests, it will require 100 The boss had to die of heartache when he got paid, which is the traditional way of connecting a thread.

The boss is a person ah, fine. The boss will have to fathom how can use 10 waiter at the same time for 100 table guests service, the boss found that the waiter in the process of service for guests is not always busy, the guests ordered the dishes, finish the dishes, eating this period of time, the waiter idle down, but this waiter is still occupied by this table guests, Not for other guests, in the words of Huawei Leader, is not full of work. How to use this idle time. The restaurant owner thought of a way, let a waiter (front desk) dedicated to collect the needs of guests, registered, such as the guests come in, the guests order, the guests to check out, are first recorded in order to arrange. Each waiter came here to take a demand, such as a la carte, and took the menu to help guests order. After a good meal, the waiter will come back immediately, collect the next demand, continue to serve other guests. This way the quality of service is not as good as one-to-one service, and when guests have a lot of data may need to wait. But the benefits are also obvious, because the guests are eating at the time the waiter does not have to idle, waiter this time can serve other guests, the original 10 waiter for up to 10 table guests service, now may be 50 tables, 60 guests service.

This type of service differs from the traditional one in two ways:

1, added a role, to have a special responsibility to collect the guests needs of the people. What corresponds to NiO is selector.

2, from the blocking service to non-blocking service, the guests eat when the waiter does not have to be waiting for guests next. Traditional IO operations, such as read (), are blocked until the data arrives when there is no data to read. When no data is readable in NiO, read () returns 0 immediately and the thread does not block.

In NiO, after the client creates a connection, the connection is registered to selector, the equivalent of the guest entering the restaurant, telling the front desk that you want to dine, the front desk will tell you what the number is, and then you may be seated at that table, Selectionkey is the table number. When a table needs to serve, the front desk will record which table needs what service, such as table 1th key dishes, table 2nd to check out, the waiter from the front desk to take a record, according to the records to provide services, finished again to remove a. So the time of service is used most effectively.

The difference between Java IO and NiO

Related Article

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.