The difference between NIO and traditional IO < turn >

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.

Transferred from: http://blog.csdn.net/zhouhl_cn/article/details/6568119

The difference between NIO and traditional IO < turn >

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.