Thread pool scale adjustment method, apparatus and device, and readable storage medium

By applying machine learning models in thread pools to predict blocking coefficients and dynamically adjusting the thread pool scale, the problem of inaccurate thread pool scale in the existing technology is solved, and the system performance and response speed are significantly improved.

CN120086020APending Publication Date: 2025-06-03中国工商银行股份有限公司湖南省分行
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510209633.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-25
Publication Date
2025-06-03

AI Technical Summary

Technical Problem

In the prior art, the inaccurate scale of thread pools leads to a decline in system performance and the inability to effectively respond to peak demands, resulting in task blocking, delaying and timeout.

Method used

Through the machine learning model trained based on task volume historical data and blocking coefficient historical data, the predicted blocking coefficient is obtained, and the thread pool size is predicted and adjusted according to the predicted blocking coefficient, the task phase is dynamically divided, and the thread pool size is optimized in real time.

Benefits of technology

It realizes accurate dynamic adjustment of thread pool scale, improves task processing performance and system response speed, and avoids resource waste and performance bottlenecks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120086020A_ABST
    Figure CN120086020A_ABST
Patent Text Reader

Abstract

The invention discloses a thread pool scale adjustment method, device and equipment and a readable storage medium, and is applied to the technical field of computers, and the method comprises the steps: carrying out the prediction through a prediction model according to the task load, and obtaining a predicted blocking coefficient; predicting the scale of the thread pool according to the predicted blocking coefficient to obtain a predicted scale of the thread pool, and dividing the task according to the predicted scale of the thread pool to obtain a plurality of stage tasks; after task execution of each stage is finished, a set number of performance parameters are obtained, and a current dynamic blocking coefficient is determined based on the performance parameters and the initial static blocking coefficient; and adjusting the thread pool scale based on the current dynamic blocking coefficient to obtain the adjusted thread pool scale. According to the method, the blocking coefficient is dynamically adjusted based on the multiple performance parameters, so that the blocking coefficient is dynamically changed, the dynamic blocking coefficient can be utilized to dynamically adjust the scale of the thread pool according to the task load, and the scale of the thread pool can be optimized in real time in the task execution process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and particularly to a method, device, equipment and readable storage medium for adjusting the scale of a thread pool. Background Art

[0002] In a multi-threaded environment, a thread pool is an important resource management mechanism and is widely used in high-concurrency scenarios. Traditional thread pool configurations are usually static, that is, a fixed number of threads are defined at system startup. For scenarios with low load, a fixed thread pool size may lead to waste of resources, and the system maintains unnecessary thread overhead. In the case of a sudden increase in load, a fixed thread pool often fails to quickly respond to peak demands, resulting in insufficient thread resources, and then a large number of task blockages, delays, and even task timeouts occur. With the accumulation of blocked tasks, the performance of the entire system deteriorates, affecting the user experience.

[0003] Therefore, how to improve the accuracy of thread pool size adjustment is a technical problem that needs to be urgently solved by those skilled in the art. Summary of the Invention

[0004] In view of this, the purpose of the present invention is to provide a method, device, equipment and readable storage medium for adjusting the scale of a thread pool, which solves the technical problem in the prior art that the inaccurate thread pool size leads to a decline in system performance.

[0005] To solve the above technical problem, the present invention provides a method for adjusting the scale of a thread pool, including:

[0006] Predicting a predicted blocking coefficient using a prediction model based on the task volume; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data;

[0007] Predicting the scale of the thread pool based on the predicted blocking coefficient to obtain a predicted thread pool scale, and dividing the tasks according to the predicted thread pool scale to obtain multiple stage tasks;

[0008] After the execution of each stage task is completed, obtaining a set number of performance parameters, and determining the current dynamic blocking coefficient based on the performance parameters and an initial static blocking coefficient; wherein, the performance parameters are parameters that change during the task execution process, and the initial static blocking coefficient is a blocking coefficient that does not change with system performance;

[0009] Adjusting the scale of the thread pool based on the current dynamic blocking coefficient to obtain an adjusted thread pool scale.

[0010] Optionally, before predicting the predicted blocking coefficient using the prediction model based on the task volume, it further includes:

[0011] Train a linear regression model based on the historical task volume data and the historical blocking coefficient data to obtain the prediction model including the first regression coefficient and the second regression coefficient.

[0012] Optionally, predict the thread pool size according to the predicted blocking coefficient to obtain the predicted thread pool size, and divide the tasks according to the predicted thread pool size to obtain multiple staged tasks, including:

[0013] Determine the predicted thread pool size based on the predicted blocking coefficient and the number of CPU cores according to the thread pool size function; wherein, the thread pool size function is: predicted thread pool size = number of CPU cores / (1 - predicted blocking coefficient);

[0014] Divide the number of tasks by the predicted thread pool size to obtain the number of divided stages, and divide the tasks based on the number of divided stages to obtain multiple staged tasks.

[0015] Optionally, after each staged task is executed, obtain a set number of performance parameters, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient, including:

[0016] Determine the comprehensive weight factor corresponding to each task based on the task priority, the execution time of the task, and the resource requirements of the task in the staged task; wherein, the performance parameters are the task priority, the execution time of the task, and the resource requirements of the task;

[0017] Determine the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient.

[0018] Optionally, the determining the comprehensive weight factor corresponding to each task based on the task priority, the execution time of the task, and the resource requirements of the task in the staged task includes:

[0019] Determine the task priority weight, the execution time weight, and the resource requirement weight corresponding to each task based on the task priority, the execution time of the task, and the resource requirements of the task in the staged task;

