Design Patterns (11) - Reactor Patterns (Reactor)

Concurrent systems often use the reactor mode instead of the common multi-threaded processing method, saving system resources and improving system throughput.

 

Let's first introduce the advantages of this method in a more intuitive way. It may be better understood by comparing it with the commonly used multi-threading method.

Take a restaurant as an example, each person who comes to eat is an event, he will look at the menu first, and then order. Just like a website will have a lot of requests, asking the server to do something. We need our service staff to handle these dining events.

 

In a multithreaded way it would be like this:

A person comes to eat, a waiter serves, and then the customer will look at the menu and order. The waiter handed the menu to the cook.

Two people come to eat, two waiters serve...

Five people come to eat, five waiters to serve...

 

This is the multi-threaded processing method. When an event arrives, there will be a thread service. Obviously, this method will have a good user experience when there are few people. Every guest feels like a VIP and is served by a dedicated person. If the restaurant has a maximum of 5 guests at the same time, this restaurant can serve well.

 

There is good news, because the service of this restaurant is good, and more people eat. There will be 10 guests at the same time, the boss is very happy, but there are only 5 waiters, so one-to-one service is not possible, and some guests will be left unattended. The boss invited 5 more waiters, and now everyone is treated as VIP.

 

More and more people are satisfied with this restaurant, and there are more customers. The number of people who come to eat at the same time has reached 20. The boss is not happy. Please ask the waiter again. People can't save money. How to do it? The boss thought for a while. 10 waiters can handle 20 guests. The waiter should be more diligent. After serving one customer, it is still too late to serve another. After comprehensive consideration, the boss decided to use the thread pool of 10 service personnel~~~

 

However, this has a serious disadvantage: if the customer who is being served by the waiter is slow to order, other customers may have to wait for a long time. Some hot-tempered guests may not be able to wait to leave.

 

How does Reactor handle this:

The boss later found out that customers were slow to order food, and most of the waiters were waiting for customers to order food. In fact, they did not do much work. Of course, being a boss is a bit different. Finally, I found a new method, that is: when a customer orders, the waiter can greet other guests. ”, and a waiter came to serve immediately. Hehe, then after the boss had this new method, there was a layoff and only one waiter remained! This is what multithreading does with a single thread.

 

Actual restaurants are served using the Reactor model. Some design models are actually from life.

 

Reactor mode is mainly to improve the throughput of the system and handle more things with limited resources.

 

On a single-core machine, multithreading does not improve system performance unless there is some blocking. Otherwise the overhead of thread switching will slow down processing. Just like you do two things by yourself, 1. Peel an apple. 2. Cut a watermelon. Then you can do it piece by piece, and I think you will do it piece by piece too. If you use multi-threading at this time, peeling apples and watermelons for a while, you can compare which is the fastest. That's why multithreading may be slower on a single-core machine.

 

But the advantage of multi-threading will be revealed when there are blocking operations. Now you have two other things to do. 1. Peel an apple. 2. Boil a pot of water. I don't think anyone is going to finish one piece before making another, and you'll definitely be peeling apples while boiling water.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243669&siteId=291194637