ThreadPoolExecutor parameter settings

1- First estimate the system load, the following values ​​are needed:
tasks: the number of tasks per second, assuming 50~100
taskTime: the time spent by each task, assuming 0.1s
responseTime: the maximum response time allowed by the system, assuming For 30s
2-calculation
1)
CorePoolSize requires taskTime seconds to process each task, so each thread can process 1/taskTime tasks per second. The system has tasks to process per second. The number of threads required is:

threadcount = tasks/(1/taskTime) = tasks*taskTime = (10~20)*0.05 = 5~10

According to calculations, the corePoolSize setting should be greater than 5 and less than 10.

The specific number is best based on the 80/20 principle, that is, the number of tasks per second of the system in 80% of the cases. If the number of tasks per second in the system of 80% is less than 80, and the maximum is 100, then corePoolSize can be set to 8.

2)
The length of the queueSize task queue depends on the number of core threads and the system's requirements for task response time. The queue length can be set to

queueSize = (corePoolSize/taskTime)*responseTime = (8/0.1) * 30 = 2400

According to the calculation, the queue length can be set to 2400

In addition, it should be noted that setting the queue length too large will cause the task response time to be too long. Do not set the queue length to Integer.MAX_VALUE, which will cause the number of threads to always be corePoolSize and never increase. When the number of tasks increases sharply At the same time, the task response time will also increase sharply.

3) maxPoolSize
When the system load reaches its maximum value, the number of core threads can no longer process all tasks on time, and then it is necessary to increase threads. It is no problem to submit 80 tasks per second, but the maximum number of tasks is reached during a certain peak period, 20 more tasks are submitted every second, reaching 100 tasks, and the extra 20 tasks per second need to be processed in responseTime complete

maxPoolSize = ( max(tasks) - 80%(tasks) ) / responseTime / (1/taskTime) + corePoolSize = (100-80)/ 30 / (1/0.1) + 8 = 15

From the above calculation, the maximum number of threads can be set to 15

4) KeepAliveTime:
When the load decreases, the number of threads can be reduced. If a thread's idle time reaches keepAliveTiime, the thread will exit. This value can be set by default when it is not sensitive to resources.

Guess you like

Origin blog.csdn.net/u011582840/article/details/107539090