[0020] According to the task priority weight, the execution time weight, and the resource requirement weight, use the weight factor formula to obtain the comprehensive weight factor corresponding to each task; wherein the weight factor formula is: comprehensive weight factor = task priority weight × execution time weight × resource requirement weight.

[0021] Optionally, the determining the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient includes:

[0022] Perform weighted average processing based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient to obtain the current dynamic blocking coefficient.

[0023] Optionally, after the execution of each stage task ends, obtain a set number of performance parameters. After determining the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient, it further includes:

[0024] Determine the difference between the current dynamic blocking coefficient and the predicted blocking coefficient;

[0025] Determine whether the difference is greater than a preset difference threshold;

[0026] When exceeding the preset difference threshold, perform the step of adjusting the thread pool size based on the current dynamic blocking coefficient to obtain the adjusted thread pool size;

[0027] When not exceeding the preset difference threshold, execute the task based on the predicted thread pool size.

[0028] The present invention also provides a thread pool size adjustment device, including:

[0029] A blocking coefficient prediction module, configured to perform prediction using a prediction model according to the task volume to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on task volume historical data and blocking coefficient historical data;

[0030] A thread pool size prediction module, configured to predict the thread pool size according to the predicted blocking coefficient to obtain a predicted thread pool size, and divide the task according to the predicted thread pool size to obtain multiple stage tasks;

[0031] A dynamic blocking coefficient determination module, configured to obtain a set number of performance parameters after the execution of each stage task ends, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; wherein, the performance parameter is a parameter that changes during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance;

[0032] A thread pool size adjustment module, configured to adjust the thread pool size based on the current dynamic blocking coefficient to obtain the adjusted thread pool size.

[0033] The present invention also provides a thread pool size adjustment device, including:

[0034] A memory, configured to store a computer program;

[0035] A processor for executing the computer program to implement the steps of the thread pool size adjustment method as described above.

[0036] The present invention also provides a readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, it implements the steps of the thread pool size adjustment method as described above.

[0037] The present invention also provides a computer program product, including a computer program / instructions. When the computer program / instructions are executed by a processor, they implement the steps of the thread pool purchase adjustment method as described above.

[0038] It can be seen that the present invention predicts using a prediction model based on the task volume to obtain a predicted blocking coefficient. Among them, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data. The thread pool size is predicted based on the predicted blocking coefficient to obtain a predicted thread pool size, and the tasks are divided according to the predicted thread pool size to obtain multiple stage tasks. After the execution of each stage task ends, a set number of performance parameters are obtained, and the current dynamic blocking coefficient is determined based on the performance parameters and the initial static blocking coefficient. Among them, the performance parameter is a parameter that changes during the task execution process, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance. The thread pool size is adjusted based on the current dynamic blocking coefficient to obtain an adjusted thread pool size. Compared with the current static thread pool size, the present application dynamically adjusts the blocking coefficient based on multiple performance parameters, making the blocking coefficient dynamically change. Thus, the dynamic blocking coefficient can be used to dynamically adjust the thread pool size according to the task volume, realizing real-time optimization of the thread pool size during the task execution process, and significantly improving the task processing performance and system response speed.

[0039] In addition, the present invention also provides a thread pool size adjustment device, equipment, and readable storage medium, which also have the above beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only the embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on the provided drawings.

[0041] Figure 1 It is a flowchart of a thread pool size adjustment method provided by an embodiment of the present invention;

[0042] Figure 2 It is a schematic diagram of a corresponding relationship provided by an embodiment of the present invention;

[0043] Figure 3 It is a flow example diagram of a thread pool size adjustment method provided by an embodiment of the present invention;

[0044] Figure 4 It is a structural schematic diagram of a thread pool size adjustment device provided by an embodiment of the present invention;

[0045] Figure 5 It is a structural schematic diagram of a thread pool size adjustment device provided by an embodiment of the present invention. Detailed implementation manners

[0046] To make the objectives, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Apparently, the described embodiments are only a part rather than all of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0047] Please refer to Figure 1 , Figure 1 It is a flowchart of a thread pool size adjustment method provided by an embodiment of the present invention. The method may include:

[0048] S101, predicting using a prediction model according to the task volume to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data.

[0049] The execution entity of this embodiment is an electronic device. The electronic device in this embodiment can be a computer, a mobile phone, etc. The task volume in this embodiment means that many tasks can be divided in a task. For example, it can be divided into I / O tasks, non-I / O tasks, etc. The number of task types included in this task is the task volume. This embodiment does not limit the specific task volume. Or the definition of task volume can be understood as the number of sub-task types that can be divided in a task. The task volume is the number of tasks in an entire task sequence, and there are also different types of tasks among them. For example, the task volume in this embodiment can be 5; or the task volume in this embodiment can also be 10. The prediction model in this embodiment is a model obtained by training a machine learning model with task volume historical data and blocking coefficient historical data. This embodiment does not limit the specific method for obtaining the blocking coefficient historical data. For example, the blocking coefficient historical data in this embodiment can be the blocking coefficient obtained based on the existing blocking coefficient determination method; or the blocking coefficient historical data in this embodiment can be the data obtained through simulation based on the self-set dynamic blocking coefficient (determined based on performance parameters and the initial static blocking coefficient) determination method; or in this embodiment, the dynamic blocking coefficient is calculated once in each stage, and at this time, the previous and current dynamic blocking coefficients can be used as the blocking coefficient historical data. This embodiment does not limit the specific machine learning model. For example, the machine learning model in this embodiment can be a neural network model; or the machine learning model in this embodiment can be a linear regression model. The input of the prediction model in this embodiment is the task volume, and the output is the blocking coefficient.

