A non-intrusive thread pool dynamic management method and system
By enhancing the bytecode of the thread pool class using Java Agent technology, and combining configuration monitoring and adaptive queue capacity adjustment algorithms, the issues of flexibility and timeliness in thread pool management are resolved. This enables dynamic parameter adjustment and adaptive queue management at runtime, reducing modification costs and improving response speed.
Patent Information
- Application Number
- CN202511165643.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-20
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2045-08-20
AI Technical Summary
Existing thread pool management methods lack flexibility and timeliness. Static configuration adjustments require restarting the application, making it impossible to dynamically adjust them based on actual conditions during program execution. Furthermore, they are intrusive to the program, resulting in high modification costs and risks.
By intercepting thread pool class loading requests using Java Agent technology, bytecode enhancement is performed on the thread pool class. Combined with configuration listening service and adaptive queue capacity adjustment algorithm, dynamic management of thread pool parameters is achieved, including periodic collection of task execution metrics and expansion/shrinking of adaptive queue capacity.
It enables dynamic adjustment of thread pool parameters without modifying application code, provides adaptive control of the task queue, reduces development and deployment costs, is suitable for programs using various thread pools, and has the advantages of fast response and strong framework compatibility.
Smart Images

Figure CN120653454B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data management technology, specifically to a non-intrusive thread pool dynamic management method and system. Background Technology
[0002] In existing technologies, thread pool adjustment methods mainly include the following approaches:
[0003] By adjusting static configurations during program initialization, core thread pool parameters such as the number of core threads, maximum number of threads, thread activity time, and task queue length are loaded from the configuration file. Once loaded, the configuration cannot be adjusted during runtime; adjusting parameters requires modifying the configuration file and restarting the application. This approach cannot provide timely adjustments in the face of sudden traffic surges and load spikes.
[0004] By modifying the native thread pool or introducing a specific framework for dynamic management, this method involves modifying the JDK's native thread pool or introducing third-party dependencies, which is intrusive to the original program code and has high modification and maintenance costs.
[0005] In summary, existing thread pool management methods have the following drawbacks:
[0006] 1) Lack of flexibility and timeliness: Adjusting static configurations requires restarting the application, making it impossible to adjust in a timely manner based on actual conditions during program operation.
[0007] 2) It is intrusive to the program, requiring modification of the program code and the introduction of a new, customized thread pool to achieve dynamic parameter management, which may damage the original program logic, resulting in high modification costs and risks. Summary of the Invention
[0008] To help solve the above-mentioned technical problems, this application provides a non-intrusive thread pool dynamic management method and system.
[0009] Firstly, this application provides a non-intrusive dynamic thread pool management method, which includes:
[0010] S1. During the application loading phase of the virtual machine, the loading request of the thread pool class is intercepted through the Java Agent;
[0011] S2. Perform bytecode enhancement on the thread pool class, including:
[0012] S21. Add a configuration listening service to the thread pool constructor to bind the thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy.
[0013] S22. Collect metrics before, during and after the execution of methods in the thread pool, periodically collect and report various monitoring metrics of the thread pool during runtime to the monitoring module. The monitoring metrics include task execution time, number of active threads, length of the blocking queue and number of times the rejection policy is executed.
[0014] S3. Runtime dynamic management, including:
[0015] S31. When the above configuration monitoring service detects a configuration change, it initiates an adjustment of the relevant thread pool parameters.
[0016] S32. When the monitoring and reporting cycle is reached, the collected indicators will be reported;
[0017] S33. The blocking queue type set in the thread pool parameters is a bounded queue. Based on the adaptive queue capacity adjustment algorithm and combined with the collected monitoring indicators, corresponding expansion and contraction operations are performed.
[0018] Preferably, S33 includes: an adaptive queue capacity adjustment algorithm including:
[0019] Expansion operation:
[0020] The expansion score ES is calculated using a multi-factor weighted composite formula.
[0021] Where W1-W4 are the weights of different factors, Waitavg is the average waiting time of the task, Waitth is the waiting time threshold, q is the length of the current waiting task queue, Capacity is the queue capacity, Taskreject is the number of tasks that trigger the rejection policy, Taskall is the total number of executed tasks, Thact is the number of active threads in the thread pool, and Thmax is the maximum number of threads.
[0022] When the ES value is greater than the preset expansion threshold and the system resources are sufficient, the expansion operation is performed and atomic queue migration is executed.
[0023] Preferably, S33 includes: an adaptive queue capacity adjustment algorithm including:
[0024] Reduced capacity operation:
[0025] The reduction score is calculated using a multi-factor weighted composite formula:
[0026] Where W5-W7 are the weights of different factors, Thact is the number of active threads in the thread pool, Thmax is the maximum number of threads, Tasksub is the number of task submissions within the time window, Ratepeak is the peak arrival rate of the task, and Twindow is the statistical time window.
[0027] When the SS value of a number of consecutive preset collection cycles exceeds the preset reduction threshold, and there is no reduction operation within the preset operation time, a reduction operation is performed.
[0028] Preferably, S2 includes:
[0029] Intercept JVM class loading requests and identify thread pool classes;
[0030] Modify the constructor of the loading request to inject the configuration listener service;
[0031] Enhance task execution methods to insert metric collection logic;
[0032] The enhanced bytecode is then returned to the JVM for loading.
[0033] Preferably, S3 includes:
[0034] Periodically collect and monitor indicators and calculate decision factors;
[0035] Queue adjustments are triggered based on expansion / shrinkage conditions;
[0036] Record capacity change events and generate adjustment reports.
[0037] Secondly, this application provides a non-intrusive thread pool dynamic management system, wherein the non-intrusive thread pool dynamic management method described in any of the first aspects includes:
[0038] The Java Agent module is used to intercept the bytecode of thread pool classes when the virtual machine loads the application;
[0039] A bytecode enhancement engine is used to enhance the constructors of thread pool classes.
[0040] The configuration listening module is injected into the thread pool constructor through the bytecode enhancement engine to dynamically bind thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy.
[0041] The metric collection module is injected into the thread pool task execution method through the bytecode enhancement engine to periodically collect and report the runtime metrics of the thread pool.
[0042] The task queue capacity adjustment module is used to perform adaptive queue capacity adjustment based on collected monitoring metrics when the thread pool uses a bounded blocking queue.
[0043] In summary, the advantages of this invention compared to the prior art are as follows:
[0044] 1. It is transparent to the application and requires no modification to the existing application. Through bytecode interception technology, the thread pool parameters can be managed and monitored by adding the -javaagent parameter when the program starts.
[0045] 2. Implement adaptive adjustment of the thread pool task queue. Through a multi-dimensional decision model, different decision factors are assigned different weights for comprehensive judgment, and the task queue is automatically expanded or shrunk. In addition, anti-jitter protection is introduced to avoid frequent triggering of shrinkage.
[0046] The method in this invention is universally applicable, effective not only for thread pools in business applications but also for any application that uses thread pools, such as common components and frameworks like Tomcat and Dubbo. It allows for easy customization of thread pool parameters in basic components. Attached Figure Description
[0047] Figure 1 This is a schematic diagram of the bytecode enhancement principle in this invention;
[0048] Figure 2 This is a schematic block diagram of a non-intrusive thread pool dynamic management method according to the present invention;
[0049] Figure 3 This is a flowchart illustrating the bytecode enhancement process using Java Agent technology in this invention.
[0050] Figure 4 This is a flowchart of the adaptive adjustment process for the task queue in this invention. Detailed Implementation
[0051] The present application will be further described below with reference to the accompanying drawings. The structure and principle of the present application are very clear to those skilled in the art. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
[0052] Figure 1 This is a schematic diagram of the bytecode enhancement principle in this invention, which includes the following three basic elements: the application containing the thread pool is a business application written in Java; the Java Agent component contains a bytecode enhancement engine, which is a special JAR file that can modify and enhance the bytecode of the thread pool class in the business application; and the JVM virtual machine is the Java environment for running the application.
[0053] Figure 2 This is a schematic block diagram of a non-intrusive thread pool dynamic management method according to the present invention. The method includes:
[0054] S1. During the application loading phase of the virtual machine, the loading request of the thread pool class is intercepted through the Java Agent;
[0055] S2. Perform bytecode enhancement on the thread pool class, including:
[0056] S21. Add a configuration listening service to the thread pool constructor to bind the thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy.
[0057] S22. Collect metrics before, during and after the execution of methods in the thread pool, periodically collect and report various monitoring metrics of the thread pool during runtime to the monitoring module. The monitoring metrics include task execution time, number of active threads, length of the blocking queue and number of times the rejection policy is executed.
[0058] S3. Runtime dynamic management, including:
[0059] S31. When the above configuration monitoring service detects a configuration change, it initiates an adjustment of the relevant thread pool parameters.
[0060] S32. When the monitoring and reporting cycle is reached, the collected indicators will be reported;
[0061] S33. The blocking queue type set in the thread pool parameters is a bounded queue. Based on the adaptive queue capacity adjustment algorithm and combined with the collected monitoring indicators, corresponding expansion and contraction operations are performed.
[0062] In this embodiment, the startup parameter is -javaagent, the agent component is Java Agent, and the virtual machine is JVM virtual machine.
[0063] S33 includes: Adaptive queue capacity adjustment algorithm including:
[0064] Expansion operation:
[0065] The expansion score was calculated using a multi-factor weighted composite formula. ES :
[0066] ,in W 1- W 4 represents the weights of different factors. Wait avg The average waiting time for the task. Wait th As the waiting time threshold, q This represents the current length of the waiting task queue. Capacity For queue capacity, Task reject To trigger the rejection policy, the number of tasks Task all This represents the total number of tasks that have been executed. Th act This represents the number of active threads in the thread pool. Th max Maximum number of threads;
[0067] when ES If the value is greater than the preset expansion threshold and the system resources are sufficient, then an expansion operation will be performed and an atomic queue migration will be executed.
[0068] Reduced capacity operation:
[0069] The reduction score is calculated using a multi-factor weighted composite formula:
[0070] ,in, W 5 - W 7 For the weights of different factors, Th act This represents the number of active threads in the thread pool. Th max The maximum number of threads, Task sub The number of tasks submitted within the time window. Rate peak The peak arrival rate of the task. T window For statistical time windows;
[0071] When the number of consecutive preset collection cycles is [number], SS If all values exceed the preset shrinkage threshold and there is no shrinkage operation within the preset operation time, a shrinkage operation will be performed.
[0072] Specifically, the configuration center is responsible for issuing thread pool-related parameters at runtime. After bytecode enhancement by the Java Agent component, the thread pool in the application possesses the capabilities of parameter monitoring, metric collection, and dynamic adjustment. Once instantiated, the enhanced thread pool can monitor changes to configuration center parameters and dynamically adjust thread pool parameters at runtime. Monitoring metrics can be periodically reported through monitoring points. When the thread pool task queue uses a bounded queue, the queue size can be adaptively calculated and adjusted based on the collected metrics.
[0073] S2 includes:
[0074] Intercept JVM class loading requests and identify thread pool classes;
[0075] Modify the constructor of the loading request to inject the configuration listener service;
[0076] Enhance task execution methods to insert metric collection logic;
[0077] The enhanced bytecode is then returned to the JVM for loading.
[0078] 1. During the application loading phase of the virtual machine, Java agent bytecode enhancement technology is used to enhance the bytecode of thread pool-related classes without any code intrusion into the application. The enhancement process is as follows: Figure 3 ,
[0079] 1) Business applications are started using the -javaagent parameter.
[0080] 2) The bytecode enhancement engine intercepts the virtual machine's class loading requests.
[0081] 3) Determine if the loaded class is a thread pool class. If so, proceed to step 4). Otherwise, intercept the next class.
[0082] 4) Obtain the bytecode of the thread pool class.
[0083] 5) Add a configuration listening service to its constructor to bind thread pool parameters to the configuration center. These parameters include the maximum number of threads, the core number of threads, the blocking queue type, thread keep-alive time, and the rejection policy. Collect metrics during the early, middle, and late stages of thread pool execution, periodically collect and report various runtime metrics of the thread pool to the monitoring module, including task execution time, active thread count, actual blocking queue length, and the number of rejection policy executions.
[0084] 6) The enhanced class bytecode is loaded and instantiated by the virtual machine.
[0085] S3 includes:
[0086] Periodically collect and monitor indicators and calculate decision factors;
[0087] Queue adjustments are triggered based on expansion / shrinkage conditions;
[0088] Record capacity change events and generate adjustment reports.
[0089] 2. During the runtime dynamic management phase, when the configuration monitoring service detects a configuration change, it initiates adjustments to the relevant thread pool parameters. When the monitoring reporting cycle is reached, the collected metrics are reported.
[0090] Furthermore, if the blocking queue type set in the above thread pool parameters is a bounded queue, then according to the adaptive queue capacity adjustment algorithm and combined with the collected monitoring indicators, corresponding expansion and contraction operations will be performed. For details, please refer to [link / reference needed]. Figure 4 .
[0091] 1) Periodically collect and monitor metrics after the thread pool starts.
[0092] 2) Multi-factor weighted composite calculation for expansion using collected indicators, the formula is as follows:
[0093] In the formula, W 1- W 4 represents the weights of different factors. Wait avg The average waiting time for the task. Wait th As the waiting time threshold, q This represents the current length of the waiting task queue. Capacity For queue capacity, Task reject To trigger the rejection policy, the number of tasks Task all The total number of tasks performed. Th act This represents the number of active threads in the thread pool. Th max This represents the maximum number of threads.
[0094] when ES If the value exceeds the set threshold and the CPU load is sufficient for heap memory resources, an expansion operation will be performed. A new task queue will be created with 150% of the current queue capacity, and atomic queue migration will be performed. Expansion operation: An expansion score is calculated based on the first multi-factor weighted formula. When the score exceeds the expansion threshold and system resources are sufficient, the queue capacity will be increased to 150% of the current capacity.
[0095] 3) Use the collected indicators to perform multi-factor weighted composite calculation for scaling down, the formula is:
[0096] In the formula, W 5 - W 7 For the weights of different factors, Th act This represents the number of active threads in the thread pool. Th max The maximum number of threads, Task sub The number of tasks submitted within the time window. Rate peak The peak arrival rate of the task. T window This represents the statistical time window.
[0097] When three consecutive cycles SS If all values are greater than the reduction threshold, and no reduction operation has been performed within 60 seconds prior to the current time, then the queue capacity is reduced to 90% of the current value, and the reduction operation time is recorded. Reduction operation: A reduction score is calculated based on the second multi-factor weighted formula. When the score exceeds the reduction threshold for three consecutive collection cycles and more than 60 seconds have passed since the last reduction operation, the queue capacity is reduced to 90% of the current capacity.
[0098] 4) After the queue adaptive adjustment is completed, record the queue capacity change event and generate an adjustment report.
[0099] Through the above implementation methods, this invention innovatively introduces Java Agent technology, enabling runtime parameter adjustment and metric monitoring of the thread pool without any code intrusion into the program, and proposes an adaptive task queue adjustment method. This solves the problems of thread pool parameters not being able to be dynamically modified at runtime and the queue size not being adjusted in a timely manner. Furthermore, using this invention requires no secondary code development of the JDK's native thread pool, is non-intrusive to the application, and reduces development and deployment costs for technical R&D personnel. It has broad application prospects and practical value in fields such as cloud computing and microservices.
[0100] This invention also proposes a non-intrusive thread pool dynamic management system, employing the aforementioned non-intrusive thread pool dynamic management method. The system includes:
[0101] The Java Agent module is used to intercept the bytecode of thread pool classes when the virtual machine loads the application;
[0102] A bytecode enhancement engine is used to enhance the constructors of thread pool classes.
[0103] The configuration listening module is injected into the thread pool constructor through the bytecode enhancement engine to dynamically bind thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy.
[0104] The metric collection module is injected into the thread pool task execution method through the bytecode enhancement engine to periodically collect and report the runtime metrics of the thread pool.
[0105] The task queue capacity adjustment module is used to perform adaptive queue capacity adjustment based on collected monitoring metrics when the thread pool uses a bounded blocking queue.
[0106] In summary, this invention, based on Java Agent technology, achieves dynamic management with zero code intrusion, supporting millisecond-level hot updates of parameters such as core thread count, maximum thread count, and queue capacity, without requiring application restarts or modifications to business code. It also provides adaptive control of the thread pool task queue, innovatively proposing a bidirectional elastic scaling algorithm that integrates multiple dimensions such as task waiting time, thread idle rate, and system load to achieve intelligent scaling up / down decisions. Compared to traditional solutions, it has core advantages such as zero modification cost, fast dynamic response, and strong framework compatibility, providing out-of-the-box thread pool optimization capabilities for high-concurrency systems.
Claims
1. A non-intrusive dynamic management method for thread pools, characterized in that, include: S1. During the application loading phase of the virtual machine, the loading request of the thread pool class is intercepted through the Java Agent; S2. Perform bytecode enhancement on the thread pool class, including: S21. Add a configuration listening service to the thread pool constructor to bind the thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy. S22. Collect metrics before, during and after the execution of methods in the thread pool, periodically collect and report various monitoring metrics of the thread pool during runtime to the monitoring module. The monitoring metrics include task execution time, number of active threads, length of the blocking queue and number of times the rejection policy is executed. S3. Runtime dynamic management, including: S31. When the above configuration monitoring service detects a configuration change, it initiates an adjustment of the relevant thread pool parameters. S32. When the monitoring and reporting cycle is reached, the collected indicators will be reported; S33. The blocking queue type set in the thread pool parameters is a bounded queue. Based on the adaptive queue capacity adjustment algorithm and combined with the collected monitoring indicators, corresponding expansion and contraction operations are performed. S33 includes: an adaptive queue capacity adjustment algorithm including: Expansion operation: The expansion score was calculated using a multi-factor weighted composite formula. ES : ,in W 1- W 4 represents the weights of different factors. Wait avg The average waiting time for the task. Wait th As the waiting time threshold, q This represents the current length of the waiting task queue. Capacity For queue capacity, Task reject To trigger the rejection policy, the number of tasks Task all This represents the total number of tasks that have been executed. Th act This represents the number of active threads in the thread pool. Th max Maximum number of threads; when ES If the value is greater than the preset expansion threshold and the system resources are sufficient, then an expansion operation will be performed and an atomic queue migration will be executed. S33 includes: an adaptive queue capacity adjustment algorithm including: Reduced capacity operation: The reduction score is calculated using a multi-factor weighted composite formula: ,in, W 5 - W 7 For the weights of different factors, Th act This represents the number of active threads in the thread pool. Th max The maximum number of threads, Task sub The number of tasks submitted within the time window. Rate peak The peak arrival rate of the task. T window For statistical time windows; When the number of consecutive preset collection cycles is [number], SS If all values exceed the preset shrinkage threshold and there is no shrinkage operation within the preset operation time, a shrinkage operation will be performed.
2. The non-intrusive thread pool dynamic management method according to claim 1, characterized in that, S2 includes: Intercept JVM class loading requests and identify thread pool classes; Modify the constructor of the loading request to inject the configuration listener service; Enhance task execution methods to insert metric collection logic; The enhanced bytecode is then returned to the JVM for loading.
3. The non-intrusive thread pool dynamic management method according to claim 1, characterized in that, S3 includes: Periodically collect and monitor indicators and calculate decision factors; Queue adjustments are triggered based on expansion / shrinkage conditions; Record capacity change events and generate adjustment reports.
4. A non-intrusive thread pool dynamic management system, characterized in that, The non-intrusive thread pool dynamic management method described in any one of claims 1 to 3 includes: The Java Agent module is used to intercept the bytecode of thread pool classes when the virtual machine loads the application; A bytecode enhancement engine is used to enhance the constructors of thread pool classes. The configuration listening module is injected into the thread pool constructor through the bytecode enhancement engine to dynamically bind thread pool parameters to the configuration center. The thread pool parameters include the maximum number of threads, the core number of threads, the blocking queue type, the thread keep-alive time, and the rejection policy. The metric collection module is injected into the thread pool task execution method through the bytecode enhancement engine to periodically collect and report the runtime metrics of the thread pool. The task queue capacity adjustment module is used to perform adaptive queue capacity adjustment based on collected monitoring metrics when the thread pool uses a bounded blocking queue.
Citation Information
Patent Citations
Method and system for dynamically adjusting parameters of thread pool during operation, medium and terminal
CN115686785A
Thread pool monitoring method and device, electronic equipment and storage medium
CN115981962A