Method for intelligently adjusting thread pool

By monitoring the system performance indicators during the thread pool operation, automatically adjusting the number of core threads and the maximum capacity of the task queue, the problem of unreasonable configuration of thread pool parameters in the existing technology is solved, intelligent adjustment of thread pools is realized, and the system's operating efficiency and reliability are improved.

CN119597473APending Publication Date: 2025-03-11FOCUS TECH

Patent Information

Application Number
CN202411680938.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-22
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

The existing technology cannot intelligently adjust thread pool parameters according to task load conditions and system operation performance, resulting in unreasonable parameter configuration and may cause system failures, such as task rejection exceptions or memory overflow.

Method used

By monitoring the number of logical CPUs, physical CPU usage and memory usage during the thread pool operation, the number of core threads and maximum capacity of task queues are automatically adjusted in real time, and intelligent adjustment of thread pools is achieved.

Benefits of technology

This reduces the dependence of parameter regulation on the experience of technical personnel, reduces the problems of system performance instability, response time delay or resource waste caused by improper parameter setting, improves the operation efficiency and reliability of the business system, and balances the performance and resource utilization of the thread pool.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597473A_ABST
    Figure CN119597473A_ABST
Patent Text Reader

Abstract

The invention discloses a method for intelligently adjusting a thread pool, which is characterized by comprising the following steps of: judging the task execution capability of the thread pool by monitoring and comprehensively analyzing system performance indexes of logic CPU (Central Processing Unit) quantity, physical CPU utilization rate and memory utilization rate in the running process of the thread pool; therefore, the number of core threads and the maximum capacity of the task queue are automatically and elastically adjusted in real time, intelligent adjustment of the thread pool is achieved, dependence of parameter regulation on experience of technicians is reduced, the problems of unstable system performance, response time delay or resource waste and the like caused by improper parameter setting are reduced, and the service life of the system is prolonged. The operation efficiency and reliability of a service system are effectively improved, and the performance of the thread pool and the resource utilization rate are balanced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of computer technology, and in particular relates to a method for intelligently adjusting a thread pool. Background Art

[0002] Computer programs use threads to execute tasks and process data. When processing concurrent tasks or batch data, the processing efficiency of a single thread is very low, and multi-threading is generally chosen. Therefore, Java developers use thread pools to complete multi-threading. Thread pools are a way to manage threads. They can reuse a certain number of threads to execute multiple tasks, avoiding the overhead of frequently creating and destroying threads. A work queue is maintained inside the thread pool to store tasks waiting to be executed; threads in the thread pool will take tasks from the work queue and execute them. When all threads are busy, new tasks will wait in the queue until a thread becomes free. Therefore, as the executor of task scheduling, the thread pool provides thread resources for the execution of tasks.

[0003] In order to use the thread pool, Java developers will set the thread pool and configurable thread pool parameters when developing the program. The key thread pool parameters are the number of core threads (corePoolSize), the maximum number of threads (maximumPoolSize), and the blocking queue size (blockingQueueSize). These parameter configurations need to be completed before the application is started. Reasonable configuration of these parameters has an important impact on system performance and resource utilization. Once the parameter configuration is unreasonable, it will cause failures and bring unpredictable problems to the system. However, the Java thread pool only allows the configuration of thread pool parameters once, which means that after the application is started, the thread pool parameters cannot be modified dynamically. This one-time parameter configuration limitation brings difficulties in initial configuration and inconvenience in tuning.

[0004] Nowadays, JMX technology, third-party libraries and monitoring tools have realized dynamic modification of thread pools; the patent "Thread Pool Parameter Processing Method, System, Computer Equipment, Medium and Program Product" (Application Number: 202210887242.1) also proposes a message subscription-based method, in which the business system subscribes to configuration change messages, receives configuration modification information by adding a management console, and transmits the business system response parameter update through the message middleware. However, no matter which method is used, it is impossible to ensure the rationality and accuracy of the parameter modification configuration, and program failures caused by incorrect configuration will still occur. For example, when processing a flash sale business, if the influx of traffic on the day of launch exceeds expectations, the thread pool may not be able to handle excessive tasks before it has time to modify to the optimal parameters, resulting in a large number of task rejection exceptions, causing production accidents; or when processing user download requests, if the blocking queue parameter blockingQueueSize is incorrectly estimated and set to a too large value, a large number of request tasks will accumulate in memory, causing a memory overflow failure.