[0050] The application scenarios of this embodiment can include: (1) High-concurrency Web services or API servers: In Web services or API servers that need to process a large number of requests, the load fluctuates with the change in the access volume. Through the dynamic thread pool adjustment strategy of the present invention, the thread pool can be quickly expanded during the peak period of request volume to cope with the traffic peak and reduce the response time; when the traffic is low, the scale of the thread pool can be automatically reduced to save system resources and reduce operating costs. (2) Batch data processing and real-time analysis systems: Data processing systems require a large number of threads to support when the task volume surges (such as data cleaning, batch calculation, log analysis, etc.). The present invention can adjust the scale of the thread pool according to the blocking coefficient of the actual task, improve the processing efficiency of the task, and is particularly suitable for scenarios of data batch processing and high-concurrency computing. (3) Complex multi-task scheduling systems: In systems such as automated operation and maintenance, CI (Continuous Integration) / CD (Continuous Delivery) pipelines, etc., it is often necessary to process a variety of different types of tasks, and the execution time and resource requirements of each task vary greatly. Through the dynamic adjustment strategy of the present invention, the system can intelligently allocate thread resources to ensure that each task receives appropriate thread support, effectively balance the load pressure of the system, and avoid performance degradation caused by resource contention.

[0051] It should be further noted that before predicting the predicted blocking coefficient using the prediction model based on the above-mentioned any embodiment according to the task volume, it may further include: training a linear regression model based on the historical task volume data and the historical blocking coefficient data to obtain a prediction model including a first regression coefficient and a second regression coefficient. The prediction algorithm in this embodiment uses linear regression. This method is simple and efficient, suitable for initial implementation, and performs well when the relationship between the blocking coefficient and the task volume is relatively linear. The algorithm process is as follows: 1. Data preparation: Record the historical task volume TV and the blocking coefficient BC, and construct a training data set. 2. Model training: Use linear regression to train the model and establish the relationship between the task volume TV and the blocking coefficient BC. The training equation , where a and b are regression coefficients. 3. Predict the blocking coefficient: For the new task volume TV_current, use the trained model to calculate the predicted value. 4. Calculate the thread pool size for the next stage based on the predicted blocking coefficient and the calculation formula. The pseudocode is as follows:

[0052] / / Prediction function: Predict the blocking coefficient based on historical data and the current task volume

[0053] {function predictBC(history_data,current_TV):

[0054] a, b = trainLinearModel(history_data) / / Train the model using historical data

[0055] return a * current_TV + b / / Predict the blocking coefficient based on the task volume

[0056] / / Function to train the linear regression model

[0057] function trainLinearModel(history_data):

[0058] X = [data.TV for data in history_data] / / Extract the task volume data

[0059] Y = [data.BC for data in history_data] / / Extract the blocking coefficient data

[0060] a = covariance(X, Y) / variance(X) / / Calculate the slope of the linear regression

[0061] b = mean(Y) - a * mean(X) / / Calculate the intercept of linear regression

[0062] return a, b}

[0063] S102. Predict the thread pool size based on the predicted blocking coefficient to obtain the predicted thread pool size, and divide the tasks according to the predicted thread pool size to obtain multiple stage tasks.

[0064] In this embodiment, the method for predicting the thread pool size based on the predicted blocking coefficient can be to determine the predicted thread pool size corresponding to the predicted blocking coefficient based on the relationship between the existing thread pool size and the blocking coefficient.

[0065] It should be further noted that, in order to improve the accuracy of stage division, the above-mentioned predicting the thread pool size based on the predicted blocking coefficient to obtain the predicted thread pool size, and dividing the tasks according to the predicted thread pool size to obtain multiple stage tasks may include: determining the predicted thread pool size based on the predicted blocking coefficient and the number of CPU cores according to the thread pool size function; where the thread pool size function is: predicted thread pool size = number of CPU cores / (1 - predicted blocking coefficient); dividing the number of tasks by the predicted thread pool size to obtain the number of divided stages, and dividing the tasks based on the number of divided stages to obtain multiple stage tasks. In this embodiment, a specific thread pool size function is given, so that when dividing stages, the number of divided stages can be obtained based on the ratio of the task volume to the predicted thread pool size. For example, when the predicted blocking coefficient is 0.84 and the number of CPU cores is 4, the predicted thread pool size is 25. When the task volume is 100 and the predicted thread pool size is 25, the number of division nodes is 4, and the blocking coefficient range is 0 - 1.

[0066] S103. After each stage task is executed, obtain a set number of performance parameters, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; where the performance parameters are parameters that change during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance.

[0067] This embodiment does not limit the specific set number. For example, the set number in this embodiment is 2; or the set number in this embodiment is 3. This embodiment does not limit the specific performance parameters. The performance parameters of this embodiment can be task priority, task execution time, task resource requirements, etc. Determining the dynamic blocking coefficient based on the performance parameters and the initial static coefficient in this embodiment can be understood as obtaining the performance parameters of each task of the current node, determining the comprehensive performance parameter based on the performance parameters of each task, and determining the current dynamic blocking coefficient based on the comprehensive performance parameter and the initial static blocking coefficient.

[0068] It should be further noted that, in order to improve the accuracy of determining the current dynamic blocking coefficient, after the execution of each stage task ends, obtaining a set number of performance parameters and determining the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient may include:

[0069] S1031. Determine a comprehensive weight factor corresponding to each task based on the task priority, execution time, and resource requirements of each task in the stage task; wherein, the performance parameters are the task priority, execution time, and resource requirements of the task.

[0070] S1032. Determine the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient.

