[Java Application] Set the optimal number of threads in the thread pool

[Java Application] Set the optimal number of threads in the thread pool

I. Introduction

When using the thread pool, it often involves how many threads should be set in the thread pool. It is related to the performance of our program execution. In most cases, it is set based on experience, but there is a lack of basis. In general, the program can be
divided CPU密集型into I/O密集型, and the methods for calculating the optimal number of threads are different for these two intensive programs.

2. CPU-intensive

For CPU密集型programs, multi-threading focuses on utilizing as many CPU resources as possible to process tasks, so in theory, 线程数=CPU核数it is the most appropriate. However, in actual work, the number of threads is generally set to: CPU核数+1, in order to avoid thread blocking due to unexpected situations. If a thread is blocked due to unexpected circumstances, the emerging thread will continue to perform tasks, thereby ensuring CPU utilization;

Therefore, in CPU-intensive programs, the number of threads can generally be set to: CPU核数+1.

3. I/O intensive

For I/O-intensive programs, if when a thread is performing an I/O operation, another thread happens to complete the CPU computing task, then the CPU utilization efficiency is optimal at this time. Therefore, in I/O-intensive programs, the theoretically optimal number of programs is related to the ratio of the time-consuming I/O operations in the program to the time-consuming CPU calculations.
Under a single-core CPU, the theoretical optimal number of threads = 1 + (time-consuming I/O operations / time-consuming CPU calculations).
Under a multi-core CPU, the theoretical optimal number of threads = number of CPU cores * (1 + I/O操作的耗时/ CPU计算的耗时)

4. Summary

The number of threads calculated through the above method is only the theoretical optimal number of threads. In actual work, the system still needs to be continuously tested 压测and determined based on the results of the stress test 最佳的线程数;
in the actual environment, there may be different processes or In the case of multiple threads, the number of threads that need to be set should be combined with the resource utilization of the actual operating environment;

Note: Get the number of CPU cores Runtime.getRuntime().availableProcessors();

Guess you like

Origin blog.csdn.net/weixin_37598243/article/details/128279415