Advantages and benefits of multithreading

Due to the birth of multi-core and multi-threaded CPUs, high-concurrency programming has attracted more and more attention. The benefits that multi-threading can bring to programs are as follows:

Make full use of CPU resources

From the above CPU introduction, the multi-threaded concurrency mechanism is not suitable for cores without CPU on the market. In particular, the server has more than one CPU. If you still use single-threaded technology to do your thinking, it will obviously be OUT. Because the programmer's basic scheduling unit is a thread, and a thread can only run on a thread of a core of a CPU. If you are an i3 CPU, the worst is the dual-core 4-thread computing power. If it is a one-threaded program, it will waste 3/4 of the CPU performance: if a multi-threaded program is designed, it can run on multiple threads of multiple cores of multiple CPUs at the same time. It can make full use of the CPU, reduce the idle time of the CPU, give play to its computing power, and increase the amount of concurrency.

It's like an order processing system. If we use single thread to achieve. When the user initiates a request, accepts the request, sends a text message, informs the express delivery, generates an order, etc. modules. We can execute the module in a new thread. You can let idle CPUs execute tasks in parallel to achieve the purpose of making full use of the CPUs.

Speed ​​up the time to respond to users

For example, we often use Thunder download, we like to open multiple threads to download, no one wants to use one thread to download,

Because multithreading is faster.

This is especially true when we are doing program development, especially when we are doing Internet projects. If the response time of the web page is increased by 1 second, a lot of conversions can be increased if the traffic is large. Anyone who has done high-performance web front-end tuning knows that two or three subdomains should be used to load static resource addresses? Because there is no more subdomain, the browser will open a few more threads to load your page resources after loading yours and to improve the response speed of the page. Multithreading, high concurrency is really everywhere.

You can make your code modular, asynchronous and simplistic

For example, when we implement an e-commerce system, place orders and send SMS to users, the emails can be split, and the two steps of sending SMS and email to users can be separated into separate modules and handed over to other threads for execution. This not only increases the asynchronous operation, improves the system performance, but also makes the program modular, clear and simple.

There are many benefits of multi-threaded application development, and you can slowly realize its charm in future code writing.

Guess you like

Origin blog.csdn.net/weixin_47184173/article/details/115107516