Reasonable selection of thread pool size

Before looking at this question, you must understand that your thread (application) cannot control the CPU's computing resource allocation. You just submit the task to the operating system, and how the CPU resources are allocated is controlled by the operating system.


For the solution under multi-threading, it is not that the more threads the better, the setting of the number of thread pools needs to take into account many aspects. First of all, it is necessary to confirm whether the tasks handled by this thread pool are CPU-intensive or IO-intensive or belong to Hybrid. For CPU-intensive general thread pools, the number of CPU cores + 1, the + 1 is to make full use of the CPU (for example, when a thread is idle or when a thread finishes computing and switches new tasks, it can make full use of this part of resources), and For IO-intensive types, it is generally set to 2N+1. However, this is not absolute. Considering the response time of IO, if the response time is relatively long, the CPU can be idle and the number of threads can be appropriately increased. But even in this case, it is not possible to increase unlimitedly, because the pressure of IO is also considered. For example, for a database, if there are too many threads connecting to the database at the same time, the number of database connections will increase the pressure on the database. This should also be taken into account when setting the thread pool size.

Guess you like

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