[JUC Advanced] How to properly set the number of core threads in the thread pool?

1 Introduction

Thread Pool (Thread Pool) is a tool for managing threads based on the idea of ​​pooling . Too many threads will bring additional overhead, including the overhead of creating and destroying threads, the overhead of scheduling threads, etc., and it also reduces the overall cost of the computer. performance. The thread pool maintains multiple threads, waiting for the supervisor to assign tasks that can be executed concurrently. This approach, on the one hand, avoids the cost of creating and destroying threads when processing tasks, and on the other hand, avoids the excessive scheduling problem caused by the expansion of the number of threads, ensuring full utilization of the core.

Therefore, the rationalization of the parameters of the thread pool will become very important.

If the number of core threads is set too small, a large number of throws will be thrown RejectedExecutionException, triggering the interface downgrade condition.

img

If the core thread setting is too large, the core threads may not be reused, but the program still needs to maintain these threads, which leads to a waste of resources.

If the thread pool queue length is set too long or corePoolSizetoo small, the task execution speed will be low

img

At this time, it is necessary to determine the number of core threads according to whether the program is IO-intensive or CPU-intensive.


2. CPU intensive

CPU-intensive means that the CPU usage frequency is relatively high, that is, the CPU often calculates some complex operations and logic processing.

In the case of CPU-intensive, the CPU load is relatively high, and the IO is finished in a short time.

For example, a program that calculates pi to less than 1,000 decimal places spends most of its time on trigonometric functions and square root calculations during execution, which is typical CPU-intensive.

At this time, the number of threads generally only needs to be set to the number of threads of the number of CPU cores.


3. I/O intensive

IO-intensive means that the CPU usage is relatively low, and the program spends most of its time waiting for IO reads or writes, resulting in a lot of idle time for threads.

In this case, the number of core threads is generally set to twice the number of CPU cores.

When a thread performs I/O operations and the CPU is idle, enable other threads to continue using the CPU to increase CPU utilization.

  1. The higher the proportion of thread waiting time, the more threads are needed, and other threads are enabled to continue using the CPU to improve CPU utilization;
  2. The higher the proportion of thread CPU time, the fewer threads are needed. This type mainly appears in some logics with frequent computing operations during development.

4. Actual situation

However, the actual situation is based on the execution status of concurrent tasks and the type of tasks. IO-intensive and CPU-intensive tasks run very differently , but this proportion is difficult to estimate reasonably, which makes it difficult to have A simple and effective general formula helps us to calculate the result directly.

For this situation, we can refer to the dynamics of the thread pool parameters of the Meituan technical team.

Interested students can read this article - Java thread pool implementation principle and its practice in Meituan business - Meituan Technical Team (meituan.com)


Guess you like

Origin blog.csdn.net/weixin_51146329/article/details/129642700