Thread pool switching method and device, electronic equipment and storage medium
By dynamically determining the thread pool switching method and selecting a suitable thread pool based on system status data, the problem of thread pool selection being unable to adapt to changes in the business system is solved, thereby improving system throughput and resource utilization.
Patent Information
- Application Number
- CN202511646119.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-11
- Publication Date
- 2026-02-27
AI Technical Summary
In existing technologies, the pre-selection of thread pools cannot adapt to changes in the operating state of business systems, resulting in reduced throughput in high-concurrency scenarios. Traditional thread pools underutilize resources when I/O operations are frequent, while virtual thread pools may exacerbate resource contention when resources are limited, thus failing to effectively improve system performance.
By acquiring system status data from the business system, it dynamically determines whether to switch to a preset thread pool or a virtual thread pool, including CPU utilization, number of idle database connections, length of the virtual thread carrier pool queue, and number of database connection waiting queues, and generates corresponding thread pool decision instructions to execute database operations.
This improves system throughput, ensures the thread pool can adapt to the current operating state, enhances response speed and resource utilization, and reduces latency and failure risk.
Smart Images

Figure CN121579153A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database operation technology, and in particular to a thread pool switching method, apparatus, electronic device and storage medium. Background Technology
[0002] In the business system architecture of software applications, the operation execution system, database connection pool, and thread pool form a hierarchical collaborative relationship to jointly support the system performance and resource management of the business system under high concurrency scenarios. The operation execution system (such as a web server or microservice framework) is responsible for receiving external requests and coordinating resource processing tasks, using the thread pool to implement task scheduling. As the underlying engine of the operation execution system, the thread pool is responsible for reusing threads, controlling concurrency, and executing specific tasks. When a thread performs database operations, the database connection pool provides pre-established database connections for reuse.
[0003] The choice of thread pool is pre-selected during the business system architecture phase; it can be either virtual threads or traditional thread pools. However, when using a traditional thread pool, if the system is in a state of frequent I / O (Input / Output) operations, its high overhead may prevent the full utilization of the business system's resources, thus reducing the system's throughput and limiting the software's concurrency and response speed. While using virtual threads can significantly improve system throughput when resources are plentiful, when resources are limited, creating an unlimited number of virtual threads to execute database calls may not only fail to improve throughput but could also lead to a decrease in throughput due to increased resource contention. Therefore, when the business system's operating state changes, the pre-selected thread pool cannot adapt well to these changes, thus reducing system throughput. Summary of the Invention
[0004] The purpose of this application is to provide a thread pool switching method, apparatus, electronic device, and storage medium to address how to improve system throughput. The specific technical solution is as follows:
[0005] In a first aspect of this application, a thread pool switching method is provided, the method comprising:
[0006] Upon receiving a database operation request, system status data representing the operating status of the business system is obtained; wherein, the system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues;
[0007] The system status data is used to determine whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool.
[0008] If so, then a thread pool decision instruction is generated for the preset thread pool, and the preset thread in the preset thread pool is invoked to execute the database operation;
[0009] If not, then a thread pool decision instruction is generated for the virtual thread carrier pool, which calls the virtual threads in the virtual thread carrier pool to execute the database operation.
[0010] In one possible implementation, when the resource utilization rate of the device hardware is greater than a preset value, the throughput of the business system corresponding to the preset thread is greater than the throughput of the business system corresponding to the virtual thread.
[0011] In one possible implementation, determining whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool based on the system status data includes:
[0012] If the CPU utilization rate is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the number of database connection waiting queues is greater than a third preset threshold, or the number of idle database connections is less than a fourth preset threshold,
[0013] The thread pool required for the database operation is determined to be the preset thread pool.
[0014] In one possible implementation, the method further includes:
[0015] Collect system status data within a preset historical period;
[0016] Based on the system status data within the preset historical period, a first time range in which the business system is in a preset state is determined; wherein, the preset state is the operating state of the business system when the resource utilization rate of the device hardware is greater than a preset value;
[0017] When the current time falls within the first time range, the parameters of the preset thread pool are configured as the first parameter; wherein, the parameters of the preset thread pool include the maximum number of threads;
[0018] When the current time is outside the first time range, the parameters of the preset thread pool are configured as the second parameter; wherein the maximum number of threads in the second parameter is greater than the maximum number of threads in the first parameter.
[0019] In one possible implementation, the method further includes:
[0020] If the CPU utilization is less than a fifth preset threshold, the number of idle database connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold, then the thread pool required for the database operation is determined to be the virtual thread carrier pool.
[0021] In a second aspect of this application, a thread pool switching device is provided, the device comprising:
[0022] The system status data acquisition module is used to acquire system status data representing the operating status of the business system after receiving a database operation request; wherein, the system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues;
[0023] The preset thread pool determination module is used to determine, based on the system status data, whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool.
[0024] If so, then a thread pool decision instruction is generated for the preset thread pool, and the preset thread in the preset thread pool is invoked to execute the database operation;
[0025] If not, then a thread pool decision instruction is generated for the virtual thread carrier pool, which calls the virtual threads in the virtual thread carrier pool to execute the database operation.
[0026] In one possible implementation, when the resource utilization rate of the device hardware is greater than a preset value, the throughput of the business system corresponding to the preset thread is greater than the throughput of the business system corresponding to the virtual thread.
[0027] In one possible implementation, the preset thread pool determination module includes:
[0028] The preset thread pool judgment submodule is specifically used to determine if the CPU utilization is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the number of database connection waiting queues is greater than a third preset threshold, or the number of idle database connections is less than a fourth preset threshold.
[0029] The thread pool required for the database operation is determined to be the preset thread pool.
[0030] In one possible implementation, the device further includes:
[0031] The system status data acquisition module is used to collect the system status data within a preset historical period;
[0032] The first time range determination module is used to determine the first time range in which the business system is in a preset state based on the system state data within the preset historical period; wherein, the preset state is the operating state of the business system when the resource utilization rate of the device hardware is greater than a preset value;
[0033] The first parameter configuration module is used to configure the parameters of the preset thread pool as the first parameter when the current time is within the first time range; wherein, the parameters of the preset thread pool include the maximum number of threads;
[0034] The second parameter configuration module is used to configure the parameters of the preset thread pool as a second parameter when the current time is outside the first time range; wherein the maximum number of threads in the second parameter is greater than the maximum number of threads in the first parameter.
[0035] In one possible implementation, the device further includes:
[0036] The virtual thread carrier pool determination module is used to determine the thread pool required for the database operation as the virtual thread carrier pool if the CPU utilization is less than a fifth preset threshold, the number of idle database connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold.
[0037] In a third aspect of the embodiments of this application, an electronic device is provided, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0038] Memory, used to store computer programs;
[0039] When a processor executes a program stored in memory, it implements the steps of the method described in any of the first aspects of the embodiments of this application.
[0040] In another aspect of this application, a computer-readable storage medium is provided, wherein a computer program is stored therein, and the computer program, when executed by a processor, implements any of the thread pool switching methods described above.
[0041] In another aspect of this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the thread pool switching methods described above.
[0042] This application provides a thread pool switching method, apparatus, electronic device, and storage medium. Upon receiving a database operation request, it acquires system status data representing the operational status of the business system. This system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues. The system status data is used to determine whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool. If so, a thread pool decision instruction is generated for the preset thread pool, invoking a preset thread in the preset thread pool to execute the database operation. If not, a thread pool decision instruction is generated for the virtual thread carrier pool, invoking a virtual thread in the virtual thread carrier pool to execute the database operation.
[0043] By applying the method of this application embodiment, system status data can be collected, and the system status data can be used to determine whether the thread pool required to perform database operations is a preset thread pool (i.e., a traditional thread pool). This allows for dynamic determination of a suitable thread pool based on the operating status of the business system, thereby improving the system throughput. Attached Figure Description
[0044] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0045] Figure 1 A flowchart of a thread pool switching method provided in an embodiment of this application;
[0046] Figure 2 A flowchart detailing step S102 is provided for an embodiment of this application.
[0047] Figure 3 A flowchart illustrating thread pool parameter configuration provided in an embodiment of this application;
[0048] Figure 4 A schematic diagram of a thread pool switching system provided in an embodiment of this application;
[0049] Figure 5 A schematic diagram of a thread pool switching method provided in an embodiment of this application;
[0050] Figure 6 A schematic diagram of a thread pool switching device provided in an embodiment of this application;
[0051] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0052] The technical solutions in the embodiments of this application will now be described with reference to the accompanying drawings.
[0053] In the business system architecture of software applications, the operation execution system, database connection pool, and thread pool form a hierarchical collaborative relationship to jointly support the system performance and resource management of the business system under high concurrency scenarios. The operation execution system (such as a web server or microservice framework) is responsible for receiving external requests and coordinating resource processing tasks, using the thread pool to implement task scheduling. As the underlying engine of the operation execution system, the thread pool is responsible for reusing threads, controlling concurrency, and executing specific tasks. When a thread performs database operations, the database connection pool provides pre-established database connections for reuse.
[0054] The choice of thread pool can be either virtual threads or traditional thread pools. However, in related technologies, the choice of thread pool is predetermined at the initial stage of business system architecture. In order to reduce the complexity of the system architecture and facilitate unified management of threads and task queues and control of system resources, those skilled in the art usually choose a single thread pool for system construction during the system architecture process.
[0055] However, while virtual threads can significantly improve system throughput, creating an unlimited number of virtual threads to execute database calls may not only fail to improve performance when business system resources are limited, but may also lead to decreased system performance due to increased resource contention. Conversely, traditional thread pools, under I / O-intensive loads, may fail to fully utilize the business system's resources due to their high overhead, limiting the software's concurrency and response speed. Therefore, when the business system's operating state changes, the pre-selected thread pool may not adapt well to these changes, thereby reducing the system's throughput and impacting its performance.
[0056] To address at least one of the aforementioned problems, a first aspect of this application provides a thread pool switching method. Upon receiving a database operation request, the method acquires system status data representing the operational status of a business system. The system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues. The method uses the system status data to determine whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool. If so, a thread pool decision instruction is generated for the preset thread pool, invoking a preset thread in the preset thread pool to execute the database operation. If not, a thread pool decision instruction is generated for the virtual thread carrier pool, invoking a virtual thread in the virtual thread carrier pool to execute the database operation.
[0057] By applying the method of this application embodiment, system status data can be collected, and the system status data can be used to determine whether the thread pool required to perform database operations is a preset thread pool (i.e., a traditional thread pool). This allows for dynamic determination of a suitable thread pool based on the operating status of the business system, thereby improving the system throughput.
[0058] The following embodiments will specifically illustrate the thread pool switching method of this application. In one example, the method includes: Figure 1 The steps shown are as follows:
[0059] Step S101: After receiving the database operation request, obtain system status data representing the operating status of the business system.
[0060] When acquiring system status data, it can be done periodically or in real-time by using the system's built-in monitoring modules or interfaces. For example, system status data can be obtained periodically through JMX MBeans (Manageable Resource Object Model in Java applications) and operating system-level APIs (Application Programming Interfaces). Another example is using threshold-triggered monitoring modules to acquire system status data. For instance, when system status data exceeds a preset threshold, a monitoring mode is triggered, and the monitoring module in the business system begins monitoring and recording the system status data.
[0061] The business system is a system architecture built according to actual business needs to implement the business. This system can include a server and a client. In the initial stage of this business system architecture, it is configured with both a virtual thread carrier pool and a preset thread pool, which is the traditional thread pool mentioned above. When the resource utilization of the device hardware exceeds a preset value, the throughput of the business system corresponding to the preset thread is greater than the throughput of the business system corresponding to the virtual thread. When the resource utilization of the device hardware exceeds the preset value, the system can be considered to be operating under high pressure. Under high pressure, the system throughput when using the preset thread to perform database operations is greater than the system throughput when using the virtual thread to perform database operations.
[0062] In a virtual thread pool, multiple virtual threads share the stack memory of a single carrier thread, which is the thread that actually performs database operations. When a virtual thread is invoked, it does not execute immediately but waits for the JVM (Java Virtual Machine) scheduler to assign it to an available carrier thread. However, under high system load, such as when the CPU (Central Processing Unit) is heavily loaded, a large number of virtual threads compete for the limited number of carrier threads. The JVM scheduler then needs to frequently switch threads, resulting in significant context switching overhead, reduced system throughput, and impacted system speed.
[0063] In practical applications, the method of this application embodiment is applied to a terminal device, which may be an electronic device such as a computer, server, or tablet computer capable of carrying a business system.
[0064] The resource utilization of the device hardware can be reflected through system status data, which includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues.
[0065] CPU utilization refers to the percentage of time the CPU spends executing instructions (non-idle state) within a given time period. It represents the proportion of CPU resources used by running programs and reflects the CPU's "busyness." For example, a CPU utilization of 50% means that the system's hardware CPU is processing tasks half the time and idle for the other half. A CPU utilization of 100% indicates that the CPU is working at full capacity throughout the entire process, with no idle time.
[0066] The number of idle database connections refers to the number of established but unused database connections in the database connection pool. This pool is used to quickly respond to new requests and avoid the overhead of frequently creating connections. When the number of idle connections is too large, it consumes system resources such as memory and threads, potentially leading to decreased system performance or resource contention. Conversely, when the number of idle connections is too small, new requests must wait for connection creation or release, increasing response time and reducing database response speed. A connection pool is a technology component that pre-creates and manages database connections, significantly improving system performance and optimizing resource management by reusing existing connections. When an application starts, the database connection pool automatically initializes a certain number of database connections (called idle connections) and stores them in a buffer pool. When an application needs to operate on the database, it directly obtains an idle connection from this connection pool (without re-establishing one). After the operation is completed, the connection is returned to the connection pool instead of being physically closed, for subsequent reuse.
[0067] The queue length of the virtual thread carrier pool refers to the upper limit of the task buffer queue in the bound platform thread pool, used to temporarily store virtual thread tasks waiting for carrier thread scheduling. When the queue length of the virtual thread carrier pool is too large, the system can be considered to be under high pressure, the carrier thread is at risk of overload, the execution of virtual threads will be delayed, thus affecting the task response speed.
[0068] The database connection waiting queue number refers to the number of requests queuing for available connections when the number of database connection requests exceeds the maximum capacity of the connection pool. This data reflects the resource contention for database connections in high-concurrency scenarios; a larger value indicates a higher system load and slower database connection processing speed. In this embodiment, the business system architecture includes both a virtual thread pool and a preset thread pool. The preset thread pool can be configured using methods similar to those for traditional thread pools in related technologies.
[0069] Step S102: Determine whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool based on the system status data.
[0070] When determining whether the thread pool required for database operations is the preset thread pool, system status data can be used to judge the current system running state, thereby determining whether the current running state is suitable for calling the preset thread pool. In one example, when the number of database connection waiting queues is large, indicating that the system is currently under high load, it can be determined to use the preset thread pool (i.e., the traditional thread pool) to perform database operations, so as to utilize the limited queue of the preset thread pool to buffer the database connection pressure.
[0071] If so, then proceed to step S103: generate a thread pool decision instruction for the preset thread pool, and call the preset thread in the preset thread pool to execute the database operation.
[0072] If not, proceed to step S104: generate a thread pool decision instruction for the virtual thread carrier pool, and call the virtual threads in the virtual thread carrier pool to execute the database operation.
[0073] After determining the thread pool required to perform database operations, a thread pool decision instruction is generated indicating the use of that thread pool. In one example, this thread pool decision instruction may include a category label representing the thread pool required to perform the database operation. This category label can be represented by characters, numbers, letters, etc. For example, "0" can indicate using a preset thread from a preset thread pool to perform the database operation, and "1" can indicate using a virtual thread from a virtual thread carrier pool to perform the database operation.
[0074] When invoking a virtual thread from a pre-defined thread pool or virtual thread carrier pool to perform database operations, you can use pre-defined commands. For example, you can use `Thread.startVirtualThread()` or `Executors.newVirtualThreadPerTaskExecutor()` to invoke the virtual thread carrier pool to create a virtual thread to perform database operations. In another example, you can use `DPTP.submit.databaseTask` to invoke the pre-defined thread pool to perform database operations.
[0075] The method described in this application can collect system status data and use this data to determine whether the thread pool required for database operations is a preset thread pool (i.e., a traditional thread pool). This allows for dynamic selection of a suitable thread pool based on the operational status of the business system, enabling threads performing database operations to adapt to the current system state. Furthermore, the system can automatically switch to the most suitable thread pool based on its load, thereby improving system throughput. In addition, because the threads in the business system of this application are better adapted to the system's operating environment, they can maximize throughput and minimize latency while ensuring system stability, resulting in faster system response, stronger processing capabilities, and further enhanced user experience. Compared to continuously creating platform threads, this application intelligently selects threads based on system status data, further improving device resource utilization and saving server costs.
[0076] In one possible implementation, step S102 can be performed as follows: Figure 2 The steps shown are to be implemented as follows:
[0077] Step S201: Determine whether the central processing unit utilization rate is greater than the first preset threshold.
[0078] Alternatively, step S202: Determine whether the queue length of the virtual thread carrier pool is greater than the second preset threshold.
[0079] Alternatively, step S203: Determine whether the number of database connection waiting queues is greater than the third preset threshold.
[0080] Alternatively, step S204: Determine whether the number of idle database connections is less than the fourth preset threshold.
[0081] If the CPU utilization is greater than the first preset threshold, or the queue length of the virtual thread carrier pool is greater than the second preset threshold, or the number of database connection waiting queues is greater than the third preset threshold, or the number of database idle connections is less than the fourth preset threshold, then the thread pool required for database operations is determined to be the preset thread pool.
[0082] The first, second, third, and fourth preset thresholds can be set according to actual application needs. For example, in practical applications, the system will perform different operations based on the content of each type of data. For instance, when CPU utilization exceeds 50%, the system begins monitoring the data; when CPU utilization exceeds 80%, the system begins to alert the user; when CPU utilization exceeds 95%, it indicates that the system is under high load and faces the risk of crashing and severe delays. Therefore, the first preset threshold can be determined according to the aforementioned preset values. For example, when the CPU utilization exceeds T_cpu_critical, the system will call a preset thread from the preset thread pool to perform database operations. Here, T_cpu_critical is the first preset threshold, which represents the threshold at which the system is under extremely high load; exceeding this value may result in system crashes or severe delays.
[0083] In another example, when the queue length of the virtual thread carrier pool is greater than T_vtq_high, the threads required for database operations are determined to be from the preset thread pool. Here, T_vtq_high is a second preset threshold, representing the system's high alert threshold corresponding to the queue length of the virtual thread carrier pool. In practical applications, when the queue length of the virtual thread carrier pool exceeds this threshold, the system will issue a high-level warning to the user, indicating that the system is under high pressure, such as a CPU-intensive state. In this case, using the preset thread pool will result in higher system throughput than using the virtual thread pool. Therefore, when the queue length of the virtual thread carrier pool is greater than T_vtq_high, the threads required for database operations are confirmed to be from the preset thread pool. This avoids increased system CPU load and also alleviates the pressure on the carrier threads in the virtual thread carrier pool.
[0084] In another example, when the number of database connection waiting queues exceeds T_dbq_high, the threads required for database operations are determined to be from the preset thread pool. Here, T_dbq_high is a third preset threshold, which can represent a high-risk latency threshold indicating the number of database connection waiting queues has reached. When the number of pending requests in the database task queue exceeds the value set by T_dbq_high, it indicates that the system faces a serious risk of request backlog, which may cause response delays or service blocking. In this case, selecting preset threads from the preset thread pool to perform database operations can buffer database connection contention through the limited queue and rejection policy of the preset thread pool.
[0085] In another example, when the number of idle database connections is less than T_db_low, the threads required for database operations are determined to be from the preset thread pool. Here, T_db_low is the fourth preset threshold, which can represent a shortage of idle database connections or a low-load system. Similarly, when the number of idle database connections is less than the fourth preset threshold, it indicates that the system is experiencing intense competition for database connections, resulting in high system load and slow database connection processing. In this case, using the preset thread pool can utilize its limited queue to buffer competition for scarce database connections, preventing a cascading failure.
[0086] Applying the method of this application embodiment, when the CPU utilization rate is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, the system can avoid exacerbating CPU load or reducing the pressure on carrier threads under high load conditions, preventing system overload or crash. It determines to use a preset thread from the preset thread pool to execute database operations, thereby improving system throughput and reducing downtime and losses. When the number of database connection waiting queues is greater than a third preset threshold, or the number of idle database connections is less than a fourth preset threshold, when system resources are insufficient, it determines to use a preset thread from the preset thread pool to execute database operations, avoiding virtual threads from exacerbating system resource consumption, thereby improving system throughput. Furthermore, by collecting data from multiple dimensions for thread judgment, the system's judgment can be more comprehensive and accurate, avoiding misjudgments caused by fluctuations in a single data indicator, and improving decision quality. Moreover, by controlling the number of idle database connections, it can prevent system avalanches caused by connection pool exhaustion while improving the utilization efficiency of the connection pool and reducing connection acquisition waiting time and timeout rate.
[0087] In one possible implementation, the method of this application embodiment may further include, for example: Figure 3 The steps shown are as follows:
[0088] Step S301: Collect the system status data within a preset historical period.
[0089] Step S302: Determine the first time range in which the business system is in a preset state based on the system status data within the preset historical period.
[0090] Step S303: When the current time is within the first time range, configure the parameters of the preset thread pool as the first parameter.
[0091] Step S304: When the current time is outside the first time range, configure the parameters of the preset thread pool to the second parameter.
[0092] In this embodiment, the parameters of the preset thread pool may include, but are not limited to, the maximum number of threads, the work queue (such as a bounded queue LinkedBlockingQueue), the rejection policy, the core thread count, and the queue capacity. The maximum number of threads refers to the maximum number of threads allowed in the preset thread pool. When the work queue is full and the number of created threads is less than the maximum number of threads, the thread pool will create new threads to handle tasks. This parameter prevents unlimited thread creation from exhausting CPU or memory resources. The work queue is used to store tasks to be executed.
[0093] When the thread pool cannot process new tasks (when the queue is full and the number of threads reaches the maximum number of threads), a rejection policy will be triggered. The rejection policy may include (1) AbortPolicy (default policy): when a task cannot be executed, an exception alarm message is thrown directly; (2) CallerRunsPolicy (execution by calling thread policy): the task is executed by the thread that calls the execute method; (3) DiscardPolicy (discard new task policy): the rejected task is discarded directly without any processing; (4) DiscardOldestPolicy (discard oldest task policy): the oldest task in the queue is discarded, and then a new task is tried to be added to the queue.
[0094] The core thread count is the minimum number of threads that the thread pool will maintain long-term; these threads will not be destroyed even when idle. Its main purpose is to avoid the overhead of frequently creating threads when tasks surge, thus preserving core computing power. The queue capacity is the upper limit of the task queue's capacity, restricting the number of tasks that can be stored in the queue and preventing system memory exhaustion.
[0095] The parameters mentioned above can be preset or adjusted according to the operating status of the business system. For example, system status data within a preset historical period can be collected first to determine the time range (i.e., the first time range) when the business system is in a preset state. The preset historical period can be set according to actual application needs; for example, the preset historical period can be a historical period of one week, one month, one quarter, or one year.
[0096] The preset state can represent conditions such as system operation under high pressure or insufficient system resources, which can be characterized by the resource utilization of device hardware. For example, it can be determined whether the system is under high pressure by at least one of the system status data. In one example, if the CPU utilization is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the number of database connection waiting queues is greater than a third preset threshold, or the number of idle database connections is less than a fourth preset threshold, then the system can be considered to be operating in a preset state, and the parameters of the preset thread pool can be modified to adapt to the high-load operating state of the system.
[0097] In another example, analysis of system status data within a preset historical period reveals that the system's CPU utilization rate is higher than the first preset threshold between 20:00 and 22:00 each day. Therefore, it can be concluded that the system is under high pressure during the period between 20:00 and 22:00 each day.
[0098] When configuring the parameters of the preset thread pool, the parameters can be set to those used when the system is under high pressure (preset state) (first parameter) whenever the current time reaches the range of 20:00 to 22:00 (i.e., the first time range); otherwise, the parameters can be set to the second parameter. When the system is under high pressure, the maximum number of threads in the preset thread pool can be appropriately reduced to prevent an explosive increase in the number of database connections, thereby protecting downstream database resources. Therefore, the maximum number of threads in the second parameter can be set to be greater than the maximum number of threads in the first parameter.
[0099] By applying the method of this application embodiment, the system can determine the first time range when the system is in a preset state by using system state data within a preset historical period. Then, when the current time reaches the first time range, the parameters of the preset thread pool are dynamically set to the first parameter so that it can better adapt to changes in the system's operating state, thereby improving the system's throughput.
[0100] In one possible implementation, the method of this application embodiment may further include the following steps:
[0101] If the CPU utilization is less than a fifth preset threshold, the number of idle database connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold, then the thread pool required for the database operation is determined to be the virtual thread carrier pool.
[0102] Similar to the embodiments described above, the fifth, sixth, and seventh preset thresholds in this application embodiment can also be set according to actual application requirements. In one example, when the CPU utilization is less than T_cpu_high, the number of idle database connections is greater than T_db_idle, and the queue length of the virtual thread carrier pool is less than T_vtq_mod, the thread pool required for the database operation is determined to be the virtual thread carrier pool.
[0103] Among them, T_cpu_high is the fifth preset threshold, which can be a relatively high threshold for CPU utilization, used to trigger warning-level alerts. When CPU utilization reaches this threshold, the system will alert the user. T_db_idle is the sixth preset threshold, which can be a threshold indicating that the database is currently in an inactive state. T_vtq_mod is the seventh preset threshold, which can be a moderate alarm threshold for the system, less than T_vtq_high.
[0104] Applying the method of this application embodiment, when the CPU utilization is less than a fifth preset threshold, the number of idle database connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold, the system resources are considered relatively sufficient. When system resources are sufficient, the system throughput using virtual threads is higher than that using preset threads. Therefore, the thread pool required for the database operation can be determined to be the virtual thread carrier pool. This allows for rapid response to database operations using virtual threads under sufficient system resource conditions, increasing both system throughput and I / O response speed. Furthermore, the system of this application embodiment includes both a preset thread pool and a virtual carrier thread pool. During system operation, the advantages and disadvantages of both can be comprehensively considered to maximize strengths and minimize weaknesses, balancing system performance and stability. This ensures both increased system throughput and robustness under pressure.
[0105] like Figure 4The diagram illustrates the system framework of the business system to which the method of this application embodiment is applied. This system includes a real-time status monitoring module, a dynamic switching decision module, a task execution scheduling module, and an execution unit module. The real-time status monitoring module can be used to obtain data such as system load, database connection pool status, and virtual thread carrier pool status from monitoring data sources. In one example, the real-time status monitoring module obtains data including, but not limited to, data representing system load (e.g., CPU utilization, memory usage), data representing I / O wait status (e.g., average system I / O wait time, database operation I / O response time), data representing database connection pool status (e.g., number of active / idle connections, wait queue length, connection acquisition wait time, and timeout rate), data representing virtual thread carrier pool status (e.g., number of active platform threads, run queue length, and saturation indicators), and data representing optional inputs (e.g., characteristics of the task to be processed, such as read / write type and complexity).
[0106] The real-time status monitoring module sends the acquired data as real-time system status indicators to the dynamic switching decision module. The dynamic switching decision module determines whether to use a preset thread or a virtual thread to execute the database operation based on at least one of the received data points. For example, if the CPU utilization exceeds a first preset threshold, or the queue length of the virtual thread pool exceeds a second preset threshold, or the number of database connection waiting queues exceeds a third preset threshold, or the number of idle database connections is less than a fourth preset threshold, the dynamic switching decision module confirms that a preset thread from the preset thread pool will be used to execute the database operation. After determining the type of thread for executing the database operation, the dynamic switching decision module generates a decision instruction and sends it to the task execution scheduling module.
[0107] The task execution scheduling module can determine the type of thread required to perform database operations based on decision instructions, and then invoke the corresponding execution unit to perform the database operations. When a task is submitted to the task execution scheduling module, if the decision instruction indicates that a preset thread from a preset thread pool should be used to perform the database operations, then the preset thread from the preset thread pool will be invoked to perform the database operations and execute the task. Similarly, if the decision instruction indicates that a virtual thread should be used to perform the database operations, then a virtual thread from a virtual thread carrier pool will be invoked to perform the database operations and execute the task.
[0108] Combination Figure 5This section describes the thread pool switching method used in this application. First, the application / client creates a task to be executed and sends a database operation request (i.e., submitting a database operation) to the server. Upon receiving the database operation request, the task execution scheduling module sends a request decision instruction to the dynamic switching decision module. Upon receiving the request decision instruction, the dynamic switching decision module begins collecting system status data, using this data to determine which thread to use for the database operation, and then generates a decision instruction.
[0109] Specifically, the dynamic switching decision module can send a request to the real-time status monitoring module to obtain real-time system status data. The real-time status monitoring module acquires various data in the execution environment, generates status indicators, and returns them to the dynamic switching decision module. The dynamic switching decision module then uses decision logic based on at least one of these data points to determine whether the execution unit for the database operation is a preset thread pool or a virtual thread. This decision logic is pre-set; for example, if the CPU utilization exceeds a first preset threshold, or the queue length of the virtual thread pool exceeds a second preset threshold, or the number of database connection waiting queues exceeds a third preset threshold, or the number of idle database connections is less than a fourth preset threshold, the dynamic switching decision module confirms that a preset thread from the preset thread pool will be used to execute the database operation.
[0110] After receiving a decision instruction, the task execution scheduling module selects the appropriate thread to execute the database operation based on the thread type specified in the instruction. For example, according to... Figure 5 In step 7, select a virtual thread to perform database operations, or follow step 8 to select a preset thread to perform database operations.
[0111] By applying the method of the embodiments of this application, system status data is collected, and the system status data is used to determine whether the thread pool required to perform database operations is a preset thread pool (i.e., a traditional thread pool). In this way, the appropriate thread pool can be dynamically determined according to the running status of the business system, thereby improving the system throughput.
[0112] In a second aspect of this application, a thread pool switching device is provided, the device comprising: Figure 6 The structure shown is as follows:
[0113] The system status data acquisition module 601 is used to acquire system status data representing the operating status of the business system after receiving a database operation request; wherein, the system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues;
[0114] The preset thread pool determination module 602 is used to determine, through the system status data, whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool.
[0115] If so, then a thread pool decision instruction is generated for the preset thread pool, and the preset thread in the preset thread pool is invoked to execute the database operation;
[0116] If not, then a thread pool decision instruction is generated for the virtual thread carrier pool, which calls the virtual threads in the virtual thread carrier pool to execute the database operation.
[0117] In one possible implementation, when the resource utilization rate of the device hardware is greater than a preset value, the throughput of the business system corresponding to the preset thread is greater than the throughput of the business system corresponding to the virtual thread.
[0118] In one possible implementation, the preset thread pool determination module includes:
[0119] The preset thread pool judgment submodule is specifically used to determine if the CPU utilization is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the number of database connection waiting queues is greater than a third preset threshold, or the number of idle database connections is less than a fourth preset threshold.
[0120] The thread pool required for the database operation is determined to be the preset thread pool.
[0121] In one possible implementation, the device further includes:
[0122] The system status data acquisition module is used to collect the system status data within a preset historical period;
[0123] The first time range determination module is used to determine the first time range in which the business system is in a preset state based on the system state data within the preset historical period; wherein, the preset state is the operating state of the business system when the resource utilization rate of the device hardware is greater than a preset value;
[0124] The first parameter configuration module is used to configure the parameters of the preset thread pool as the first parameter when the current time is within the first time range; wherein, the parameters of the preset thread pool include the maximum number of threads;
[0125] The second parameter configuration module is used to configure the parameters of the preset thread pool as a second parameter when the current time is outside the first time range; wherein the maximum number of threads in the second parameter is greater than the maximum number of threads in the first parameter.
[0126] In one possible implementation, the device further includes:
[0127] The virtual thread carrier pool determination module is used to determine the thread pool required for the database operation as the virtual thread carrier pool if the CPU utilization is less than a fifth preset threshold, the number of idle database connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold.
[0128] This application also provides an electronic device, such as... Figure 7 As shown, it includes a processor 701, a communication interface 702, a memory 703, and a communication bus 704, wherein the processor 701, the communication interface 702, and the memory 703 communicate with each other through the communication bus 704.
[0129] Memory 703 is used to store computer programs;
[0130] When processor 701 executes a program stored in memory 703, it performs the following steps:
[0131] Upon receiving a database operation request, system status data representing the operating status of the business system is obtained; wherein, the system status data includes at least one of the following: CPU utilization of the device hardware, number of idle database connections, queue length of the virtual thread carrier pool, and number of database connection waiting queues;
[0132] The system status data is used to determine whether the thread pool required to execute the database operation corresponding to the database operation request is a preset thread pool.
[0133] If so, then a thread pool decision instruction is generated for the preset thread pool, and the preset thread in the preset thread pool is invoked to execute the database operation;
[0134] If not, then a thread pool decision instruction is generated for the virtual thread carrier pool, which calls the virtual threads in the virtual thread carrier pool to execute the database operation.
[0135] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0136] The communication interface is used for communication between the aforementioned terminal and other devices.
[0137] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0138] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0139] In another embodiment provided in this application, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the thread pool switching methods described in the above embodiments.
[0140] In another embodiment provided in this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the thread pool switching methods described in the above embodiments.
[0141] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0142] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0143] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, electronic devices, and computer-readable storage media are basically similar to the method embodiments, and therefore the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0144] The above description is merely a preferred embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application are included within the scope of protection of this application.
Claims
1. A thread pool switching method, characterized in that, The method comprises: After receiving a database operation request, system state data representing a running state of a business system is acquired; wherein the system state data comprises at least one of a central processing unit utilization rate of device hardware, a database idle connection number, a queue length of a virtual thread carrier pool, and a database connection waiting queue number; It is determined, through the system state data, whether a thread pool required for executing a database operation corresponding to the database operation request is a preset thread pool; If yes, a thread pool decision instruction for the preset thread pool is generated, and a preset thread in the preset thread pool is called to execute the database operation; If no, a thread pool decision instruction for a virtual thread carrier pool is generated, and a virtual thread in the virtual thread carrier pool is called to execute the database operation.
2. The method of claim 1, wherein, When the resource usage rate of the device hardware is greater than a preset value, the throughput of the business system corresponding to the preset thread is greater than the throughput of the business system corresponding to the virtual thread.
3. The method of claim 1, wherein, The determination, through the system state data, of whether the thread pool required for executing the database operation is the preset thread pool comprises: If the central processing unit utilization rate is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the database connection waiting queue number is greater than a third preset threshold, or the database idle connection number is less than a fourth preset threshold, it is determined that the thread pool required for the database operation is the preset thread pool.
4. The method of claim 1, wherein, The method further comprises: The system state data in a preset historical period is collected; According to the system state data in the preset historical period, a first time range in which the business system is in a preset state is determined; wherein the preset state is a running state of the business system when the resource usage rate of the device hardware is greater than a preset value; When the current time is within the first time range, the parameters of a preset thread pool are configured as first parameters; wherein the parameters of the preset thread pool comprise a maximum thread number; When the current time is outside the first time range, the parameters of the preset thread pool are configured as second parameters; wherein the maximum thread number in the second parameters is greater than the maximum thread number in the first parameters.
5. The method of claim 1, wherein, The method further comprises: If the central processing unit utilization rate is less than a fifth preset threshold, and the database idle connection number is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold, it is determined that the thread pool required for the database operation is the virtual thread carrier pool.
6. A thread pool switching apparatus characterized by comprising: The apparatus comprises: A system state data acquisition module is configured to acquire, after receiving a database operation request, system state data representing a running state of a business system; wherein the system state data comprises at least one of a central processing unit utilization rate of device hardware, a database idle connection number, a queue length of a virtual thread carrier pool, and a database connection waiting queue number; The preset thread pool judgment module is configured to determine, by using the system state data, whether a thread pool required for executing the database operation corresponding to the database operation request is a preset thread pool. If yes, a thread pool decision instruction for the preset thread pool is generated, and a preset thread in the preset thread pool is called to execute the database operation. If no, a thread pool decision instruction for a virtual thread carrier pool is generated, and a virtual thread in the virtual thread carrier pool is called to execute the database operation.
7. The apparatus according to claim 6, wherein when the resource usage rate of the device hardware is greater than a preset value, the throughput of a service system corresponding to the preset thread is greater than the throughput of a service system corresponding to the virtual thread.
8. The apparatus of claim 6, wherein, The preset thread pool judgment module comprises: A preset thread pool judgment submodule is configured to determine that the thread pool required for the database operation is the preset thread pool, if the central processor utilization rate is greater than a first preset threshold, or the queue length of the virtual thread carrier pool is greater than a second preset threshold, or the number of database connection waiting queues is greater than a third preset threshold, or the number of database idle connections is less than a fourth preset threshold. The apparatus further comprises:
9. The apparatus of claim 6, wherein, A system state data acquisition module is configured to acquire the system state data in a preset historical period. A first time range determination module is configured to determine a first time range in which the service system is in a preset state according to the system state data in the preset historical period, wherein the preset state is a running state of the service system when the resource usage rate of the device hardware is greater than a preset value. A first parameter configuration module is configured to configure a parameter of a preset thread pool as a first parameter when a current time is within the first time range, wherein the parameter of the preset thread pool comprises a maximum thread number. A second parameter configuration module is configured to configure the parameter of the preset thread pool as a second parameter when the current time is outside the first time range, wherein the maximum thread number in the second parameter is greater than the maximum thread number in the first parameter. The apparatus further comprises:
10. The apparatus of claim 6, wherein, A virtual thread carrier pool determination module is configured to determine that the thread pool required for the database operation is the virtual thread carrier pool, if the central processor utilization rate is less than a fifth preset threshold, and the number of database idle connections is greater than a sixth preset threshold, and the queue length of the virtual thread carrier pool is less than a seventh preset threshold. The apparatus comprises a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus.
11. An electronic device, comprising: The memory is configured to store a computer program. The processor is configured to execute the program stored in the memory to implement the method steps of any one of claims 1-5. The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method steps of any one of claims 1-5.
12. A computer-readable storage medium, characterized in that,