Java NiO (ii) a story that speaks clearly about NIO

Source: Internet
Author: User

Suppose a bank has only 10 employees. The bank's business process is divided into the following 4 steps:

1) Customer filling application form (5 minutes);

2) Staff audit (1 minutes);

3) The clerk called Security to go to the vault to withdraw money (3 minutes);

4) The clerk prints the ticket and returns the money and bills to the customer (1 minutes).

Let's look at how the different ways of working in a bank affect their productivity.

1 Bio Way

Each one of the customers, immediately by a staff to receive treatment, and this staff will be responsible for the above 4 complete process. When there are more than 10 customers, the remaining customers need to wait in line.

We figure out how many customers the bank can handle in one hours? An employee takes 10 minutes (5+1+3+1) time to process a customer, one hours (60 minutes) to handle 6 customers, and 10 employees, that is, only 60 customers can be processed.

You can see that the working status of the bank staff is not saturated, such as in the 1th step, in fact, is in the waiting.

This kind of work is actually bio, each time a request (customer), allocated to the thread pool by a thread (staff) processing, if the maximum limit of the thread pool (10), throw to the queue waiting.

2 NiO Way

How to improve the bank's throughput?

Idea: Divide and conquer, separate the task, by the special person responsible for the special task.

Specifically, the job of the bank to appoint an employee a,a is to send a form to the customer whenever a customer comes to the bank, and when a customer fills out the form, a randomly assigns it to the remaining 9 employees to complete the next steps.

We calculate how many customers can the bank handle in one hours under this working mode?

Assuming a lot of customers, staff A's work is saturated, he constantly fill the table of customers to the counter processing, the counter staff 5 minutes to finish a customer, one hours 9 staff can handle: 9* (60/5) = 108.

The change of working mode can bring about great improvement of efficiency.

This way of working is actually the idea of NIO. is a very classic NiO illustration, the mainreactor thread is responsible for listening to the server socket,accept new connection and assigning the established socket to Subreactor;subreactor can be a thread, can also be a thread pool (generally can be set to the number of CPU cores), is responsible for the separation of the connected socket, read and write network data, here the read and write network data can be analogous to customer fill out this time-consuming action, the specific business processing function, which is thrown to the worker thread pool completed.

You can see that typical NIO has three types of threads, namely Mainreactor thread, Subreactor thread, and work thread. Different line Cheng Gan professional things, eventually each thread is not empty, the system's throughput naturally went up.

3 Async Mode

Is there anything that can be improved in the second mode of work?

Take a closer look at the 3rd step. This 3-minute counter staff is waiting to spend, so how can you keep the clerk at full load?

Or divide and conquer the idea of assigning 1 staff B to be responsible for the 3rd step. Whenever the counter staff completes the 2nd step, they inform staff B to be responsible for the communication with the security to withdraw money. At this time the counter staff can continue to process the next customer. When clerk B gets the money, what will he do? He will inform the customer that the money has arrived at the counter, let the customer queue up, when the counter staff to serve the customer again, found that the customer first 3 steps have been completed, direct implementation of the 4th step.

We can calculate how much the bank's throughput can be improved by this method.

Assuming staff B's work is very saturated, a clerk at the counter can now handle a customer in 2 minutes, and one hours and 8 staff can handle it: 8* (60/2) = 240.

In today's Web services, it is often necessary to call third-party services via RPC or HTTP, which is the 3rd step, and if this takes a long time, it can greatly reduce resource usage by asynchronous means.

Jetty Continuations realized the above asynchronous way, interested students can go to try (http://wiki.eclipse.org/Jetty/Feature/Continuations).

The nio+ asynchronous approach allows a small number of threads (resources) to do a lot of things, which can be used in many scenarios, such as proxy services, API services, long-connected services, and so on, which, if used synchronously, will consume a lot of machine resources. Although nio+ can increase the throughput of the system asynchronously, it does not allow the latency of a request to drop and may increase the wait time instead.

4 Summary

A summary of the sentence: "Divide and conquer, split the task, by the special person responsible for the special task", which not only in the computer field, in the entire social field is effective.

Java NiO (ii) a story that speaks clearly about 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.