[0005] The reason for the above problems is that no matter how the parameters are modified, the values ​​of the parameters still rely on the experience, evaluation and configuration of the technicians, and cannot be adjusted intelligently according to the task load and system performance. Therefore, an intelligent thread pool adjustment technology is needed to improve the rationality, timeliness and flexibility of parameter settings. Summary of the invention

[0006] In order to solve the existing technical problems, the present invention provides a method for intelligently adjusting a thread pool.

[0007] The technical solution of the present invention is as follows: a method for intelligently adjusting a thread pool, the specific steps comprising:

[0008] Step 1: During the operation of the thread pool, the number of active threads and the number of core threads of the thread pool are collected and compared. If the number of active threads is less than the number of core threads, the number of logical CPUs of the current computer is obtained and step 2 is executed. Otherwise, the average utilization rate of the physical CPU of the current computer is obtained and step 3 is executed. The computer is the computer currently running the thread pool.

[0009] Step 2: Adaptively adjust the number of core threads according to the system's parallel processing capability, specifically including: comparing the number of core threads obtained in step 1 with the number of logical CPUs; if the number of core threads is greater than the number of logical CPUs, modify the number of core threads to the value of the number of active threads in step 1, and execute step 3; otherwise, continue to monitor the thread pool operation.

[0010] Step 3: Analyze whether the number of threads matches the CPU load, including: Calculate the average physical CPU usage, where U irepresents the usage rate of a single CPU core, N represents the number of CPU cores, compares the average physical CPU usage rate with the maximum physical CPU usage rate, the maximum physical CPU usage rate refers to the highest value of the physical CPU usage rate that the system can tolerate, if the average physical CPU usage rate is less than the maximum physical CPU usage rate, determine whether the number of core threads is less than the number of CPUs, if so, increase the number of core threads by 1; otherwise, execute step 4; the average physical CPU usage rate is:

[0011] Average CPU usage = (ΣU i ) / N;

[0012] Step 4: Adjust the maximum capacity of the task queue according to the memory usage: Get the memory usage of the current computer, compare the memory usage with the maximum memory usage, if the memory usage is less than the maximum memory usage, check the remaining capacity of the task queue, if the capacity is 0, recalculate the maximum capacity of the task queue,

[0013] Maximum capacity of task queue = current task queue capacity * [1 + (maximum memory usage - current computer memory usage)]

[0014] Otherwise continue to monitor the operation of the thread pool.

[0015] In step 1, the number of logical CPUs is half of the total number of logical CPUs of the current computer; the computer includes a physical computer and a virtual machine.

[0016] In step 3, the maximum physical CPU usage is 80%.

[0017] In said step 4, the maximum memory usage is 80%.

[0018] The beneficial effects achieved by the present invention are:

[0019] By monitoring and comprehensively analyzing the system performance indicators of the number of logical CPUs, physical CPU usage, and memory usage during the operation of the thread pool, the task execution capability of the thread pool is judged, and the number of core threads and the maximum capacity of the task queue are automatically and flexibly adjusted in real time to achieve intelligent adjustment of the thread pool. This reduces the dependence of parameter control on the experience of technical personnel, reduces system performance instability, response time delays, or resource waste caused by improper parameter settings, and effectively improves the operating efficiency and reliability of the business system, achieving a balance between the performance of the thread pool and resource utilization. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] Figure 1 A schematic flow chart of the steps for implementing a method for intelligently adjusting a thread pool according to an embodiment of the present invention;

[0021] Figure 2 The present invention is a structural diagram of an intelligent thread pool adjustment system according to an embodiment of the present invention. DETAILED DESCRIPTION

[0022] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention is described in detail below with reference to the accompanying drawings and specific embodiments.

[0023] Thread Pool is a tool for managing threads based on the idea of ​​pooling. In a thread pool, there are always several active threads. When a thread is needed to execute a task, an idle thread is randomly extracted from the thread pool to execute the task. When the task is completed, the thread does not die, but returns to the thread pool and becomes idle again, waiting to execute the next task.