[0071] It can be understood that for compute-intensive tasks, their characteristic is that the work they do requires a large amount of computation, and the CPU runs at full speed during the computation. The proportion of work that causes blocking, such as I / O (input / output), is very small, and mainly CPU resources are consumed. For a thread executing a compute-intensive task, the optimal number of core threads should be equal to the number of CPU cores. When the number of core threads is less than the number of CPU cores, there is always a CPU in a non-running state, wasting the parallel advantage of a multi-core CPU. When the number of core thread pools is greater than the number of CPU cores, all CPUs are always in a running state, but the time consumed by thread switching increases, which may reduce the utilization rate of the CPU. Under the same task scale, the corresponding relationship between the number of core threads and the task running time is as Figure 2 shown Figure 2A schematic diagram of a correspondence relationship provided by an embodiment of the present invention. The characteristics of I / O-intensive tasks are that the work done mainly includes data sending, data reading, etc., which require a large number of I / O operations. The work done by the CPU accounts for a relatively small proportion, mainly consuming I / O resources. For example, network transmission, database reading, etc. When the CPU processes such work, because the speed of I / O is much lower than the processing speed of the CPU, the CPU is in a waiting state for I / O most of the time. When waiting occurs, the CPU resources occupied by the thread can be switched to other threads that are not performing I / O operations until the I / O operation ends, and then the CPU switches back to this thread to continue working. For threads executing I / O-intensive tasks, more threads should be set, rather than matching the number of CPU cores. If the number of thread pools is set too small, it is possible that all threads are in I / O block at a certain time, and the CPU has no tasks to complete, wasting the parallel performance of the CPU. Of course, setting too many threads will also cause a series of problems, such as excessive resource consumption for thread switching, and excessive I / O overload due to too large a concurrency volume. The optimal number of threads for executing I / O-intensive tasks should follow: number of threads = number of CPU cores / (1 - blocking coefficient), where the semantic meaning of the blocking coefficient is the proportion of the amount of tasks completed for I / O in the total amount of tasks, and can be defined as the proportion of the time required to complete the I / O operation in the total time for the thread to complete the work, that is: blocking coefficient = time to complete I / O / total time to complete the task. This blocking coefficient relationship is the existing method for determining the static blocking coefficient. The basis for calculating the number of threads in the existing solution is that it is considered that the blocking coefficient is static and unchanged during the task running process, that is, in the case of only one task, the time to complete the I / O operation and the total time to complete the work are recorded, and the blocking coefficient is calculated through the formula and used in the multi-task scenario. In fact, the blocking coefficient is constantly changing during the process of completing multiple tasks, and it is affected by several aspects: ① Multiple tasks compete for I / O resources simultaneously, which may lead to an increase in the time to complete the I / O operation, thereby affecting the blocking coefficient. ② Other programs in the system occupy CPU resources and I / O resources, thereby affecting the blocking coefficient. Therefore, the static blocking coefficient is not applicable in complex situations, and the blocking coefficient needs to be calculated in other ways. In order to further improve the accuracy and real-time performance of thread pool size adjustment, the present invention proposes a dynamic weight adjustment mechanism based on task type and execution status, and introduces dynamic weights of task priority and resource competition when calculating the blocking coefficient. The traditional method for calculating the blocking coefficient assumes that the contributions of the blocking coefficients of all tasks to thread pool adjustment are equal. However, in practical applications, there may be significant differences in the execution time, resource occupancy, and parallelism of different tasks, resulting in the blocking coefficient of some tasks having a more critical impact on system performance. For this reason, the present invention dynamically adjusts the weights of tasks, and comprehensively considers the priority, execution time, and resource requirements of tasks when calculating the thread pool size, so as to more accurately reflect the actual requirements of tasks for overall system resources.

[0072] To quantify the impact of tasks on system performance, a dynamic weight factor \(W_i\) can be defined for each task and calculated based on the following factors: (1) Task priority (\(P_i\)) The priority of a task affects its occupancy and scheduling of system resources. High-priority tasks may require more thread resources to ensure their timely execution. Therefore, higher weights are assigned to high-priority tasks. If the priority of a task is high, a higher weight is assigned; otherwise, a lower weight is assigned. The priority \(P_i\) can take values in the range \([0, 1]\), where 1 represents the highest priority. , represents the weight of the task priority. (2) Execution time of the task (\(T_i\)) The execution time \(T_i\) of a task reflects the duration during which the task occupies the CPU and other resources. A longer execution time indicates that the task continuously occupies system resources. Therefore, when calculating the size of the thread pool, tasks with longer execution times should be assigned higher weights. The weight of a task can be determined by normalizing the ratio of the task execution time \(T_i\) to the execution times of all current tasks. ; where, is the longest execution time in the current task set, represents the weight of the task execution time. (3) Resource requirements of the task (\(R_i\)) Each task has different requirements for resources such as the CPU, memory, and I / O. Tasks with higher resource requirements may affect the execution of other tasks in the thread pool. Therefore, higher weights should be assigned. The resource requirements \(R_i\) can be quantified based on factors such as the CPU usage rate and memory occupancy of the task. For example, the occupancy of a task for I / O can be reflected by the I / O blocking time. ; where, is the task with the highest resource requirements in the current task set, represents the weight of the task resource requirements. Calculation of the comprehensive weight factor: Considering the priority, execution time, and resource requirements of the task comprehensively, the final weight factor \(W_i\) can be calculated using the following formula: ; or it can also be calculated by addition. Based on the dynamic weight of the task, the calculation formula for the dynamic blocking coefficient is in the form of a weighted average: . is the initial blocking coefficient corresponding to each task. The initial blocking coefficient can be measured by having a thread execute a task once before a large number of tasks are executed and measuring the initial blocking coefficient \(BC_{initial}\), where \(BC_{initial}=\frac{time\ to\ complete\ current}{total\ time\ to\ complete\ task}\).

