Elastic thread pool management method and device based on load prediction and electronic equipment
Through the elastic thread pool management method based on load prediction, the load value is predicted by the Holt-Winters model and the thread pool resources are dynamically adjusted, which solves the problems of low resource utilization, high response delay and high operation and maintenance costs in existing thread pool management, and achieves more efficient resource utilization and response speed.
Patent Information
- Application Number
- CN202510439738.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-09
- Publication Date
- 2025-07-22
AI Technical Summary
The existing thread pool management solutions have problems such as low resource utilization, high load response delay, mixed task resource contention, and high operation and maintenance costs.
Through the elastic thread pool management method based on load prediction, the Holt-Winters model is used to predict future load values, dynamically adjust the number of threads in the thread pool, and route tasks to asynchronous queues or thread pool queues according to task characteristics, reduce resource contention and make resource adjustments in advance.
It improves resource utilization, reduces response delay in burst traffic scenarios, reduces the number of invalid adjustments, and improves the stability and efficiency of the system.
Smart Images

Figure CN120353591A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of thread pools, and in particular, to an elastic thread pool management method, device, and electronic device based on load prediction. Background Art
[0002] The current mainstream thread pool management solutions include: (1) Fixed-size thread pool (such as Java's Executors.newFixedThreadPool), which is simple to implement and only needs to set parameters during initialization. However, it has the following problems: resource waste, high thread idle rate during low-traffic periods; sudden traffic accumulation, when the instantaneous request volume exceeds the queue capacity, the rejection policy is triggered, resulting in task loss; high manual parameter adjustment cost, and operation and maintenance personnel need to manually adjust parameters based on historical experience.
[0003] (2) Queue length feedback adjustment, that is, A[queue full] -> B[expand], C[queue empty] -> D[shrink]. This method can achieve basic dynamic adjustment and meet simple requirements. However, it has the following problems: adjustment lag, that is, the expansion decision is 3-5 monitoring cycles later than the actual demand, resulting in a relatively high percentage of queue accumulation in sudden traffic scenarios; jitter problem, frequent adjustments are triggered at critical loads (such as the number of threads oscillates between 10 and 12), increasing the risk of system instability; ignoring task types: not distinguishing between IO / CPU tasks, resulting in resource contention (such as IO tasks blocking CPU threads).
[0004] (3) Priority scheduling scheme. This method can guarantee high-priority tasks. However, it has the following problems: total resource limitation: high-priority tasks are still limited by the fixed number of threads and cannot handle sudden high-priority requests; high complexity: it is necessary to pre-define task priority classification rules, increasing the development and maintenance costs; there is a monitoring blind spot: lack of the ability to predict the overall load of the thread pool and unable to avoid resource bottlenecks in advance.
[0005] In summary, the existing thread pool management solutions have problems such as low resource utilization, high load response latency, resource contention for mixed tasks, and high operation and maintenance costs. Summary of the Invention
[0006] In view of this, the purpose of the present invention is to provide an elastic thread pool management method, device, and electronic device based on load prediction to improve the problems existing in the existing thread pool management solutions.
[0007] To achieve the above purpose, the technical solution adopted by the present invention is as follows:
[0008] In a first aspect, the present invention provides a method for managing an elastic thread pool based on load prediction, including: obtaining tasks submitted by a client, and routing the tasks to corresponding queues according to the task characteristics of the tasks; wherein, I / O type tasks are routed to an asynchronous queue, and CPU computing type tasks are routed to a thread pool queue; monitoring and collecting thread metric data of the thread pool queue in real time, and obtaining the thread metric data of a first preset time period based on a preset sliding window; inputting the thread metric data of the first preset time period into a prediction model to obtain a predicted load value for a second preset time period; wherein, the prediction model is a Holt-Winters model; and performing thread adjustment based on the predicted load value and a preset load threshold.
[0009] Optionally, routing the tasks to corresponding queues according to the task characteristics of the tasks includes: if the task characteristics of the task include I / O operations and / or the historical duration of the task meets a preset condition, determining the task as an I / O type task and routing the I / O type task to the asynchronous queue, otherwise determining the task as a CPU computing type task and routing the CPU computing type task to the thread pool queue.
[0010] Optionally, before obtaining the tasks submitted by the client, it further includes: creating a basic thread pool, loading the model parameters of the prediction model, and initializing the seasonal factor.
[0011] Optionally, inputting the thread metric data of the first preset time period into the prediction model to obtain the predicted load for the second preset time period includes: initializing the level component and the trend component; iteratively updating the level component, the trend component, and the seasonal factor based on the thread metric data of the first preset time period and the model parameters; and obtaining the predicted load for the second preset time period based on the updated level component, trend component, and seasonal factor.
[0012] Optionally, performing thread adjustment based on the predicted load value and the preset load threshold includes: if the predicted load value is greater than the first load threshold, calculating the expansion increment according to a preset dynamic formula and increasing the number of threads based on the expansion increment; if the predicted load value is less than the second load threshold, reducing the number of threads according to a preset rule; wherein, the preset rule is to reduce one thread each time; and wherein, the second load threshold is less than the first load threshold.
[0013] Optionally, the preset dynamic formula is: Δ = ceil[2×(1 + (predicted load value - expansion threshold) / expansion threshold)]; wherein, Δ represents the expansion increment, and ceil represents the ceiling function.
[0014] Optionally, the above method further includes: if the prediction model calculates abnormally, obtaining the current actual load value and adjusting the threads based on the current actual load value; if the number of CPU computing tasks in the thread pool queue exceeds a preset value, executing the CPU computing tasks based on the I / O type tasks to the asynchronous queue.
[0015] In a second aspect, the present invention provides a flexible thread pool management device based on load prediction, including: a task classification module, configured to obtain tasks submitted by a client and route the tasks to corresponding queues according to the task characteristics of the tasks; wherein, the I / O type tasks are routed to the asynchronous queue, and the CPU computing type tasks are routed to the thread pool queue; a real-time monitoring module, configured to monitor and collect the thread metric data of the thread pool queue in real time and obtain the thread metric data of the first preset time period based on a preset sliding window; a load prediction module, configured to input the thread metric data of the first preset time period into a prediction model to obtain a predicted load value of the second preset time period; wherein, the prediction model is a Holt-Winters model; a thread adjustment module, configured to adjust the threads based on the predicted load value and a preset load threshold.
[0016] In a third aspect, the present invention provides an electronic device, including a processor and a memory, the memory stores computer executable instructions that can be executed by the processor, and the processor executes the computer executable instructions to implement the steps of the method according to any one of the first aspects provided above.
[0017] In a fourth aspect, the present invention provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is run by a processor, it executes the steps of the method according to any one of the first aspects provided above.
[0018] The present invention brings the following beneficial effects:
[0019] The above-mentioned elastic thread pool management method, device and electronic device provided by the present invention first obtain tasks submitted by clients, and route the tasks to corresponding queues according to the task characteristics of the tasks; among them, I / O type tasks are routed to an asynchronous queue, and CPU computing type tasks are routed to a thread pool queue; then, the thread metric data of the thread pool queue is monitored and collected in real time, and the thread metric data for the first preset time period is obtained based on a preset sliding window; then, the thread metric data for the first preset time period is input into a prediction model to obtain a predicted load value for the second preset time period; among them, the prediction model is the Holt-Winters model; finally, thread adjustment is performed based on the predicted load value and a preset load threshold. In the above method, tasks are divided into I / O type tasks and CPU computing type tasks according to task characteristics, and an asynchronous queue and a dedicated thread pool are used respectively for processing, reducing the resource contention rate; the predicted load value for the future second preset time period is predicted based on the thread metric data for the first preset time period, advancing the resource adjustment decision by multiple monitoring cycles, thereby reducing the response delay in the case of burst traffic; the threads are dynamically adjusted through the load threshold, improving the resource utilization rate while reducing the number of ineffective adjustments.
[0020] Other features and advantages of the present invention will be described in the following specification, and, in part, will be obvious from the specification, or will be understood by practicing the present invention. The objectives and other advantages of the present invention are achieved and obtained by the structures particularly pointed out in the specification, the claims, and the drawings.
[0021] To make the above objectives, features, and advantages of the present invention more obvious and understandable, the following specifically enumerates preferred embodiments and, in conjunction with the accompanying drawings, makes a detailed description as follows. Description of the Drawings
[0022] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following will briefly introduce the drawings required for use in the description of the specific embodiments or the prior art. Obviously, the following drawings are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0023] Figure 1 It is a flowchart of an elastic thread pool management method based on load prediction provided by an embodiment of the present invention;
[0024] Figure 2 It is a schematic structural diagram of an elastic thread pool management device based on load prediction provided by an embodiment of the present invention;
[0025] Figure 3 It is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. Detailed Embodiments
[0026] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are some, but not 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 scope of protection of the present invention.
[0027] Currently, the existing thread pool management solutions have the following problems:
[0028] (1) Low resource utilization rate: In the scenario of load fluctuations, the resource idle rate of traditional static thread pools is relatively high; each idle thread occupies about 1MB of memory, resulting in memory waste (for example, a 20-thread pool wastes 16MB of memory during the low-traffic period).
[0029] (2) High load response latency: In the scenario of sudden traffic (a 300% instantaneous increase in the number of requests), the response is slow; the queue accumulation rate > 80%, triggering a task rejection strategy (such as AbortPolicy) resulting in task loss.
[0030] (3) Resource contention for mixed tasks: IO-intensive tasks block CPU threads, resulting in a decrease in the utilization rate of computing resources; the difference in task execution time > 300% (the average execution time of IO tasks is 150ms vs. 50ms for CPU tasks).
[0031] (4) High operation and maintenance costs: Manual intervention is required to adjust parameters; configuration errors increase the system failure rate.
[0032] Based on this, an elastic thread pool management method, device, and electronic device provided by the embodiments of the present invention can improve the problems existing in the existing thread pool management solutions.
[0033] To facilitate the understanding of this embodiment, first, a detailed introduction is given to an elastic thread pool management method based on load prediction disclosed in the embodiments of the present invention. This method can be executed by an electronic device, such as a smart phone, a computer, a tablet computer, etc. Refer to Figure 1 The flowchart of an elastic thread pool management method based on load prediction shown in the figure, which shows that this method mainly includes the following steps S101 to S104:
[0034] Step S101: Obtain the tasks submitted by the client and route the tasks to the corresponding queues according to the task characteristics of the tasks.
[0035] In one embodiment, when the system starts up, the thread pool is initialized first. A basic thread pool (initial number of threads = 2) is created, core thread timeout recycling is enabled, and the model parameters (α, β, γ) of the prediction model are loaded and the seasonal factors are initialized.
[0036] After the client submits a task, the system can analyze the task characteristics in real time (such as: operation type, time consumption, etc.), and then route the task to the corresponding queue according to the task characteristics of the task. Specifically, if the task characteristics of the task include I / O operations and / or the historical time consumption of the task meets the preset conditions, the task is determined as an I / O type task and the I / O type task is routed to the asynchronous queue; otherwise, the task is determined as a CPU computing type task and the CPU computing type task is routed to the thread pool queue.
[0037] In specific implementation, a quick check can be performed when the task is submitted to automatically identify the task characteristics, including the following two points:
[0038] (1) Whether it includes I / O operations such as file reading and writing, network requests, etc.;
[0039] (2) Statistic the historical time consumption of similar tasks (such as the average execution time in the past 10 times);
[0040] If the task characteristics of the task meet the above two situations, it is regarded as an I / O type task, and the others are CPU computing type tasks. The I / O type tasks use CachedThreadPool to avoid blocking, and the CPU computing type tasks are bound to dedicated threads. In addition, the classification criteria for different types of tasks can also be defined according to the business requirements of itself.
[0041] Step S102: Monitor and collect the thread metric data of the thread pool queue in real time, and obtain the thread metric data of the first preset time period based on the preset sliding window.
[0042] In one embodiment, when the system starts up, an independent monitoring thread can also be started to collect the thread metric data at intervals of 5 seconds. After the client submits a task, the monitoring thread can monitor and collect the thread metric data of the thread pool queue in real time, such as: the number of active threads, queue length, CPU utilization, etc.; then based on the preset sliding window, the thread metric data of the first preset time period (such as 60 seconds) is retained. In the embodiment of the present invention, only 12 sampling points (60 - second data) are retained by using the sliding window, reducing the calculation overhead.
[0043] Step S103: Input the thread metric data of the first preset time period into the prediction model to obtain the predicted load value of the second preset time period.
[0044] In one embodiment, the thread metric data for the first preset time period is input into a prediction model to predict the predicted load value for the future second preset time period (such as 5 seconds). Among them, the prediction model in this embodiment adopts the Holt-Winters model.
[0045] Step S104: Adjust the threads based on the predicted load value and a preset load threshold.
[0046] In one embodiment, an asymmetric load threshold is set, that is, a first load threshold (expansion threshold, such as: 0.8) and a second load threshold (contraction threshold, such as: 0.3), where the second load threshold is less than the first load threshold, and the threads are adjusted according to the predicted load value and the load threshold. When setting the load threshold, the first load threshold can be set according to the golden ratio principle: 80% of the system's full-load capacity (leaving 20% as a buffer), which can not only avoid wasting resources due to premature expansion but also prevent performance bottlenecks caused by late expansion. The dual-threshold anti-shake design (0.8 for expansion / 0.3 for contraction) is mainly to prevent frequent adjustments caused by load fluctuations. The division of the threshold can also be analyzed specifically according to the specific business and is not limited here.
[0047] In specific implementation, if the predicted load value is greater than the first load threshold, calculate the expansion increment according to a preset dynamic formula, and increase the threads based on the expansion increment. Specifically, if the predicted load value > 0.8, calculate the expansion increment according to the dynamic formula and increase the threads progressively. The preset dynamic formula is: Δ = ceil[2×(1 + (predicted load value - expansion threshold) / expansion threshold)]; where Δ represents the expansion increment, ceil represents the ceiling function to ensure resource adequacy first; the base 2 is the minimum expansion unit guarantee, and at least 2 threads are added each time to avoid management overhead caused by minor adjustments, and the overload rate = (predicted load value - expansion threshold) / expansion threshold. This embodiment adopts non-linear expansion. The more severe the overload, the greater the expansion amplitude (such as expanding 3 threads when the predicted load is 1.0), and boundary checks can be performed to ensure that the number of threads is in the range of [2, 16].
[0048] Assume that the current thread pool = 4 threads, the expansion threshold MAX = 8; the predicted load value is 85%, then the overload rate = (85 - 80) / 80 = 6.25%; the expansion increment = 2×(1 + 6.25%) = 2.125, rounded up = 3; the new number of threads = 4 + 3 = 7 (not exceeding MAX).
[0049] If the predicted load value is less than the second load threshold, reduce the threads according to a preset rule; the preset rule is to reduce one thread each time. Specifically, if the predicted load value < 0.3, contract the threads. In this embodiment, safe contraction adopts progressive contraction, that is, only one thread is reduced each time to avoid violent fluctuations.
[0050] In the embodiments of the present invention, the adjustment instruction updates the thread pool parameters in a lock-free manner. Specifically, the thread pool updates the parameters using the executor.setCorePoolSize(newSize) function; when adding new threads, a Worker thread is immediately created and added to task processing; when reducing threads, the function allowCoreThreadTimeOut(true) is used to let the idle threads exit automatically. This method can achieve a smooth transition, and the tasks being executed are not affected, ensuring the stability of the system.
[0051] In addition, threads that have been idle for more than 30 seconds can be automatically destroyed to release memory resources, and memory pages are regularly aligned to release memory to reduce fragmentation, thereby improving resource utilization.
[0052] The elastic thread pool management method based on load prediction provided by the embodiments of the present invention divides tasks into I / O type tasks and CPU computing type tasks according to task characteristics, and uses an asynchronous queue and a dedicated thread pool to process them respectively, reducing the resource contention rate; predicting the predicted load value in the future second preset time period based on the thread metric data in the first preset time period, advancing the resource adjustment decision by multiple monitoring cycles, thereby reducing the response delay in the scenario of burst traffic; dynamically adjusting threads through a load threshold, while improving resource utilization, reducing the number of ineffective adjustments.
[0053] In one implementation, the future load is predicted through the Holt-Winters model to trigger resource adjustment in advance, solving the problem of lag in traditional solution adjustments. The Holt-Winters model uses the Triple Exponential Smoothing algorithm to predict the system requirements in the next 5 seconds based on historical load data. The Triple Exponential Smoothing algorithm includes three elements: level, trend, and seasonality. Based on this, in the embodiments of the present invention, when inputting the thread metric data in the first preset time period into the prediction model to obtain the predicted load in the second preset time period, the following methods may be used, including but not limited to:
[0054] First, initialize the level component and the trend component.
[0055] In specific implementation, when inputting the thread metric data in the first preset time period into the prediction model, the input data is first detected to ensure that there is enough data to start the prediction (at least one complete seasonal cycle); then, using the first data point as the initial level reference, the level component is determined; if the data is greater than or equal to 2, the initial trend component (i.e., the difference) is calculated, otherwise the initial trend component is 0.
[0056] Then, based on the thread metric data and model parameters in the first preset time period, the horizontal component, trend component, and seasonal factor are iteratively updated.
[0057] In specific implementation, the model parameters (α, β, γ) are pre-determined parameters. In the embodiments of the present invention, different model parameters can be passed in through an open parameter configuration (constructor input) to adapt to different scenarios and reduce the prediction error caused by fixed parameters.
[0058] Specifically, α controls the weight of new data and historical estimates. The update formula for the horizontal component is:
[0059] L t = α(Y - S t-m ) + (1 - α)(L t-1 + T t-1 )
[0060] Where L t represents the horizontal component at time t, Y represents the thread metric data, S t-m represents the seasonal factor at time t - m, L t-1 represents the horizontal component at time t - 1, and T t-1 represents the trend component at time t - 1.
[0061] β controls the sensitivity of trend changes. The update formula for the trend component is:
[0062] T t = β(L t - L t-1 ) + (1 - β)T t-1
[0063] Where T t represents the trend component at time t.
[0064] γ adjusts the influence of seasonal fluctuations. The update formula for the seasonal factor is:
[0065] S t = γ(Y - L t ) + (1 - γ)S t-m
[0066] Where S t represents the seasonal factor at time t.
[0067] Finally, based on the updated horizontal component, trend component, and seasonal factor, the predicted load for the second preset time period is obtained.
[0068] In specific implementation, the prediction formula is:
[0069]
[0070] When predicting the next time point (h = 1), the latest seasonal factor can be directly used instead of the periodic historical value, and the simplified prediction formula is as follows:
[0071]
[0072] In the embodiments of the present invention, the trend component is dynamically adjusted by exponential smoothing, and β controls the response speed to changes, thereby improving the prediction response speed of burst traffic. The seasonal period period can be configured (set to 5 in the code, corresponding to 25-second data), and the seasonal factor is updated in real time. Each iteration updates the factor at the corresponding seasonal site (seasons.set(i % period,...)), thereby improving the prediction accuracy.
[0073] The embodiments of the present invention also provide an exception handling method, including:
[0074] (1) Prediction failure handling: If the prediction model calculation is abnormal, obtain the current actual load value and perform thread adjustment based on the current actual load value.
[0075] Specifically, if the prediction model calculation is abnormal (such as insufficient data), it degrades to the latest sampled value decision, that is, when the prediction model fails (insufficient data or calculation error), the system will immediately use the current actual load value for decision-making. The latest sampled value (current actual load value) = the load value of the most recent actual measurement (e.g., the CPU usage rate of 75% just counted 5 seconds ago). Make a judgment directly based on this latest true value, and no longer rely on the predicted load value. If the current actual load value > 0.8, expand the capacity; if the current actual load value < 0.3, shrink the capacity. For details, please refer to the foregoing embodiments.
[0076] (2) Resource contention handling: If the number of CPU computing tasks in the thread pool queue exceeds the preset value, the CPU computing tasks are executed in the asynchronous queue based on the I / O type tasks.
[0077] Specifically, when the CPU queue is piled up > 80%, the CPU computing tasks can be temporarily borrowed from the IO queue threads for execution.
[0078] (3) Overload protection: After the fuse is triggered, the system automatically records the diagnostic log (including stack and resource snapshot). Specifically, the trigger of the fuse may include the following situations: At the hardware level: The CPU usage rate is overloaded, such as exceeding 95% for 30S continuously; the memory occupancy is relatively high, and the program frequently has fullgc; the disk IO response is slow, etc.; At the service level: The thread pool is saturated and the rejection times are relatively high; the request timeout rate is relatively high, etc.
[0079] The above method provided by the embodiments of the present invention adopts a predictive elastic scaling mechanism: predicting the load trend through a time series prediction algorithm (Holt-Winters), enabling the thread pool to complete expansion before the traffic peak, which can reduce the idle rate of resources and handle sudden traffic at the same time; adopting a double-threshold anti-jitter mechanism: asymmetric adjustment (the expansion threshold (80%) is higher than the shrinkage threshold (30%)) to form a buffer zone to avoid ineffective scaling, thereby improving resource utilization; through task partitioning, separating I / O type tasks and CPU computing type tasks for processing, improving the overall task processing speed in a mixed scenario; adopting a progressive adjustment strategy to ensure that the adjustment amplitude is positively correlated with the load deviation amount, avoiding service oscillation caused by a single drastic change; if the prediction model calculation is abnormal (such as insufficient data), it degrades to the latest sampled value decision, thereby ensuring system stability; automatically destroying threads that have been idle for more than 30 seconds, thus reducing server resource waste; achieving early anomaly warning through the load time series prediction path, reducing the response delay to sudden traffic.
[0080] For the elastic thread pool management method based on load prediction provided in the foregoing embodiments, the embodiments of the present invention also provide an elastic thread pool management device based on load prediction. Refer to Figure 2 The structural schematic diagram of an elastic thread pool management device based on load prediction shown in the figure schematically shows that the device mainly includes the following parts:
[0081] A task classification module 201, configured to obtain tasks submitted by a client and route the tasks to corresponding queues according to the task characteristics of the tasks; among them, I / O type tasks are routed to an asynchronous queue, and CPU computing type tasks are routed to a thread pool queue;
[0082] A real-time monitoring module 202, configured to monitor and collect thread metric data of the thread pool queue in real time and obtain the thread metric data of a first preset time period based on a preset sliding window;
[0083] A load prediction module 203, configured to input the thread metric data of the first preset time period into a prediction model to obtain a predicted load value of a second preset time period; among them, the prediction model is a Holt-Winters model;
[0084] A thread adjustment module 204, configured to perform thread adjustment based on the predicted load value and a preset load threshold.
[0085] The above-mentioned elastic thread pool management device based on load prediction provided by the embodiments of the present invention divides tasks into I / O type tasks and CPU computing type tasks according to task characteristics, and processes them using an asynchronous queue and a dedicated thread pool respectively, reducing the resource contention rate; predicting the predicted load value in the future second preset time period based on the thread metric data in the first preset time period, advancing the resource adjustment decision by multiple monitoring cycles, thereby reducing the response delay in the scenario of burst traffic; dynamically adjusting threads through load thresholds, while improving resource utilization, reducing the number of ineffective adjustments.
[0086] In one implementation manner, the above-mentioned task classification module 201 is specifically configured to: if the task characteristics of a task include I / O operations and / or the historical elapsed time of the task meets a preset condition, determine the task as an I / O type task, and route the I / O type task to the asynchronous queue; otherwise, determine the task as a CPU computing type task, and route the CPU computing type task to the thread pool queue.
[0087] In one implementation manner, the above-mentioned device further includes an initialization module, which is used to: create a basic thread pool, load the model parameters of the prediction model, and initialize the seasonal factor.
[0088] In one implementation manner, the above-mentioned load prediction module 203 is specifically configured to: initialize the level component and the trend component; iteratively update the level component, the trend component, and the seasonal factor based on the thread metric data and the model parameters in the first preset time period; obtain the predicted load in the second preset time period based on the updated level component, trend component, and seasonal factor.
[0089] In one implementation manner, the above-mentioned thread adjustment module 204 is specifically configured to: if the predicted load value is greater than the first load threshold, calculate the expansion increment according to a preset dynamic formula, and increase the number of threads based on the expansion increment; if the predicted load value is less than the second load threshold, reduce the number of threads according to a preset rule; where the preset rule is to reduce one thread each time; where the second load threshold is less than the first load threshold.
[0090] In one implementation manner, the above-mentioned preset dynamic formula is: Δ = ceil[2×(1 + (predicted load value - expansion threshold) / expansion threshold)]; where Δ represents the expansion increment, and ceil represents the ceiling function.
[0091] In one implementation manner, the above-mentioned device further includes an exception handling module, which is used to: if the prediction model calculation is abnormal, obtain the current actual load value, and perform thread adjustment based on the current actual load value; if the number of CPU computing type tasks in the thread pool queue exceeds a preset value, execute the CPU computing type tasks based on the I / O type tasks to the asynchronous queue.
[0092] It should be noted that for the device provided in the embodiments of the present invention, its implementation principle and the technical effects produced are the same as those of the foregoing method embodiments. For the sake of brief description, for the parts not mentioned in the device embodiments, reference may be made to the corresponding content in the foregoing method embodiments. The specific values provided in the embodiments of the present invention are only exemplary and are not limited herein.
[0093] The embodiments of the present invention further provide an electronic device. Specifically, the electronic device includes a processor and a storage device; a computer program is stored on the storage device, and when the computer program is run by the processor, it executes the method described in any one of the above embodiments.
[0094] Figure 3 FIG. 7 is a schematic structural diagram of an electronic device provided in an embodiment of the present invention. The electronic device 100 includes: a processor 30, a memory 31, a bus 32, and a communication interface 33. The processor 30, the communication interface 33, and the memory 31 are connected through the bus 32; the processor 30 is used to execute an executable module stored in the memory 31, such as a computer program.
[0095] Among them, the memory 31 may include a high-speed random access memory (RAM, Random Access Memory), and may also include a non-volatile memory, such as at least one disk memory. Through at least one communication interface 33 (which can be wired or wireless), a communication connection is realized between the system network element and at least one other network element, and the Internet, a wide area network, a local area network, a metropolitan area network, etc. can be used.
[0096] The bus 32 may be an ISA bus, a PCI bus, an EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience of representation, Figure 3 only a bidirectional arrow is used in FIG. 7, but it does not mean that there is only one bus or one type of bus.
[0097] Among them, the memory 31 is used to store a program. After receiving an execution instruction, the processor 30 executes the program. The method executed by the device defined by the flow process disclosed in any one of the foregoing embodiments of the present invention can be applied to the processor 30 or implemented by the processor 30.
[0098] The processor 30 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the integrated logic circuit of the hardware in the processor 30 or the instructions in the form of software. The above-mentioned processor 30 may be a general-purpose processor, including a central processing unit (CPU for short), a network processor (NP for short), etc.; it may also be a digital signal processor (DSP for short), an application specific integrated circuit (ASIC for short), a field-programmable gate array (FPGA for short), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. It can implement or execute the various methods, steps and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in combination with the embodiments of the present invention can be directly embodied as being executed and completed by the hardware decoding processor, or executed and completed by a combination of the hardware and software modules in the decoding processor. The software module may be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. This storage medium is located in the memory 31, and the processor 30 reads the information in the memory 31 and combines its hardware to complete the steps of the above method.
[0099] The computer program product of the readable storage medium provided by the embodiments of the present invention includes a computer-readable storage medium storing program code, and the instructions included in the program code can be used to execute the methods described in the foregoing method embodiments. For the specific implementation, reference can be made to the foregoing method embodiments and will not be elaborated herein.
[0100] When the above-mentioned functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.
[0101] Finally, it should be noted that: the above-mentioned embodiments are only specific embodiments of the present invention, used to illustrate the technical solutions of the present invention, rather than limiting it. The protection scope of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: any person skilled in the art within the technical scope disclosed by the present invention can still modify the technical solutions described in the foregoing embodiments, or can easily think of changes, or perform equivalent replacements on some of the technical features; and these modifications, changes, or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be covered by the protection scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.
Claims
1. An elastic thread pool management method based on load prediction, characterized in that, Including: Obtain the tasks submitted by the client, and route the tasks to the corresponding queues according to the task characteristics of the tasks; among them, I / O type tasks are routed to the asynchronous queue, and CPU computing type tasks are routed to the thread pool queue; Real-time monitor and collect the thread metric data of the thread pool queue, and obtain the thread metric data of the first preset time period based on a preset sliding window; Input the thread metric data of the first preset time period into the prediction model to obtain the predicted load value of the second preset time period; among them, the prediction model is the Holt-Winters model; Perform thread adjustment based on the predicted load value and a preset load threshold.
2. The method according to claim 1, wherein Routing the task to the corresponding queue according to the task characteristics of the task includes: If the task characteristics of the task include I / O operations and / or the historical elapsed time of the task meets the preset conditions, then determine the task as an I / O type task and route the I / O type task to the asynchronous queue, otherwise determine the task as a CPU computing type task and route the CPU computing type task to the thread pool queue.
3. The method according to claim 1, characterized in that, Before obtaining the tasks submitted by the client, it further includes: Create a basic thread pool, load the model parameters of the prediction model, and initialize the seasonal factor.
4. The method according to claim 3, characterized in that Inputting the thread metric data of the first preset time period into the prediction model to obtain the predicted load of the second preset time period includes: Initialize the level component and the trend component; Iteratively update the level component, the trend component, and the seasonal factor based on the thread metric data of the first preset time period and the model parameters; Obtain the predicted load of the second preset time period based on the updated level component, trend component, and seasonal factor.
5. The method according to claim 1, characterized in that, Performing thread adjustment based on the predicted load value and a preset load threshold includes: If the predicted load value is greater than the first load threshold, calculate the expansion increment according to a preset dynamic formula, and increase the number of threads based on the expansion increment; If the predicted load value is less than the second load threshold, reduce the number of threads according to a preset rule; where the preset rule is to reduce one thread each time; where the second load threshold is less than the first load threshold.
6. The method according to claim 5, wherein The preset dynamic formula is: Δ = ceil[2×(1 + (predicted load value - expansion threshold) / expansion threshold)]; Among them, Δ represents the expansion increment, and ceil represents the ceiling function.
7. The method according to claim 1, wherein It further includes: If the prediction model calculation is abnormal, obtain the current actual load value, and perform thread adjustment based on the current actual load value; If the number of CPU computing type tasks in the thread pool queue exceeds the preset value, then execute the CPU computing type tasks based on the I / O type tasks to the asynchronous queue.
8. An elastic thread pool management device based on load prediction, characterized in that, Including: A task classification module, used to obtain the tasks submitted by the client, and route the tasks to the corresponding queues according to the task characteristics of the tasks; among them, I / O type tasks are routed to the asynchronous queue, and CPU computing type tasks are routed to the thread pool queue; A real-time monitoring module, used to real-time monitor and collect the thread metric data of the thread pool queue, and obtain the thread metric data of the first preset time period based on a preset sliding window; A load prediction module, configured to input the thread metric data of the first preset time period into a prediction model to obtain a predicted load value for a second preset time period; wherein, the prediction model is a Holt-Winters model; A thread adjustment module, configured to perform thread adjustment based on the predicted load value and a preset load threshold.
9. An electronic device, characterized in that, It includes a processor and a memory, the memory stores computer-executable instructions that can be executed by the processor, and the processor executes the computer-executable instructions to implement the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, on which a computer program is stored, characterized in that, When the computer program is run by the processor, it executes the steps of the method according to any one of claims 1 to 7 above.
Citation Information
Cited By
IDC server resource dynamic scheduling method based on load prediction
CN121092304A
Video business card generation method and device, video business card playing method and device, equipment, medium and product
CN121665087A