[0024] The industry generally divides the tasks processed by the thread pool into compute-intensive tasks and I / O-intensive tasks. Among them, compute-intensive tasks (CPU-intensive) involve a lot of calculations, such as complex algorithms, big data processing, etc., which require a lot of CPU computing resources, such as CPU cores. Such tasks cannot wait, and their running time depends on the CPU speed. Ideally, the CPU occupancy rate is close to 100%, but there will be no resources left to handle other important system tasks or new urgent tasks. I / O-intensive tasks involve a lot of input and output operations, such as file reading and writing, network communication or database interaction. Such tasks are often in a waiting state because they need to wait for the response of external resources. Correspondingly, the CPU will also be in an idle state. At this time, the CPU utilization rate is usually not too high, but if the CPU utilization rate is too low, the CPU resources cannot be fully utilized due to insufficient threads, resulting in poor thread performance. Different from the CPU core (physical CPU), the CPU thread (Thread) is a virtual component (that is, at the logical level, only visible to the operating system), also known as the logical CPU, logical core or logical processor, which represents the number of threads that your CPU core can support, helping the CPU to process tasks more efficiently. If the core supports hyperthreading technology, then one core can function as two cores. Each thread runs as an independent CPU instance. At the same time, the number of threads also affects memory usage. Therefore, the technicians used monitoring tools and performance analysis software to do a lot of performance analysis and testing work, and determined that based on the analysis of the number of CPUs, average CPU usage, and memory usage, they chose the right time to adaptively adjust the number of core threads and queue capacity.

[0025] like Figure 1 The implementation steps of a method for intelligently adjusting a thread pool in an embodiment of the present invention shown in the figure specifically include:

[0026] Step 101: Add an intelligent adjustment module to the thread pool client, and use the Maven tool to integrate the client Jar package into the application code; the thread pool client is any thread pool monitoring tool;

[0027] Step 102: monitor and collect thread pool operation data, and analyze thread pool operation performance, specifically including: during the thread pool operation process, the thread pool client collects and compares the size relationship between the number of active threads and the number of core threads in the thread pool. If the number of active threads is less than the number of core threads, the number of logical CPUs of the current computer is obtained, and step 103 is executed, and the number of logical CPUs is half of the total number of logical CPUs of the computer; otherwise, the average CPU usage rate of the current computer is obtained, and step 104 is executed; the computer is the computer currently running the thread pool; the computer is any one of a physical machine and a virtual machine;

[0028] In step 102, by limiting the number of core threads to no more than half of the total number of logical CPUs, negative impacts on other processes and applications of the system due to excessive use of CPU resources by applications can be avoided. Especially in a multi-tasking environment, thread creation and destruction will not be triggered frequently, which helps maintain the responsiveness and stability of the system.

[0029] Step 103: adaptively adjust the number of core threads according to the system parallel processing capability, specifically including: obtaining the number of core threads of the thread pool, and comparing the value with the number of logical CPUs in step 102; if the number of core threads is greater than the number of logical CPUs, modifying the number of core threads to the value of the number of active threads, and executing step 104; otherwise, continuing to monitor the operation of the thread pool.

[0030] Step 104: Analyze whether the number of threads matches the CPU load, specifically including: Calculate the average physical CPU usage according to the following formula 1, where U i represents the usage rate of a single CPU core, N represents the number of CPU cores, compares the average physical CPU usage rate with the maximum physical CPU usage rate, the maximum physical CPU usage rate refers to the highest value of the physical CPU usage rate that the system can tolerate, if the average physical CPU usage rate is less than the maximum physical CPU usage rate, determines whether the number of core threads is less than the number of logical CPUs, if so, increases the number of core threads by 1; otherwise, executes step 105;

[0031] Average physical CPU usage = (ΣU i ) / N (Formula 1)

[0032] The server CPUs on the market now are all multi-core. Assuming a dual-core server, the utilization rate of CPU1 is 80% and the utilization rate of CPU2 is 60%, then the average utilization rate is 70%. The utilization rate of each CPU core can be obtained through computer commands.