[0073] It should be further noted that, in order to improve the accuracy of determining the comprehensive weight factor, the above method of determining the comprehensive weight factor corresponding to each task based on the task priority, execution time, and resource requirements of each task in the stage task may include: determining the task priority weight, execution time weight, and resource requirement weight corresponding to each task based on the task priority, execution time, and resource requirements of each task in the stage task; obtaining the comprehensive weight factor corresponding to each task by using the weight factor formula according to the task priority weight, execution time weight, and resource requirement weight; where the weight factor formula is: This embodiment takes into account that the weight combination method of addition calculation cannot reflect the difference in weights, while the multiplication method can reflect the difference in weights, thereby improving the change of the subsequent blocking coefficient.

[0074] It should be further noted that determining the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient may include: performing weighted average processing based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient to obtain the current dynamic blocking coefficient. In this embodiment, since the blocking coefficient is calculated by the weighted average method, the efficiency and accuracy of determining the dynamic blocking coefficient can be improved.

[0075] It should be further noted that, in order to reduce the number of adjustments to the thread pool size and reduce system energy consumption, after each stage task is executed, a set number of performance parameters are obtained. After determining the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient, it may further include: determining the difference between the current dynamic blocking coefficient and the predicted blocking coefficient; determining whether the difference is greater than a preset difference threshold; when the preset difference threshold is exceeded, performing the step of adjusting the thread pool size based on the current dynamic blocking coefficient to obtain the adjusted thread pool size; when the preset difference threshold is not exceeded, executing the task based on the predicted thread pool size. In this embodiment, only when the preset difference threshold is exceeded will the number of threads in the thread pool be adjusted, thereby reducing the adjustment frequency and reducing system energy consumption.

[0076] S104, adjusting the thread pool size based on the current dynamic blocking coefficient to obtain the adjusted thread pool size.

[0077] In this embodiment, adjusting the thread pool size based on the current dynamic blocking coefficient may be adjusted based on the relationship formula between the dynamic blocking coefficient and the thread pool size, and this relationship formula is the relationship formula between the existing static blocking coefficient and the thread pool size.

[0078] A method for adjusting the scale of a thread pool provided by an embodiment of the present invention may include: S101, predicting using a prediction model according to the task volume to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data; S102, predicting the scale of the thread pool according to the predicted blocking coefficient to obtain a predicted thread pool scale, and dividing the tasks according to the predicted thread pool scale to obtain multiple stage tasks; S103, after each stage task is executed, obtaining a set number of performance parameters, and determining the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; wherein, the performance parameters are parameters that change during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance; S104, adjusting the scale of the thread pool based on the current dynamic blocking coefficient to obtain an adjusted thread pool scale. Compared with the current static thread pool scale, in this application, by dynamically adjusting the blocking coefficient based on multiple performance parameters, the blocking coefficient is dynamically changed, so that the dynamic blocking coefficient can be used to dynamically adjust the thread pool scale according to the task volume, realizing real-time optimization of the thread pool scale during the task execution process, and significantly improving the task processing performance and system response speed.

[0079] The present invention aims to solve the deficiencies of the existing dynamic adjustment strategies for thread pool scale in dealing with complex and dynamic loads. Traditional solutions are difficult to accurately adjust the thread pool scale in the case of large load fluctuations or high task blocking rates, resulting in poor system performance or resource waste. The present invention realizes real-time optimization of the thread pool scale during the task execution process through a dynamic thread pool adjustment strategy based on the blocking coefficient, to more accurately respond to load changes, and significantly improves the task processing performance and system response speed while ensuring efficient utilization of system resources.

[0080] For the present invention to be more easily understood, specifically refer to Figure 3 , Figure 3 which is a flow example diagram of a method for adjusting the scale of a thread pool provided by an embodiment of the present invention, and may specifically include:

[0081] S201, before executing the total task with a set task volume, use a thread to execute each task to obtain the initial static blocking coefficient corresponding to each task.

[0082] S202, determine the initial thread pool scale based on the number of CPU cores and the initial static blocking coefficient, and create threads in the thread pool based on the initial thread pool scale.

[0083] This embodiment can use the formula to calculate the initial thread pool scale , NOT is the initial thread pool size, NOC is the number of CPU cores, and BC is the initial static blocking coefficient. This embodiment can record the task volume, thread pool size, initial static blocking coefficient, etc. as historical data.

[0084] S203. When the task volume is less than or equal to the initial thread pool size, it is determined that there is no need to update the thread pool size, and the task is directly executed based on the thread pool.

[0085] S204. When the task volume is greater than the initial thread pool size, a prediction model is used to predict the predicted blocking coefficient for the next stage.

[0086] The prediction model in this embodiment is ; where a and b are regression coefficients, TV is the task volume, and BC is the predicted blocking coefficient obtained by prediction.

[0087] S205. Based on the number of CPU cores and the predicted blocking coefficient, the predicted thread pool size is determined, and the task is divided based on the task volume and the predicted blocking coefficient to obtain stage tasks.

[0088] This embodiment uses a prediction model to calculate the predicted blocking coefficient for the next stage , and predicts the thread pool size accordingly , and then divides the task into stages, and the task volume is evenly distributed to each stage.

[0089] S206. At the end of each stage task, the actual dynamic blocking coefficient is determined through the actual task execution data and compared with the predicted blocking coefficient.

[0090] S207. If the deviation between the actual dynamic blocking coefficient and the predicted blocking coefficient exceeds the preset threshold, the thread pool size is recalculated based on the actual dynamic blocking coefficient, the thread pool size is adjusted, and finally the blocking coefficient prediction model is optimized using the feedback mechanism.

