Reactor mode (reactor Mode)

Source: Internet
Author: User

There is no proper way to translate the word reactor into Chinese. Many places are called the reactor mode, but it is more like the reactor mode. In fact, I think it is better to understand the reactor mode. By understanding, this mode is more like a guard who has been waiting for your call, or called a summoned beast.

 

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

 

First, we will introduce the advantages of this method in a more intuitive way. It may be better understood by comparing it with the commonly used multi-threaded method.

Taking a meal as an example, it is an event for everyone to eat. He will first take a look at the menu and then order the meal. Just like a website has many requests and requires the server to do something. Our service personnel are needed to handle these dining events.

 

The multi-thread processing method is as follows:

One person comes to eat, one waiter goes to serve, and then the guests will see the menu and order. The waiter gives the menu to the kitchen.

Two people come to dinner, two waiters go to service ......

Five people come to dinner, five waiters go to service ......

 

This is a multi-threaded processing method. When an event arrives, there will be a thread service. Obviously, this method has a good user experience when there are few people. Every guest feels that they are VIP and dedicated personnel. If the restaurant keeps serving up to five guests at the same time, this restaurant can be well served.

 

The good news is that the store provides good services and more people are eating. There will be 10 guests at the same time, and the boss is very happy, but there are only five waiters, so we cannot provide one-on-one services. Some guests will be left empty. The boss invited five more waiters. Now, everyone is entitled to VIP treatment.

 

More and more people are satisfied with this restaurant, and there are more customers. At the same time, the number of people who come to dinner has reached 20, and the boss is not happy. Please ask the waiter again, leave the place blank, and start work for money, then ask someone to save the money. What should we do? The boss thought, 10 waiters can deal with 20 guests, and the waiter can work diligently. After waiting for one guest, the waiter can wait for another one immediately. After a comprehensive consideration, the boss decided to use the thread pool of 10 service personnel ~~~

 

However, this is a serious drawback: If the ordering of the guests who are receiving the service is slow, other guests may have to wait for a long time. Some hot-tempered guests may not be able to leave.

 

How does the reactor handle this problem:

The boss later found that the ordering of the guests was slow, and most of the waiters were waiting for the guests to order. In fact, they did not do much work. The boss can be a bit different from the boss, and finally found a new method, that is: when the guests order, the waiter can greet other guests, after the guests have ordered the food, they will directly say "waiter", and a waiter will serve immediately. Hey, after the boss had this new method, he made a layoff and left only one waiter! This is to use a single thread for multithreading.

 

Actually, all restaurants use the reactor mode for service. Some design models come from life.

 

The reactor mode mainly improves the system throughput and handles more things with limited resources.

 

On a single-core machine, multithreading does not improve the system performance unless there is some blocking. Otherwise, the overhead of thread switching slows down the processing speed. Just like you do two things by yourself. 1. Cut an apple. 2. Cut a watermelon. You can do it one by one, and I think you will do it one by one. If you use multiple threads at this time, you can cut the apple and watermelon in a short time, just like what the speed is. That is why multithreading on a single-core machine may be slower.

 

However, when a blocking operation occurs, the advantage of Multithreading is displayed. Now you have two other things to do: 1. Split an apple. 2. Boil a pot of boiling water. I think no one will finish one piece and then another. You will certainly cut the apple while boiling water.

 

I will not talk about it much in the east and west. Please refer to the appendix reactor-shanghens.pdf. There are many figures, which can be understood if the E text is not good.

 

From: http://www.jdon.com/concurrent/reactor.htm

PS. Reactor mode is an event-driven design mode, similar to event processing in AWT. Io operations are common.

Reactor mode (reactor Mode)

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.