[0033] In step 104, the reason why the number of core threads is first compared with the number of logical CPUs is to be able to adaptively adjust the number of core threads according to the parallel processing capability of the system, so as to achieve the initial balance between thread performance and resource utilization, and ensure that under the premise of a fixed number of logical CPUs, by reasonably adjusting the number of threads, when threads process computationally intensive tasks, there will be no frequent context switching caused by preempting the CPU, and at the same time, when processing IO intensive tasks, there will be no long processing waiting time. Because the number of core threads represents the minimum number of threads running simultaneously in the thread pool. Ideally, in order to maximize the utilization of the physical CPU and avoid the overhead caused by context switching, the number of core threads should match the number of logical CPUs, so that it can better determine how to allocate threads to avoid excessive use or idleness of the CPU. After technical personnel tested and verified the impact of different numbers of threads on performance, when the number of core threads is greater than the number of logical CPUs, adjusting the number of core threads to the current number of active threads can better ensure that the performance of the thread pool and resource utilization are balanced.

[0034] In step 104, the reason for comparing the average CPU usage and the maximum CPU usage is to analyze the busyness of the CPU's current execution thread, and then determine the appropriate time to increase or decrease the number of core threads. By first comparing and then increasing the number of core threads based on the number of logical CPUs, the growth of the number of core threads is within a reasonable range, ensuring that the threads can cope with changes in the current task volume and will not cause frequent context switching due to too many threads competing for limited CPU resources, affecting system performance. After technical personnel tested and verified the impact of different numbers of threads on performance, the number of core threads is gradually increased by 1, which is convenient for carefully observing the impact of each increase on performance and avoiding problems caused by over-adjustment.

[0035] In terms of selecting the maximum physical CPU usage, technicians conducted multiple experiments and data analysis to observe the impact of different physical CPU usage on system performance and responsiveness, and found that 80% is the most appropriate maximum threshold, which can not only ensure that CPU resources are effectively utilized and improve the operating efficiency of the system, but also leave enough room to deal with possible sudden tasks or load increases, thereby ensuring the stability and reliability of the system.

[0036] Step 105: Adjust the maximum capacity of the task queue according to the memory usage: obtain the memory usage of the current computer, compare the memory usage with the maximum memory usage, if the memory usage is less than the maximum memory usage, check the remaining capacity of the task queue, if the capacity is 0, recalculate the maximum capacity of the task queue according to the following formula, otherwise continue to monitor the operation of the thread pool.

[0037] Maximum capacity of task queue = current task queue capacity * [1 + (maximum memory usage - current computer memory usage)]

[0038] In step 105, when the physical CPU usage rate exceeds the maximum CPU usage rate, it means that the system's processing capacity is close to the limit. At this time, the number of threads cannot be adjusted because increasing the number of threads will cause CPU overload, which will increase the system response time and even cause the risk of service crash. At this time, the technicians found through testing that when the memory usage rate has not reached the maximum value, the problem of task backlog can be solved by expanding the queue capacity to ensure that more tasks can be queued for execution.

[0039] In the process of selecting the maximum memory usage rate, the technical staff conducted historical data analysis, system performance testing, resource consumption assessment, simulation of high-load scenarios, stress testing, performance tuning and other methods, and determined that the maximum memory usage rate is 80%. This is because this value ensures that the system will not crash due to insufficient memory while maximizing the use of available memory resources, avoiding memory overflow and ensuring the effective use of memory, thereby improving the operating efficiency of the entire system.

[0040] The method for intelligently adjusting the thread pool in the embodiment of the present invention can be applied to the thread pool monitoring and management system, and specifically comprises: configuring a thread pool parameter adjustment algorithm in the thread pool client, integrating it into the intelligent adjustment module of the client, and using it to analyze the thread pool; the client automatically analyzes the number of logical CPUs, average utilization rate of physical CPUs, and memory utilization rate of the current system, and automatically adjusts the parameters of the thread pool; using the Maven tool to integrate the client Jar package into the code of the application program; Figure 2The system structure of intelligently adjusting the thread pool in the monitoring scenario in the embodiment of the present invention shown includes a thread pool management terminal connected to an application, wherein the application is connected to the thread pool management terminal management module through a data reporting module in the thread pool client, the management module is externally connected to a database, and the thread pool client also includes a thread pool creation module and an intelligent adjustment module; the thread pool creation module is used to initialize and configure the thread pool of the application, and the configured parameters include the thread pool name, the number of core threads, the maximum number of threads, the task queue and its length; the intelligent adjustment module is used to monitor and collect thread pool operation data, analyze the thread pool operation performance, CPU load and memory usage, and adaptively adjust the parameters of the thread pool; the thread pool management terminal also includes a display module, which is used to count and visualize the data of the thread pool parameters. The thread pool client is used to create, monitor the thread pool and adaptively adjust the parameters of the thread pool, and the thread pool management terminal is configured as a visual web graphical interface, which is called by the client through an exposed interface and receives the thread pool operation data reported by the client; the database is used to store thread pool parameters.