[0091] This embodiment updates the current blocking coefficient BC_dynamic through the actual task execution data at the end of each stage task execution, and compares it with the predicted blocking coefficient. If the deviation exceeds the preset threshold, the thread pool size NOT_dynamic is recalculated based on the latest BC_dynamic, the thread pool size is adjusted, and finally the blocking coefficient prediction model is optimized using the feedback mechanism, thereby further improving the accuracy and execution efficiency of thread pool adjustment. It should be noted that the predicted blocking coefficient in this embodiment will change, and each time the dynamic blocking coefficient is calculated, the blocking coefficient given by the prediction model will also be different. Equivalent to, the previously calculated blocking coefficient is used as the input of the prediction model and trained again, and each time it is different. The strategy can be represented by pseudocode as:

[0092] pthread_create(Task);

[0093] BC_initial = getBC(); / / Calculate the initial blocking coefficient

[0094] NOT_initial = NOC / (1 - BC_initial); / / Calculate the initial thread pool size

[0095] for i ← 0 to NOT_initial do

[0096] pthread_create(Task, NULL);

[0097] if TV <= NOT_initial then

[0098] pthread_pool_work(); / / If the task volume is less than the initial thread pool size, start execution directly

[0099] else

[0100] TIMES = ceil(TV / NOT_initial); / / Calculate the number of task stages

[0101] NUMS = TV / TIMES; / / The amount of tasks assigned to each stage

[0102] while pthread_pool_work() do

[0103] if TV_dynamic % NUMS == 0 then / / Update the blocking coefficient at the end of each stage

[0104] BC_next = predictBC(history_data); / / Predict the next stage blocking coefficient based on historical data

[0105] NOT_next = NOC / (1 - BC_next); / / Calculate the predicted thread pool size

[0106] BC_dynamic = getBC(); / / Obtain the current actual blocking coefficient

[0107] if abs(BC_dynamic - BC_next) > threshold then

[0108] NOT_dynamic = NOC / (1 - BC_dynamic); / / If the deviation is too large, recalculate the thread pool size

[0109] pthread_pool_adjust(NOT_dynamic); / / Adjust the thread pool according to the latest calculation result.

[0110] An embodiment of the present invention proposes a method for adjusting the thread pool size based on a dynamic blocking coefficient, which intelligently predicts and adjusts the thread pool size by real-time monitoring the blocking coefficient of system tasks. This method configures the thread pool size according to the initial blocking coefficient before task execution, and dynamically optimizes the number of threads in the thread pool during task execution according to the change in the task volume. Through this strategy, the present invention can efficiently adjust resource usage according to the change in system load, thereby achieving the optimization of task processing and avoiding resource waste. This method effectively improves the system's response speed and concurrent processing ability, and provides an accurate and flexible optimization scheme for resource management in a multi-threaded environment.

[0111] Beneficial effects brought by the technical solution of the present invention: By introducing a dynamic calculation of the blocking coefficient and an automatic adjustment mechanism for the thread pool size, efficient allocation of thread resources and load adaptability are achieved. Compared with traditional static or simple dynamic adjustment strategies, the present invention can flexibly adjust the thread pool size according to the actual blocking situation of tasks, avoiding waste of thread resources and performance bottleneck problems. The system can quickly increase the number of threads during peak loads to accelerate task processing, and reduce threads during low loads to save resources. This solution not only improves the concurrent processing performance of the system, but also reduces the system's response latency, thus significantly improving resource utilization and the overall efficiency of the system.

[0112] The following introduces the thread pool size adjustment device provided by the embodiment of the present invention. The thread pool size adjustment device described below can be correspondingly referred to the thread pool size adjustment method described above.

[0113] Specifically, please refer to Figure 4 , Figure 4 which is a schematic structural diagram of a thread pool size adjustment device provided by an embodiment of the present invention, and may include:

[0114] A blocking coefficient prediction module 100, configured to predict using a prediction model according to the task volume to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data;

[0115] The thread pool size prediction module 200 is used to predict the thread pool size according to the predicted blocking coefficient, obtain the predicted thread pool size, and divide the tasks according to the predicted thread pool size to obtain multiple stage tasks;

[0116] The dynamic blocking coefficient determination module 300 is used to obtain a set number of performance parameters after the execution of each stage task, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; wherein, the performance parameters are parameters that change during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance;

[0117] The thread pool size adjustment module 400 is used to adjust the thread pool size based on the current dynamic blocking coefficient to obtain the adjusted thread pool size.

[0118] Furthermore, based on any of the above embodiments, the above thread pool size adjustment device may further include:

[0119] The prediction model training module is used to train a linear regression model based on the historical task volume data and the historical blocking coefficient data to obtain the prediction model including the first regression coefficient and the second regression coefficient.

[0120] Furthermore, based on any of the above embodiments, the above thread pool size prediction module 200 may include:

[0121] The predicted thread pool size determination unit is used to determine the predicted thread pool size based on the predicted blocking coefficient and the number of central processing unit cores according to the thread pool size function; wherein, the thread pool size function is: predicted thread pool size = number of central processing unit cores / (1 - predicted blocking coefficient);

[0122] The task division unit is used to divide the number of tasks by the predicted thread pool size to obtain the number of divided stages, and divide the tasks based on the number of divided stages to obtain multiple stage tasks.

[0123] Furthermore, based on any of the above embodiments, the above dynamic blocking coefficient determination module 300 may include:

[0124] The comprehensive weight factor determination unit is used to determine the comprehensive weight factor corresponding to each task based on the task priority, the execution time of the task, and the resource requirements of the task in the stage task; wherein, the performance parameters are the task priority, the execution time of the task, and the resource requirements of the task;

