I. What is Reactor Model
Reactor pattern is a design pattern for processing concurrent service requests and submitting them to one or more
Event design patterns for multiple service handlers. When client requests arrive, the service handler uses a multi-channel allocation strategy, receives all requests from a non-blocking thread, and then dispatches these requests to the relevant worker thread for processing.
Second, in what scenarios do you use Reactor mode?
For high concurrent systems, Reactor mode is often used instead of multithreading, which saves the system.
Resources to improve system throughput.
The use scenarios of this mode are described in a more intuitive form below.
In a restaurant, for example, everyone’s meal is an event. Customers will first look at the menu, then order, and deal with it.
Food events require attendants.
Just as a network service has many requests, the server receives each request and then assigns a worker thread to process it.
In multi-threaded processing mode:
A person comes to dinner, a waiter goes to serve, and then the guest looks at the menu and orders. The waiter gives the menu to the back cook.
Two people come to dinner, two waiters to serve…
Five people eat, five waiters serve…
This is similar to multithreading. When an event arrives, a thread will serve it.
Obviously, this approach has a good user experience in a small number of situations, and every guest feels that they enjoy the best.
If the restaurant keeps visiting up to five guests at the same time, the restaurant can serve well.
Because of the good service in this shop, there are more people eating.
Ten guests will come at the same time, and the boss is very happy, but there are only five waiters, so one-on-one service is not possible.
Some guests can’t immediately enjoy the service of a waiter.
The boss had to hire five more waiters in order to make money.
Now it’s all right. Every customer enjoys the best and fastest treatment.
More and more people are satisfied with the restaurant. There are more and more customers. At the same time, there are 20 people coming to the restaurant. The boss is happy but also happy.
I can’t be happy. Please invite the waiter again. If you don’t occupy the place, you have to start the work. If you invite someone else, you won’t make any money.
What shall I do?
The boss thought about it. Ten waiters can handle 20 guests. Just be diligent and wait on them.
It’s still too late for one guest to serve another immediately.
After a comprehensive consideration, the boss decided to use 10 server thread pools!
But there is another serious disadvantage: if the customer who is receiving the waiter’s service orders slowly, the others
Guests may have to wait a long time.
Some hot-tempered guests may not be able to wait to leave.
In this way, the Reactor pattern is introduced.
So how does the Reactor pattern deal with this problem?
The boss later found that the guests ordered slowly, and most of the waiters were waiting for the guests to order. In fact, the work was not too much.
The reason why the boss can be a boss is of course a little different, and finally found a new way, that is, to be a guest.
When people order, the waiter can go and greet other guests. When the guests have ordered, he will greet them directly with “Serve”.
Servant “, there will be a waiter to serve in the past.
After using this new method, the boss made a layoff, leaving only one waiter!
This is how to use a single thread to do multithreading.
Actual restaurants are served in Reactor mode.
Copyright Statement: This article is the original article of the blogger. Please attach a link to the blog article for reprinting.