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, generate audit pass voucher (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. As shown, 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, or the thread pool (which can typically be set to CPU cores). is responsible for the multi-channel separation of the connected sockets, read and write network data, here the read and write network data corresponding to the customer fill out the time-consuming action, the specific business processing function, which is thrown to the worker thread pool completed.
Here you can see three types of threads, namely the Mainreactor thread, the subreactor thread, and the work thread. Different threads Cheng Gan professional things, eventually each thread is not empty, and the throughput of the natural system goes 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 spent in idle waiting, how can the clerk keep full load?
Or divide-and-conquer mentality, specifically assigned 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 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, the asynchronous approach can greatly improve efficiency.
In fact, Jetty continuations to achieve the above working methods, interested students can go to try (http://wiki.eclipse.org/Jetty/Feature/Continuations).
nio+ asynchronous way to allow a small number of threads (resources) can do a lot of things, it is worth noting that it does not allow a request to decline the waiting time, on the contrary may increase the waiting time.
4 Summary
Summary of a sentence: "Divide and conquer, the task is split, by the special person responsible for the special task."
Http://www.cnblogs.com/LBSer/p/4622749.html
Java NIO (GO)