The difference between NIO and traditional IO

Source: Internet
Author: User

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.

========================================

As far as speed is concernedCPU > 内存 > 硬盘

    • I-From hard disk to memory
    • O-From memory to hard drive

The first way: I read the data from the hard disk, then the program has been waiting, the data after reading, continue to operate. This approach is the simplest, called blocking IO.

The second way: I read the data from the hard disk, then the program continues to execute, and so on after the data read, notify the current program (for the hardware is called an interrupt, the program is called a callback), and then the program can immediately process the data, you can also perform the current operation in reading data.

In the previous Java io, the nonblocking Io,nio introduced non-blocking IO.

Another is synchronous IO and asynchronous IO. Often said a term is "asynchronous non-blocking", as if asynchronous and non-blocking is the same thing, this is probably a misunderstanding it.

As for Java NIO's Selector, in the old Java IO system, it is stream-based, or "stream", streaming IO.

When the program reads data from the hard disk into memory, the operating system uses 2 "tricks" to improve performance, that is, pre-reading, if I read the first sector of the third track content, then you will most likely also use the second track and the fourth track content, so the operating system will be the contents of the nearby tracks read ahead, Put in memory, that is, the cache.

As can be seen above, the operating system is block blocks from the hard drive to take data, like a large basin, suddenly put into a basin of water. However, when Java is used, the old IO is actually based on the stream stream, that is, although the operating system gave me a basin of water, but I have to drink slowly with a straw.

As a result, NIO was born.

Http://www.jb51.net/article/50621.htm

The difference between NIO and traditional IO

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.