Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Java NIO and traditional IO

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the difference between Java NIO and traditional IO". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In traditional socket IO, one thread needs to be created for each connection. When the number of concurrent connections is very large, the stack memory occupied by threads and the overhead of CPU thread switching will be very huge. With NIO, instead of creating separate threads for each thread, you can use a thread pool with a limited number of threads, or even one thread, to serve any number of connections. Because the number of threads is less than the number of connections, each thread cannot block when performing IO operations, and if blocked, some connections will not be processed, and NIO provides this non-blocking capability.

How can a small number of threads serve a large number of connections at the same time? the answer is ready choice. This is like going to a restaurant, every table of guests has a waiter to serve you, from you to the restaurant to check out, the advantages of this way are good service quality, one-on-one service, VIP ah, but the disadvantages are also obvious, the cost is high. If the restaurant business is good and 100 tables of guests come at the same time, you need 100 waiters, and the boss will have to die of heartache when he pays the salary. This is the traditional way to connect one thread at a time.

Who is the boss? he is exquisite. The boss had to figure out how to use 10 waiters to serve 100 guests at the same time. The boss found that the waiters were not always busy in the process of serving the guests. During this period of time when the guests ordered, served and ate, the waiter was idle, but the waiter was still occupied by the guests at this table and could not serve other guests. In the words of the leader of Huawei, the work was not full. Then how to make use of this idle time? The owner of the restaurant thought of a way to have a waiter (front desk) specially responsible for collecting the needs of the guests and registering them. For example, when a guest comes in, the guest orders, and the guest wants to check out, record it first and arrange it in order. Each waiter came here to pick up a demand, such as ordering, and then took the menu to order for the guests. After ordering, the waiter will come back immediately to pick up the next demand and continue to serve other guests. The quality of service in this way is not as good as one-on-one service, and you may have to wait when there is a lot of guest data. But the benefits are also obvious. since the waiter does not have to be idle when the guests are eating, the waiter can serve other guests at this time. Originally, 10 waiters served at most 10 tables at the same time, but now they may serve 50 tables and 60 guests.

This kind of service is different from the traditional one in two ways:

1. A role has been added to have a person responsible for collecting guest needs. The counterpart in NIO is Selector.

2. it has changed from blocking service to non-blocking service, so that the waiter does not have to wait beside the guest all the time. Traditional IO operations, such as read (), block threads until the data arrives when there is no data to read. When there is no data to read in the NIO, read () immediately returns 0 and the thread does not block.

In NIO, after the client creates a connection, it first registers the connection to Selector, which is equivalent to telling the front desk that you want to eat after the guest enters the restaurant. The front desk will tell you what your table number is, and then you may sit down at that table. SelectionKey is the table number. When a table needs service, the front desk records which table needs what service, for example, table 1 needs to order, table 2 needs to check out, the waiter takes a record from the front desk, provides service according to the record, and then comes back to pick up the next one. In this way, the time of service is used most effectively.

This is the end of the content of "what is the difference between Java NIO and traditional IO". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report