[0125] The dynamic blocking coefficient determination unit is used to determine the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient.

[0126] Further, based on any of the above embodiments, the above comprehensive weight factor determination unit may include:

[0127] An initial weight determination subunit, configured to determine a task priority weight, an execution time weight, and a resource requirement weight corresponding to each task based on the task priority, the execution time of the task, and the resource requirement of each task in the stage task;

[0128] A comprehensive weight factor determination subunit, configured to obtain the comprehensive weight factor corresponding to each task according to the task priority weight, the execution time weight, and the resource requirement weight by using a weight factor formula; wherein, the weight factor formula is: comprehensive weight factor = task priority weight × execution time weight × resource requirement weight.

[0129] Further, based on any of the above embodiments, the dynamic blocking coefficient determination unit may include:

[0130] A weighted average subunit, configured to perform a weighted average process based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient to obtain the current dynamic blocking coefficient.

[0131] Further, based on any of the above embodiments, the above thread pool size adjustment device may further include:

[0132] A difference determination module, configured to determine the difference between the current dynamic blocking coefficient and the predicted blocking coefficient;

[0133] A judgment module, configured to determine whether the difference is greater than a preset difference threshold;

[0134] A thread pool size adjustment dynamic adjustment module, configured to, when exceeding the preset difference threshold, perform the step of adjusting the thread pool size based on the current dynamic blocking coefficient to obtain an adjusted thread pool size;

[0135] A thread pool size maintenance module, configured to, when not exceeding the preset difference threshold, execute tasks based on the predicted thread pool size.

[0136] It should be noted that the order of the modules and units in the above thread pool size adjustment device can be changed before and after without affecting the logic.

[0137] An apparatus for adjusting the scale of a thread pool provided by an embodiment of the present invention may include: a blocking coefficient prediction module 100, configured to predict using a prediction model according to the task volume to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data; a thread pool scale prediction module 200, configured to predict the thread pool scale according to the predicted blocking coefficient to obtain a predicted thread pool scale, and divide the tasks according to the predicted thread pool scale to obtain multiple stage tasks; a dynamic blocking coefficient determination module 300, configured to, after each stage task is executed, obtain a set number of performance parameters, and determine a current dynamic blocking coefficient based on the performance parameters and an initial static blocking coefficient; wherein, the performance parameters are parameters that change during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance; a thread pool scale adjustment module 400, configured to adjust the thread pool scale based on the current dynamic blocking coefficient to obtain an adjusted thread pool scale. Compared with the current static thread pool scale, the present application dynamically adjusts the blocking coefficient based on multiple performance parameters, so that the blocking coefficient changes dynamically, thereby enabling the use of the dynamic blocking coefficient to dynamically adjust the thread pool scale according to the task volume, realizing real-time optimization of the thread pool scale during the task execution, and significantly improving the task processing performance and system response speed.

[0138] Next, an apparatus for adjusting the scale of a thread pool provided by an embodiment of the present invention is introduced. The apparatus for adjusting the scale of a thread pool described below can be mutually corresponding and referred to the method for adjusting the scale of a thread pool described above.

[0139] Please refer to Figure 5 , Figure 5 which is a schematic structural diagram of an apparatus for adjusting the scale of a thread pool provided by an embodiment of the present invention, and may include:

[0140] A memory 10, configured to store computer programs;

[0141] A processor 20, configured to execute the computer programs to implement the above-mentioned method for adjusting the scale of a thread pool.

[0142] The memory 10, the processor 20, and the communication interface 30 all complete communication with each other through a communication bus 40.

[0143] In an embodiment of the present invention, the memory 10 is used to store one or more programs, and the programs may include program codes, and the program codes include computer operation instructions. In an embodiment of the present invention, the memory 10 may store programs for implementing the following functions:

[0144] Predict according to the task volume using a prediction model to obtain a predicted blocking coefficient; wherein, the prediction model is a model obtained by training a machine learning model based on historical task volume data and historical blocking coefficient data;

[0145] Predict the thread pool size according to the predicted blocking coefficient to obtain a predicted thread pool size, and divide the tasks according to the predicted thread pool size to obtain multiple stage tasks;

[0146] After each stage task is executed, obtain a set number of performance parameters, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; wherein, the performance parameters are parameters that change during the task execution, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance;

[0147] Adjust the thread pool size based on the current dynamic blocking coefficient to obtain an adjusted thread pool size.

[0148] In a possible implementation, the memory 10 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system and application programs required for at least one function, etc.; the data storage area may store data created during use.

[0149] In addition, the memory 10 may include a read-only memory and a random access memory, and provide instructions and data to the processor. A part of the memory may also include NVRAM. The memory stores an operating system and operation instructions, executable modules or data structures, or subsets thereof, or extended sets thereof. Among them, the operation instructions may include various operation instructions for implementing various operations. The operating system may include various system programs for implementing various basic tasks and processing hardware-based tasks.

[0150] The processor 20 may be a central processing unit (CPU), an application-specific integrated circuit, a digital signal processor, a field programmable gate array or other programmable logic devices. The processor 20 may be a microprocessor or any conventional processor, etc. The processor 20 may call the program stored in the memory 10.

[0151] The communication interface 30 may be an interface of a communication module for connecting to other devices or systems.

[0152] Of course, it should be noted that Figure 5 The structure shown does not constitute a limitation on the thread pool size adjustment device in the embodiments of the present invention. In actual applications, the thread pool size adjustment device may include more or fewer components than Figure 5 shown, or combine some components.