[0041] The present invention monitors and comprehensively analyzes system performance indicators such as the number of logical CPUs, physical CPU usage, and memory usage during the operation of the thread pool, determines the task execution capability of the thread pool, and then automatically and flexibly adjusts the number of core threads and the maximum capacity of the task queue in real time, thereby realizing intelligent adjustment of the thread pool, reducing the dependence of parameter control on the experience of technicians, and reducing problems such as unstable system performance, delayed response time, or waste of resources caused by improper parameter settings, thereby effectively improving the operating efficiency and reliability of the business system and achieving a balance between the performance of the thread pool and the utilization of resources.

[0042] The present invention may also have many other implementation modes. The above embodiments do not limit the present invention in any way. Without departing from the spirit and essence of the present invention, technicians familiar with the field may make various corresponding changes and modifications based on the present invention. All other improvements and applications made to the above embodiments in an equivalent transformation manner should fall within the scope of protection of the claims attached to the present invention.

Claims

1. A method for intelligently adjusting a thread pool, characterized in that: include: Step 1: During the operation of the thread pool, the number of active threads and the number of core threads of the thread pool are collected and compared. If the number of active threads is less than the number of core threads, the number of logical CPUs of the current computer is obtained and step 2 is executed. Otherwise, the average utilization rate of the physical CPU of the current computer is obtained and step 3 is executed. The computer is the computer currently running the thread pool. Step 2: Adaptively adjust the number of core threads according to the system's parallel processing capability, specifically including: comparing the number of core threads obtained in step 1 with the number of logical CPUs; if the number of core threads is greater than the number of logical CPUs, modify the number of core threads to the value of the number of active threads in step 1, and execute step 3; otherwise, continue to monitor the thread pool operation. Step 3: Analyze whether the number of threads matches the CPU load, including: Calculate the average physical CPU usage, where U i represents the usage rate of a single CPU core, N represents the number of CPU cores, compares the average physical CPU usage rate with the maximum physical CPU usage rate, the maximum physical CPU usage rate refers to the highest value of the physical CPU usage rate that the system can tolerate, if the average physical CPU usage rate is less than the maximum physical CPU usage rate, determines whether the number of core threads is less than the number of CPUs, if so, increases the number of core threads by 1; Otherwise, execute step 4; the average physical CPU usage is: Average CPU usage = (ΣU i ) / N; Step 4: Adjust the maximum capacity of the task queue according to the memory usage: Get the memory usage of the current computer, compare the memory usage with the maximum memory usage, if the memory usage is less than the maximum memory usage, check the remaining capacity of the task queue, if the capacity is 0, recalculate the maximum capacity of the task queue, The maximum capacity of the task queue = the current task queue capacity * [1 + (maximum memory usage - current computer memory usage)] otherwise continue to monitor the operation of the thread pool.

2. A method for intelligently adjusting a thread pool as claimed in claim 1, characterized in that: In step 1, the number of logical CPUs is half of the total number of logical CPUs of the current computer; the computer includes a physical computer and a virtual machine.

3. A method for intelligently adjusting a thread pool as described in claim 2, characterized in that: In step 3, the maximum physical CPU usage is 80%.

4. A method for intelligently adjusting a thread pool as claimed in claim 3, characterized in that: In said step 4, the maximum memory usage is 80%.

Citation Information

Patent Citations

  • Thread pool parameter processing method and system, computer equipment, medium and program product

    CN115328625A

Cited By

  • High-performance and easy-to-expand unified search method based on Spring

    CN122387604A