[0153] The computer-readable storage medium provided by the embodiments of the present invention will be introduced below. The computer-readable storage medium described below can be correspondingly referred to the thread pool size adjustment method described above.

[0154] The present invention also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned thread pool size adjustment method are implemented.

[0155] The computer-readable storage medium may include various media that can store program codes, such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs.

[0156] The embodiments in this specification are described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. The same or similar parts among the embodiments can be referred to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple. For the relevant parts, please refer to the description of the method part.

[0157] Those skilled in the art can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed in this article can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of each example have been generally described according to their functions in the above description. Whether these functions are executed in the form of hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.

[0158] Finally, it should also be noted that in this article, relationships such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.

[0159] The above has introduced in detail a method, apparatus, device and readable storage medium for adjusting the scale of a thread pool. In this article, specific examples are used to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation on the present invention.

Claims

1. A thread pool size adjustment method, characterized in that: include: A prediction model is used to predict the task volume to obtain a predicted blocking coefficient; wherein the prediction model is a model obtained by training a machine learning model based on historical data of the task volume and historical data of the blocking coefficient; Predicting the thread pool size according to the predicted blocking coefficient to obtain a predicted thread pool size, and dividing the tasks according to the predicted thread pool size to obtain multiple stage tasks; After the execution of each stage task is completed, a set number of performance parameters are obtained, and the current dynamic blocking coefficient is determined based on the performance parameters and the initial static blocking coefficient; wherein the performance parameters are parameters that will change during the task execution process, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance; The thread pool size is adjusted based on the current dynamic blocking coefficient to obtain an adjusted thread pool size.

2. The thread pool size adjustment method according to claim 1, characterized in that: Before using the prediction model to predict the task volume and obtain the predicted blocking coefficient, it also includes: The linear regression model is trained based on the task volume historical data and the blocking coefficient historical data to obtain the prediction model including the first regression coefficient and the second regression coefficient.

3. The thread pool size adjustment method according to claim 1, characterized in that: The thread pool size is predicted according to the predicted blocking coefficient to obtain the predicted thread pool size, and the tasks are divided according to the predicted thread pool size to obtain multiple stage tasks, including: Determine the predicted thread pool size based on the thread pool size function according to the predicted blocking coefficient and the number of CPU cores; wherein the thread pool size function is: predicted thread pool size = number of CPU cores / (1-predicted blocking coefficient); The number of divided stages is obtained by dividing the amount of tasks by the predicted thread pool size, and the tasks are divided based on the number of divided stages to obtain a plurality of the stage tasks.

4. The thread pool size adjustment method according to any one of claims 1 to 3, characterized in that: After each stage task is executed, a set number of performance parameters are obtained, and a current dynamic blocking coefficient is determined based on the performance parameters and the initial static blocking coefficient, including: Determine a comprehensive weight factor corresponding to each task based on the task priority, task execution time and task resource requirement of each task in the stage task; wherein the performance parameters are the task priority, the task execution time and the task resource requirement; The current dynamic blocking coefficient is determined based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient.

5. The thread pool size adjustment method according to claim 4, characterized in that: The step of determining the comprehensive weight factor corresponding to each task based on the task priority, task execution time and task resource requirements of each task in the stage task includes: Determine the task priority weight, execution time weight and resource requirement weight corresponding to each task based on the task priority, execution time and resource requirement of each task in the stage task; According to the task priority weight, the execution time weight and the resource requirement weight, the weight factor formula is used to obtain the comprehensive weight factor corresponding to each task; wherein the weight factor formula is: comprehensive weight factor = task priority weight × execution time weight × resource requirement weight.

6. The thread pool size adjustment method according to claim 5, characterized in that: Determining the current dynamic blocking coefficient based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient includes: The current dynamic blocking coefficient is obtained by performing weighted averaging processing based on the comprehensive weight factor corresponding to each task and the initial static blocking coefficient.

7. The thread pool size adjustment method according to claim 1, characterized in that: After each stage task is executed, a set number of performance parameters are obtained, and after the current dynamic blocking coefficient is determined based on the performance parameters and the initial static blocking coefficient, the following is further included: determining a difference between the current dynamic blocking coefficient and the predicted blocking coefficient; Determining whether the difference is greater than a preset difference threshold; When the preset difference threshold is exceeded, the step of adjusting the thread pool size based on the current dynamic blocking coefficient to obtain an adjusted thread pool size is performed; When the preset difference threshold is not exceeded, the task is executed based on the predicted thread pool size.

8. A thread pool size adjustment device, characterized in that: include: A blocking coefficient prediction module is used to predict the blocking coefficient using a prediction model according to the task volume, wherein the prediction model is a model obtained by training a machine learning model based on the task volume historical data and the blocking coefficient historical data; A thread pool size prediction module is used to predict the thread pool size according to the predicted blocking coefficient to obtain the predicted thread pool size, and divide the tasks according to the predicted thread pool size to obtain multiple stage tasks; A dynamic blocking coefficient determination module is used to obtain a set number of performance parameters after the execution of each stage task is completed, and determine the current dynamic blocking coefficient based on the performance parameters and the initial static blocking coefficient; wherein the performance parameters are parameters that will change during the task execution process, and the initial static blocking coefficient is a blocking coefficient that does not change with the system performance; The thread pool size adjustment module is used to adjust the thread pool size based on the current dynamic blocking coefficient to obtain an adjusted thread pool size.

9. A thread pool size adjustment device, characterized in that: include: Memory for storing computer programs; A processor, configured to execute the computer program to implement the steps of the thread pool size adjustment method as described in any one of claims 1 to 7.

10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the thread pool size adjustment method according to any one of claims 1 to 7 are implemented.