Thread pool matching method, electronic device and storage medium
By dynamically allocating thread pools, the problems of idle thread pool resources and excessive congestion are solved, achieving efficient utilization of thread resources and improving system stability.
Patent Information
- Application Number
- CN202510575916.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-06
- Publication Date
- 2026-11-06
AI Technical Summary
The current technology for calling thread pools is not efficient enough, causing the thread pool to be idle at times and overly congested at others, making it difficult to achieve reasonable and efficient use of resources.
By obtaining the required number of threads for the business interface and the available number of threads in the thread pool in real time, the most suitable thread pool is dynamically allocated to achieve on-demand allocation and cross-pool scheduling of thread resources, avoiding thread pool overload or idleness.
It achieves efficient utilization of thread resources, reduces thread resource waste, improves system response speed and stability, and avoids performance degradation and service interruption problems caused by frequent thread pool reorganization.
Smart Images

Figure CN123048113A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer software technology, and in particular to a thread pool matching method, electronic device, and storage medium. Background Technology
[0002] Thread pools are a key technology for improving system performance and responsiveness, and are widely used in modern software development. As an efficient multi-threading mechanism, they achieve thread reuse and management by adding tasks to a queue and automatically starting these tasks after thread creation, thus reducing the overhead of thread creation and destruction. Threads in a thread pool are typically set as background threads, using the default stack size, running at the default priority, and existing within a multi-threaded unit. The thread pool design mechanism also includes dynamically adjusting the number of threads: when a thread is idle in managed code (e.g., waiting for an event), the thread pool inserts another auxiliary thread to keep all processors busy; if all thread pool threads remain busy, but there is still pending work in the queue, the thread pool will create new auxiliary threads after a period of time, but the total number of threads will not exceed a preset maximum value. Threads exceeding the maximum value will enter a waiting queue and will not start until other threads complete. This thread pool mechanism, automatically managed by the system, greatly reduces the burden of thread management for programmers, allowing them to focus on processing application tasks.
[0003] Currently, related technologies typically allocate different business interfaces to independent thread pools, ensuring that each type of business interface runs only in its corresponding thread pool. When a business interface thread in a certain thread pool becomes blocked, it will not occupy resources in other thread pools, thus avoiding affecting the normal operation of other thread pools. However, traditional thread pool creation methods often involve pre-programming and setting up a fixed thread pool when the program starts, resulting in a fixed binding relationship between business interfaces and thread pools. This fixed binding mode is prone to causing the corresponding thread pool to be idle at times and over-congested at other times for business interfaces with unstable access, making it difficult to achieve reasonable and efficient use of thread pool resources. Summary of the Invention
[0004] The purpose of this application is to provide a thread pool matching method, electronic device and storage medium, which aims to solve the problem that the current thread pool is not efficient enough, resulting in the thread pool being sometimes idle and sometimes over-congested.
[0005] To achieve the above objectives, this application adopts the following technical solution:
[0006] Firstly, this application provides a thread pool matching method, comprising: obtaining information about a business interface to be accessed and information about multiple thread pools; wherein, the information about the business interface to be accessed includes at least the required number of threads for the business interface to be accessed, and the thread pool information includes at least the number of available threads in the thread pool; performing thread pool matching on the business interface to be accessed based on the information about the business interface to be accessed and the information about the multiple thread pools, and obtaining a matching result; if the matching result is successful, submitting the task of the business interface to be accessed to the task queue of the thread pool indicated in the matching result.
[0007] The technical solution provided in this application can dynamically allocate the most suitable thread pool by obtaining the required number of threads for the business interface and the available number of threads in the thread pool in real time, thereby realizing on-demand allocation and cross-pool scheduling of thread resources, avoiding thread pool overload or idleness, and avoiding the situation where the business interface and the thread pool are statically bound in the traditional thread pool configuration, thereby reducing the waste of thread resources and realizing the efficient utilization of the thread pool.
[0008] In some embodiments, the above-mentioned thread pool matching for the service interface to be accessed based on the information of the service interface to be accessed and the information of multiple thread pools to obtain a matching result specifically includes: if the number of available threads in multiple thread pools is greater than or equal to the number of threads required by the service interface to be accessed, the matching result is determined to be a successful match; wherein, the matching result is also used to indicate the thread pool that matches the service interface to be accessed.
[0009] In some embodiments, the method further includes: when the number of available threads in multiple thread pools is less than the number of threads required for the business interface to be accessed, obtaining information of at least one thread pool group; wherein each thread pool group includes multiple thread pools, and the information of the thread pool group includes at least the sum of the number of available threads in all thread pools in the thread pool group; and performing thread pool matching again on the business interface to be accessed based on the information of the business interface to be accessed and the information of at least one thread pool group to obtain a matching result.
[0010] Based on the above embodiments, when a single thread pool cannot meet the demand, the concept of a thread pool group can be introduced. By combining the available thread counts of multiple thread pools for matching, the resources of multiple thread pools are aggregated through the thread pool group, solving the problem of insufficient resources in a single thread pool. Furthermore, it supports cross-thread pool resource allocation, improving overall resource utilization and achieving flexible scheduling of thread pools.
[0011] In some embodiments, based on the information of the service interface to be accessed and the information of at least one thread pool group, the thread pool matching of the service interface to be accessed is performed again to obtain a matching result, including: if the sum of the number of available threads in at least one thread pool group is greater than or equal to the number of threads required by the service interface to be accessed, the matching result is determined to be a successful match; wherein, the matching result is also used to indicate all thread pools in the thread pool group that match the service interface to be accessed.
[0012] In some embodiments, the information of the service interface to be accessed further includes the task type of the task; submitting the task of the service interface to be accessed to the task queue of the thread pool indicated in the matching result includes: marking all thread pools in the thread pool group indicated in the matching result as marked allocation thread pools; submitting CPU-intensive tasks to the task queues of each allocation thread pool according to the allocatable weight of the allocation thread pool; and submitting I / O-intensive tasks to the remaining task queues of each allocation thread pool.
[0013] Based on the above embodiments, by analyzing the types of thread tasks required by the business interface, when matching the available threads in the two thread pools to allocate the interface, CPU-intensive tasks can be allocated to the thread pool with high computing power, and IO-intensive tasks can be allocated to the thread pool with high I / O throughput, thereby improving task execution efficiency.
[0014] In some embodiments, the method further includes: if the matching result is a failure, allocating the service interface to be accessed to a blocking queue. This allows for buffering service requests through the blocking queue when resources are insufficient, preventing system crashes or task loss.
[0015] In some embodiments, before obtaining the information of the business interface to be accessed and the information of multiple thread pools, the method further includes: obtaining the number of threads required for each business interface and the task type corresponding to each thread; establishing a thread pool according to the number of threads required for each business interface and the task type corresponding to each thread; and generating a thread pool information set, which is used to reflect the number of available threads in each thread pool in real time.
[0016] Based on the above embodiments, by pre-establishing a thread pool, the resource allocation time during business access is reduced, and the system response speed is improved. Furthermore, the thread pool information set reflects the number of available threads in real time, supporting dynamic adjustment and optimization of resource allocation strategies.
[0017] In some embodiments, the above-mentioned establishment of a thread pool based on the number of threads required by each business interface and the task type corresponding to each thread specifically includes: generating a set of thread numbers based on the thread requirements of each business interface; wherein, the required thread numbers in the set of thread numbers are arranged in ascending order; dividing the set of thread numbers into multiple subsets based on equal intervals; determining the core thread number of each thread pool to be established based on each subset of thread numbers; and establishing multiple thread pools based on the core thread number of each thread pool to be established.
[0018] Secondly, this application provides a thread pool matching device, the device comprising:
[0019] The acquisition module is used to acquire information about the business interface to be accessed and information about multiple thread pools; the information about the business interface to be accessed includes at least the required number of threads for the business interface to be accessed, and the thread pool information includes at least the number of available threads in the thread pool.
[0020] The matching module is used to perform thread pool matching for the business interface to be accessed based on the information of the business interface to be accessed and the information of multiple thread pools, and obtain the matching result.
[0021] The processing module is used to submit the task to be connected to the business interface to the task queue of the thread pool indicated in the matching result if the matching result is successful.
[0022] Thirdly, this application also provides an electronic device, including: a processor and a memory; the memory storing processor-executable instructions; when the processor is configured to execute the instructions, it causes a communication device to implement any of the methods provided in the first aspect above.
[0023] Fourthly, a computer-readable storage medium is provided that stores computer instructions that, when executed on a computer, cause the computer to perform any of the methods provided in the first aspect.
[0024] Fifthly, a computer program product comprising computer instructions is provided, which, when executed on a computer, cause the computer to perform any of the methods provided in the first aspect.
[0025] The technical effects of any of the possible embodiments of the second to fifth aspects can be found in the first aspect described above, as well as the technical effects of any of the possible embodiments of the first aspect described above, which will not be repeated here. Attached Figure Description
[0026] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0027] Figure 1 This is a schematic diagram of the structure of a thread pool matching device provided in an embodiment of this application;
[0028] Figure 2 A flowchart illustrating a thread pool matching method provided in an embodiment of this application;
[0029] Figure 3 A flowchart illustrating another thread pool matching method provided in an embodiment of this application;
[0030] Figure 4 A flowchart illustrating another thread pool matching method provided in an embodiment of this application;
[0031] Figure 5 This is a schematic diagram of the composition of a thread pool matching device provided in an embodiment of this application. Detailed Implementation
[0032] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0033] In the description of this application, it should be understood that the terms "upper," "lower," "left," "right," "front," "rear," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or relative positional relationship shown in the accompanying drawings. They are used only for the convenience of describing this application and for simplification, and do not indicate or imply that the device or element referred to needs to have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application. Unless otherwise specified, the above-mentioned orientational descriptions can be flexibly set in practical applications, provided that the relative positional relationship shown in the accompanying drawings is satisfied.
[0034] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, unless otherwise stated, "a plurality of" means two or more.
[0035] In the description of this application, it should be noted that, unless otherwise expressly specified and limited, the terms "installation," "connection," "linking," and "communication" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection. They can refer to a direct connection or an indirect connection through an intermediate medium, or a connection within two components. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.
[0036] In embodiments of this application, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, 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, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, article, or apparatus that includes that element.
[0037] In the embodiments of this application, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design that is described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design. Specifically, the use of the terms "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.
[0038] In the description of this specification, specific features, structures, materials, or characteristics may be combined in any suitable manner in one or more embodiments or examples.
[0039] Currently, the common practice is to assign business interfaces to different thread pools, ensuring that each type of business interface can only be executed in its corresponding thread pool. The initial intention of this design is to prevent a blocked thread of one business interface from occupying resources of other thread pools, thereby affecting the overall thread efficiency of the system. However, this statically bound thread pool configuration method has significant shortcomings. Thread pool creation is usually fixed during program startup, forming a one-to-one fixed relationship between business interfaces and their corresponding thread pools. For business interfaces with unstable access patterns, this static binding strategy often leads to their corresponding thread pools being idle at times and congested at others, failing to achieve efficient utilization of thread pool resources. In related technologies, once a thread pool with a fixed number of core threads is set, when these thread pools cannot meet the concurrency requirements of business interfaces, the system often needs to reconfigure or reorganize the thread pools. This process not only consumes a large amount of CPU and memory resources but may also adversely affect system stability. Especially in high-concurrency, high-load application scenarios, frequent thread pool reorganization can lead to system performance degradation or even serious problems such as service interruption.
[0040] In view of this, this application provides a thread pool matching method, including: obtaining information about the business interface to be accessed and information about multiple thread pools; wherein, the information about the business interface to be accessed includes at least the required number of threads for the business interface to be accessed, and the thread pool information includes at least the number of available threads in the thread pool; based on the information about the business interface to be accessed and the information about multiple thread pools, performing thread pool matching on the business interface to be accessed to obtain a matching result; if the matching result is successful, submitting the task of the business interface to be accessed to the task queue of the thread pool indicated in the matching result. Based on this method, the most suitable thread pool can be dynamically allocated by obtaining the required number of threads for the business interface and the number of available threads in the thread pool in real time, realizing on-demand allocation and cross-pool scheduling of thread resources, avoiding thread pool overload or idleness, and avoiding the situation of static binding between business interfaces and thread pools in traditional thread pool configurations, thereby reducing thread resource waste and achieving efficient utilization of thread pools.
[0041] A thread is the smallest unit of computation that the operating system can schedule. It is contained within a process and is the actual unit of operation within that process. A process, on the other hand, is the basic unit for allocating resources by the operating system, containing program code, data, and execution status information. Generally, when a program is started, the operating system creates a process for that program. It can be understood that the program itself is not a process, but rather an executable file stored on the disk.
[0042] The embodiments of this application can be applied to the field of terminal technology, specifically to the operating system (OS) of a terminal. The OS involved in the embodiments of this application is the most basic system software running on a terminal device. The software system of the terminal device can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture, etc., and this application does not specifically limit it in this regard.
[0043] In some embodiments, this application also provides a thread pool matching device, which is used in the thread pool matching method provided in this application and can be an electronic device with data processing capabilities. For example, the thread pool matching device can be a terminal device. Alternatively, the thread pool matching device can be a functional module of the terminal device, or it can be any computing device connected to the terminal device. This application does not limit this aspect.
[0044] like Figure 1 The diagram shown is a structural schematic of a thread pool matching device 100 provided in an embodiment of this application.
[0045] like Figure 1 As shown, the thread pool matching device 100 includes a processor 110, a communication line 120, and a communication interface 130.
[0046] Optionally, the thread pool matching device 100 may further include a memory 140. The processor 110, memory 140, and communication interface 130 can be connected via a communication line 120.
[0047] The processor 110 can be a central processing unit (CPU), a network processor (NP), a digital signal processor (DSP), a microprocessor, a microcontroller, a programmable logic device (PLD), or any combination thereof. The processor 110 can also be any other device with processing capabilities, such as a circuit, device, or software module, without limitation.
[0048] In one example, processor 110 may include one or more CPUs, for example Figure 1 CPU0 and CPU1 in the CPU.
[0049] As an optional implementation, the thread pool matching device 100 may include multiple processors, for example, in addition to processor 110, it may also include processor 170. A communication line 120 is used to transmit information between the components included in the thread pool matching device 100.
[0050] Communication interface 130 is used to communicate with other devices or other communication networks. These other communication networks can be Ethernet, radio access network (RAN), wireless local area networks (WLAN), etc. Communication interface 130 can be a module, circuit, transceiver, or any device capable of enabling communication.
[0051] Memory 140 is used to store instructions. These instructions may be computer programs.
[0052] The memory 140 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and / or instructions; it may also be a random access memory (RAM) or other type of dynamic storage device capable of storing information and / or instructions; it may also be an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media, or other magnetic storage devices, etc., without limitation.
[0053] It should be noted that the memory 140 can exist independently of the processor 110 or be integrated with the processor 110. The memory 140 can be used to store instructions, program code, or some data. The memory 140 can be located within or outside the thread pool matching device 100, without restriction.
[0054] The processor 110 is configured to execute instructions stored in the memory 140 to implement the method provided in the following embodiments of this application. For example, when the thread pool matching device 100 is a terminal or a chip or system-on-a-chip in the terminal, the processor 110 can execute instructions stored in the memory 140 to implement the thread pool matching method provided in this application.
[0055] As an optional implementation, the thread pool matching device 100 also includes an output device 150 and an input device 160. The output device 150 can be a display screen, speaker, or other device capable of outputting data from the thread pool matching device 100 to the user. The input device 160 can be a keyboard, mouse, microphone, joystick, or other device capable of inputting data into the thread pool matching device 100.
[0056] Understandable, Figure 1 The structure shown does not constitute a specific limitation on the thread pool matching device. For example, in other embodiments of this application, the thread pool matching device may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0057] It is understood that in the embodiments of this application, the thread pool matching device performs some or all of the steps in the embodiments of this application. These steps or operations are merely examples, and the embodiments of this application may also perform other operations or variations thereof. Furthermore, the various steps may be performed in different orders as presented in the embodiments of this application, and it is not necessarily necessary to perform all the operations in the embodiments of this application.
[0058] The method provided in this application will be described in detail below with reference to specific embodiments.
[0059] like Figure 2 As shown in the figure, this application embodiment provides a flowchart of a thread pool matching method, which may include the following steps:
[0060] S101. Obtain information about the business interface to be accessed and information about multiple thread pools.
[0061] Among them, the business interface to be accessed refers to the external or internal business interface that needs to be accessed but has not yet been allocated thread pool resources. For example, the business functional unit that needs to process tasks through the thread pool in the current system is usually represented as a functional module that can be called in the software system.
[0062] It should be understood that when these interfaces receive external requests or are triggered by internal tasks, they need to acquire thread resources to execute specific business logic. However, they have not yet been allocated to a specific thread pool for processing, so a thread pool matching mechanism is needed to determine the corresponding execution resources. For example, the business logic in this application may include various possible business processes such as user registration / login, message sending, order payment, device status reporting, and power management.
[0063] In some embodiments, the information of the service interface to be accessed includes at least the required number of threads for the service interface to be accessed.
[0064] For example, the required number of threads, which is also the number of threads needed when the business interface processes tasks, can be determined based on historical concurrency, performance test results, or estimates.
[0065] In some embodiments, the information of the service interface to be accessed may also include service priority, maximum allowed execution time, resource requirements, service type, etc. The service type may include CPU-intensive (computation-intensive) or I / O-intensive (primarily waiting for external responses), and I / O-intensive may also be referred to as I / O-intensive.
[0066] In some embodiments, the thread pool information includes at least the number of available threads in the thread pool.
[0067] In some embodiments, thread pool information may also include core thread count, maximum thread count, task queue type and capacity, thread lifespan, thread pool status, current active thread count, resource usage, or other possible information.
[0068] For example, the thread pool matching device can obtain information about the aforementioned multiple thread pools from the thread pool information set corresponding to the thread pools established in the current system. The thread pool establishment process can be specifically referred to below. Figure 3 The embodiments shown are not described in detail here.
[0069] The thread pool information set is used to reflect the number of available threads in each thread pool in real time. For example, the thread pool information set can be in the form of a graph showing the number of available threads in each thread pool. Taking a table showing the number of available threads in a thread pool as an example, this table can include identification information for multiple thread pools and multiple numbers of available threads, with a corresponding relationship between the identification of the multiple thread pools and the multiple numbers of available threads.
[0070] For example, the thread pool information set may also include other possible thread pool information, such as the core thread count, maximum thread count, task queue type and capacity, etc., which will not be listed here.
[0071] In some embodiments, the thread pool matching device may store the thread pool information set and may dynamically maintain the thread pool information set.
[0072] S102. Based on the information of the business interface to be accessed and the information of multiple thread pools, perform thread pool matching on the business interface to be accessed and obtain the matching result.
[0073] In one possible implementation, if the number of available threads in multiple thread pools is greater than or equal to the number of threads required by the business interface to be accessed, the matching result is determined to be a successful match.
[0074] The matching results are also used to indicate the thread pool that matches the business interface to be accessed.
[0075] For example, when accessing a business interface, the number of threads required by the business interface can be compared with the thread pool information set to determine whether there is a thread pool with a number of available threads greater than or equal to the number of threads required by the business interface to be accessed. If such a thread pool exists, it means that the match is successful.
[0076] At this point, the thread pool that matches the business interface to be accessed can also be determined.
[0077] For example, all thread pools with a number of available threads greater than or equal to the number of threads required for the business interface to be accessed can be identified, and one of these thread pools can be selected as the thread pool that matches the business interface to be accessed, such as the thread pool with the fewest available threads. That is, the thread pool with the fewest available threads is the thread pool indicated in the matching result.
[0078] The thread pool with the fewest available threads is identified as the thread pool that matches the business interface to be integrated. In other words, the thread pool with the fewest available threads is the thread pool indicated in the matching results.
[0079] In another possible implementation, if the number of available threads in multiple thread pools is less than the number of threads required by the business interface to be integrated, information on at least one thread pool group is obtained. Then, based on the information of the business interface to be integrated and the information of at least one thread pool group, thread pool matching is performed again for the business interface to be integrated, and a matching result is obtained.
[0080] For example, if the sum of the number of available threads in at least one thread pool group is greater than or equal to the number of threads required for the business interface to be accessed, the matching result is determined to be a successful match.
[0081] Each thread pool group comprises multiple thread pools, and the information for each thread pool group includes at least the sum of the available thread counts of all thread pools within the group. The matching results are also used to indicate which thread pools in the thread pool group match the business interface to be integrated.
[0082] For example, a business interface can also be marked as an allocation interface.
[0083] Therefore, based on the thread pool information set and the required number of threads for the allocation interface, thread pool matching is performed. Any two (or more) available thread numbers are added together, and after traversal, several combinations (i.e., one thread pool group as mentioned above) of available thread numbers are obtained. It is then determined whether there exists a combination of available thread numbers greater than or equal to the available thread number for the allocation interface. If such a combination exists, the match is successful.
[0084] At this point, the thread pool in the thread pool group that matches the business interface to be accessed can also be determined.
[0085] For example, if there are multiple thread pool groups whose sum of available threads is greater than or equal to the number of threads required by the business interface to be accessed, all thread pool groups that meet this condition can be identified first, and then one of them can be selected as the thread pool group that matches the business interface to be accessed. For example, the thread pool group with the fewest sum of available threads, that is, the thread pool group with the fewest available threads is the thread pool group indicated in the matching result.
[0086] In some embodiments, the matching results may also indicate multiple thread pool groups.
[0087] It should be understood that the above-described method of forming a thread pool group with two thread pools is only an illustrative example. In this embodiment, the number of thread pools in a thread pool group is not specifically limited, and can be 3, 4 or other possible numbers. The specific matching process is similar to the above two examples, and will not be described in detail here.
[0088] It should be noted that, based on the above embodiments, when a single thread pool cannot meet the demand, the concept of a thread pool group can be introduced. By combining the available thread counts of multiple thread pools for matching, the resources of multiple thread pools can be aggregated through the thread pool group, thus solving the problem of insufficient resources in a single thread pool. Furthermore, it supports cross-thread pool resource allocation, improving overall resource utilization and achieving flexible scheduling of thread pools.
[0089] In some embodiments, if the number of available threads in multiple thread pools is less than the number of threads required for the business interface to be connected, the business interface to be connected can be directly transferred to the blocking queue. This means the matching result is determined to be a failure.
[0090] In another possible implementation, if the sum of the number of available threads in at least one thread pool group is less than the number of threads required by the business interface to be accessed, the matching result is determined to be a matching failure.
[0091] S103. If the matching result is successful, submit the task to be connected to the business interface to the task queue of the thread pool indicated in the matching result.
[0092] For example, for business interfaces to be accessed, their business tasks can usually be assigned to a suitable thread pool so that the threads in the thread pool can execute these tasks according to certain rules and priorities.
[0093] Each thread pool typically has one or more task queues to store tasks to be executed. These tasks are queued in a certain order (such as first-in-first-out, priority, etc.) waiting for threads in the thread pool to execute them.
[0094] Therefore, the specific task to be connected to the business interface (which could be a function call, an executable object, or a task descriptor) can be added to the task queue of the thread pool specified in the matching results. After the task is submitted, it becomes part of the tasks pending processing in the thread pool, waiting to be retrieved and executed by a thread in the thread pool.
[0095] In some embodiments, step S103 may also be specifically implemented as the following steps S1-S3:
[0096] S1. Mark all thread pools in the thread pool group indicated in the matching results as marked allocation thread pools.
[0097] For example, the thread pool group indicated in the matching results may include multiple thread pools.
[0098] S2. Based on the allocable weight of the allocation thread pool, submit tasks of CPU intensive type to the task queue of each allocation thread pool.
[0099] For example, obtain the task type of the task for this interface, which includes CPU-intensive and I / O-intensive tasks. Alternatively, it can be understood as obtaining the task type of the thread required by this interface, and distinguishing between CPU-intensive and I / O-intensive tasks.
[0100] For example, taking the allocation of thread pools including thread pool A and thread pool B as an example, the allocatable weights are explained as follows:
[0101]
[0102] M A =M·W A Formula (3)
[0103] M i =M·W B Formula (4)
[0104] Among them, W A W represents the weight coefficient of thread pool A. B N represents the weight coefficient of thread pool B. A Let N be the number of available threads in thread pool A. B M represents the number of available threads in thread pool B. A The number of CPU-intensive tasks allocated to thread pool A, rounded up, M BM represents the number of CPU-intensive tasks allocated to thread pool B, rounded down, where M is the total number of CPU-intensive tasks.
[0105] In another example, M A It can also be understood as the number of CPU-intensive threads allocated to thread pool A, rounded up, M B It can also be understood as the number of CPU-intensive threads allocated to thread pool B, rounded down, where M is the total number of CPU-intensive threads.
[0106] It should be understood that the above-mentioned allocatable weights may include W. A With W B Therefore, based on this assignable weight, N tasks of CPU-intensive type can be assigned... A N tasks are submitted to the task queue of thread pool A, with N tasks of CPU intensive type. B Each task is submitted to the task queue of thread pool B. In some embodiments, thread pool A is an allocation thread pool with a large number of available threads, and thread pool B is an allocation thread pool with a large number of available threads.
[0107] S3. Assign IO-intensive tasks based on the remaining available threads in each thread pool.
[0108] For example, one can first calculate the number of remaining available threads in each allocated thread pool.
[0109] In some embodiments, tasks of the I / O intensive type can be assigned based on the priority of each allocation thread pool.
[0110] For example, thread pools with fewer available threads have higher priority.
[0111] For example, I / O-intensive tasks are submitted to the task queue of a thread pool with fewer available threads (e.g., thread pool B). After that thread pool has finished allocating its threads, the remaining I / O-intensive tasks are submitted to the task queue of a thread pool with more available threads (e.g., thread pool A). Alternatively, threads can be understood as being allocated to a thread pool with fewer available threads, and after that thread pool has finished allocating its threads, the remaining I / O-intensive threads are allocated to a thread pool with more available threads.
[0112] In this way, by analyzing the types of thread tasks required by the business interface, when matching the available threads in the two thread pools to the assigned interface, CPU-intensive tasks can be assigned to the thread pool with high computing power, and IO-intensive tasks can be assigned to the thread pool with high I / O throughput, thereby improving task execution efficiency.
[0113] In some embodiments, if the matching result is a failure, the service interface to be accessed is assigned to a blocking queue.
[0114] Among them, the blocking queue is a task temporary storage area, which is used to temporarily store tasks to be processed when the system cannot immediately allocate enough thread resources.
[0115] For example, this interface can be redirected to a blocking queue. For business interfaces queued in the blocking queue, changes in the number of available threads in the thread pool information set are detected. When a change occurs in the available thread count graph, the above operation is repeated to match the threads for the interfaces in the queue. In this way, when resources are insufficient, business requests can be buffered through the blocking queue, preventing system crashes or task loss.
[0116] The technical solution provided in this application can dynamically allocate the most suitable thread pool by obtaining the required number of threads for the business interface and the available number of threads in the thread pool in real time, thereby realizing on-demand allocation and cross-pool scheduling of thread resources, avoiding thread pool overload or idleness, and avoiding the situation where the business interface and the thread pool are statically bound in the traditional thread pool configuration, thereby reducing the waste of thread resources and realizing the efficient utilization of the thread pool.
[0117] In some embodiments, such as Figure 3 As shown, prior to step S101 above, the method may further include the following steps:
[0118] S201. Obtain the number of threads required for each business interface and the task type corresponding to each thread.
[0119] For example, the number of threads required for all business interfaces and the task types corresponding to different threads can be obtained. The task types include CPU-intensive and I / O-intensive tasks.
[0120] CPU-intensive tasks refer to tasks that require a large amount of CPU computation. These tasks are characterized by high CPU utilization and relatively few I / O operations. CPU-intensive tasks include, but are not limited to, mathematical calculations, encryption algorithms, and high-definition video decoding. I / O-intensive tasks refer to tasks that require frequent input / output operations. These tasks are characterized by low CPU utilization and frequent and time-consuming I / O operations. I / O-intensive tasks include, but are not limited to, file reading and writing, database access, and network requests.
[0121] S202. Establish a thread pool based on the number of threads required for each business interface and the task type corresponding to each thread.
[0122] For example, the process of creating a thread pool may specifically include:
[0123] A set of thread counts is generated based on the thread requirements of each business interface; the required thread counts in the set are then sorted in ascending order. In other words, the thread counts required for all business structures are sorted from fewest to most.
[0124] The set of thread counts is divided into multiple subsets based on the number of thread pools to be planned. For example, the set of thread counts is divided into several segments based on the number of thread pools to be planned, where the number of thread pools to be planned is an integer greater than 1.
[0125] The core thread count for each thread pool to be created is determined based on the subset of thread counts for each pool. For example, the maximum value of a single thread count segment can be used as the core thread count for the thread pool.
[0126] Create multiple thread pools based on the core thread count of each pool to be created. For example, the planned number of threads for each thread pool can be constructed according to the core thread count.
[0127] S203. Generate a thread pool information set, which is used to reflect the number of available threads in each thread pool in real time.
[0128] For example, a unique marker can be added to the thread pool to obtain the core thread count corresponding to the thread pool. Using the unique marker as the preceding data and the corresponding core thread count as the following data, an initial graph of the number of available threads in the thread pool can be constructed (that is, the thread pool information set mentioned above).
[0129] Then, the core threads that are working in the thread pool can be recorded as the number of worker threads, and the core threads that are idle in the thread pool can be recorded as the number of available threads. The number of available threads can be used to replace the subsequent data to obtain a chart of the number of available threads in the thread pool.
[0130] Based on the above embodiments, by performing a gradient sorting of the number of threads required by the business interface, several thread pools with different core thread counts can be established. The number of available threads in different thread pools can be dynamically updated by establishing real-time available thread count information. Furthermore, this method can reduce resource allocation time during business access and improve system response speed by pre-establishing thread pools. Moreover, the thread pool information set reflects the number of available threads in real time, supporting dynamic adjustment and optimization of resource allocation strategies.
[0131] In some embodiments, in conjunction with the above Figure 2 as well as Figure 3 In addition to the embodiments described herein, the method provided in this application can also be as follows: Figure 4 As shown, it includes:
[0132] Sa, obtain the number of threads required for all business interfaces and the task types corresponding to different threads.
[0133] Sb. Determine the core number of threads based on the number of threads required by the business interface, and establish a thread pool.
[0134] Sc. Create a graph showing the number of available threads in the thread pool.
[0135] When integrating business interfaces, the thread pool is matched to the number of threads required by the business interface by referring to the available thread pool number chart.
[0136] If a match is found, proceed to step Sf.
[0137] If the match fails, proceed to step Se.
[0138] Se, mark the business interface as the allocation interface, and match the thread pool according to the number of threads required by the allocation interface based on the available thread pool quantity chart.
[0139] If a match is found, proceed to step Sf.
[0140] If the match fails, proceed to step Sg.
[0141] Sf, thread scheduling ends.
[0142] Sg, transfer the allocation interface to the blocking queue.
[0143] In this way, cross-pool scheduling of threads is achieved, effectively reducing the thread idle rate while ensuring system stability and efficient allocation of system resources. This solves the problem that existing thread pool dynamic scheduling cannot perform cross-pool scheduling, and that frequent thread pool reorganization consumes CPU and memory resources, which can easily affect system stability.
[0144] In some embodiments, this application also provides another thread pool matching method, including:
[0145] Step 1: Obtain the number of threads required for all business interfaces.
[0146] Step 2: Determine the core thread count based on the required number of threads for the business interface, and establish a thread pool. Specific methods for establishing the thread pool include:
[0147] Sort the required number of threads for all business structures in ascending order of minimum to maximum, and obtain the set of thread counts;
[0148] The set of threads is divided into several equal segments according to the number of thread pools to be planned, where the number of thread pools to be planned is an integer greater than one.
[0149] Take the maximum value of a single thread count segment as the core thread count of the thread pool;
[0150] Construct the number of thread pools to be planned based on the core number of threads.
[0151] Step 3: Create a graph showing the number of available threads in the thread pool. Specific methods include:
[0152] Add a unique marker to the thread pool, obtain the core thread count corresponding to the thread pool, and construct an initial chart of the number of available threads in the thread pool with the unique marker as the preceding data and the corresponding core thread count as the following data.
[0153] The core threads currently working in the thread pool are recorded as worker threads, and the core threads in the thread pool that are idle are recorded as available threads. The available thread count is used to replace the subsequent data to obtain a chart of the number of available threads in the thread pool.
[0154] Step 4: When integrating business interfaces, match the thread pool to the required number of threads based on the available thread count chart. Matching methods include:
[0155] Obtain the number of threads required by the business interface and compare it with the number of available threads in the thread pool available thread count chart. Determine if there is a thread pool with an available thread count greater than or equal to the number of threads required by the business interface. If there is, mark the thread pool as a waiting thread pool. At this time, the waiting thread pool is a thread pool with an available thread count greater than or equal to the number of threads required by the business interface. If there is no waiting thread pool, it means that the match failed and proceed to step five.
[0156] The pool of available threads with the fewest available threads is selected as the worker thread pool. The available threads in the worker thread pool are matched with the business interface. If the match is successful, proceed to step six.
[0157] Step 5: Transfer the business interface to the blocking queue.
[0158] Step 6: Thread scheduling ends.
[0159] For business interfaces queued in the blocking queue, the thread pool's available thread count chart is checked for changes. When the chart changes, step four is repeated to match the threads of the business interfaces in the queue.
[0160] The foregoing primarily describes the solutions provided by the embodiments of this application from a methodological perspective. To achieve the aforementioned functions, it includes corresponding hardware structures and / or software modules for executing each function. Those skilled in the art should readily recognize that, in conjunction with the units and algorithm steps of the various examples described in the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0161] This application embodiment can group functional modules such as thread pool matching devices according to the above method examples. For example, each functional group can be assigned to a different functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the grouping of modules in this application embodiment is illustrative and only represents one logical functional grouping. In actual implementation, there may be other grouping methods.
[0162] Figure 5 The diagram shown is a schematic representation of a thread pool matching device provided in an embodiment of this application. Figure 5 As shown, the thread pool matching device 500 can be a terminal device, a chip within a terminal device, or a system on a chip of a forwarding device. The thread pool matching device 500 can be used to perform the functions of the terminal device involved in the above embodiments, such as executing the thread pool matching method described above. As one possible implementation, such as... Figure 5 As shown, the thread pool matching device 500 includes an acquisition module 501, a matching module 502, and a processing module 503.
[0163] The acquisition module 501 is used to acquire information about the business interface to be accessed and information about multiple thread pools; wherein, the information about the business interface to be accessed includes at least the required number of threads for the business interface to be accessed, and the thread pool information includes at least the number of available threads in the thread pool.
[0164] The matching module 502 is used to perform thread pool matching on the business interface to be accessed based on the information of the business interface to be accessed and the information of multiple thread pools, and obtain the matching result.
[0165] The processing module 503 is used to submit the task to be accessed by the business interface to the task queue of the thread pool indicated in the matching result if the matching result is successful.
[0166] In some embodiments, the matching module 502 is specifically used to: determine the matching result as a successful match when the number of available threads in multiple thread pools is greater than or equal to the number of threads required by the business interface to be accessed; wherein the matching result is also used to indicate the thread pool that matches the business interface to be accessed.
[0167] In some embodiments, the matching module 502 is further configured to: obtain information about at least one thread pool group when the number of available threads in multiple thread pools is less than the number of threads required for the business interface to be accessed; wherein each thread pool group includes multiple thread pools, and the information of the thread pool group includes at least the sum of the number of available threads in all thread pools in the thread pool group; and perform thread pool matching again on the business interface to be accessed based on the information of the business interface to be accessed and the information of at least one thread pool group to obtain a matching result.
[0168] In some embodiments, the matching module 502 is further configured to: determine the matching result as a successful match if the sum of the number of available threads in at least one thread pool group is greater than or equal to the number of threads required for the business interface to be accessed; wherein the matching result is further configured to indicate all thread pools in the thread pool group that match the business interface to be accessed.
[0169] In some embodiments, the processing module 503 is further configured to: mark all thread pools in the thread pool group indicated in the matching result as marked allocation thread pools; submit tasks of CPU intensive type to the task queue of each allocation thread pool according to the allocable weight of the allocation thread pool; and allocate tasks of I / O intensive type based on the remaining available thread count of each allocation thread pool.
[0170] In some embodiments, the processing module 503 is further configured to: allocate the service interface to be accessed to the blocking queue if the matching result is a matching failure.
[0171] In some embodiments, the acquisition module 501 is further configured to: acquire the number of threads required for each business interface and the task type corresponding to each thread. The processing module 503 is further configured to: establish a thread pool based on the number of threads required for each business interface and the task type corresponding to each thread; generate a thread pool information set, which is used to reflect the number of available threads in each thread pool in real time.
[0172] In some embodiments, the processing module 503 is further configured to: generate a set of thread numbers based on the thread requirements of each business interface; wherein the required thread numbers in the set of thread numbers are arranged in ascending order; divide the set of thread numbers into multiple subsets of thread numbers at equal intervals; determine the core thread number of each thread pool to be established based on each subset of thread numbers; and establish multiple thread pools based on the core thread number of each thread pool to be established.
[0173] For a more detailed description of the acquisition module 501, the matching module 502, and the processing module 503, as well as a more detailed description of each of their technical features and the beneficial effects, please refer to the corresponding method embodiment section above, which will not be repeated here.
[0174] It should be noted that, Figure 5 A module can also be called a unit; for example, an acquisition module can be called an acquisition unit. Additionally, Figure 5 In the embodiments shown, the names of the modules may not be the same as those shown in the figures. For example, the processing module may also be called the determination module.
[0175] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the grouping of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0176] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For instance, the grouping of modules or units is only a logical functional grouping, and in actual implementation, there may be other grouping methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0177] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0178] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0179] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device, such as a microcontroller, chip, or processor, to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media for storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.
[0180] This application also provides a computer-readable storage medium including computer-executable instructions that, when run on a computer, cause the computer to execute any of the thread pool matching methods provided in the above embodiments.
[0181] This application also provides a computer program product containing computer execution instructions, which, when run on a computer, causes the computer to execute any of the thread pool matching methods provided in the above embodiments.
[0182] This application also provides a chip comprising a processor for executing instructions that cause a device including the chip to perform any of the thread pool matching methods provided in the above embodiments.
[0183] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A thread pool matching method, characterized in that, The method includes: Obtain information about the business interface to be accessed and information about multiple thread pools; wherein, the information about the business interface to be accessed includes at least the required number of threads for the business interface to be accessed, and the thread pool information includes at least the number of available threads in the thread pool. Based on the information of the service interface to be accessed and the information of the multiple thread pools, thread pool matching is performed on the service interface to be accessed to obtain the matching result. If the matching result is successful, the task of the business interface to be accessed will be submitted to the task queue of the thread pool indicated in the matching result.
2. The method according to claim 1, characterized in that, The step of performing thread pool matching on the service interface to be accessed based on the information of the service interface to be accessed and the information of the multiple thread pools, and obtaining the matching result, includes: If the number of available threads in the plurality of thread pools is greater than or equal to the number of threads required by the service interface to be accessed, the matching result is determined to be a successful match; wherein, the matching result is also used to indicate the thread pool that matches the service interface to be accessed.
3. The method according to claim 2, characterized in that, The method further includes: If the number of available threads in the plurality of thread pools is less than the number of threads required for the business interface to be accessed, information of at least one thread pool group is obtained; wherein, each thread pool group includes a plurality of thread pools, and the information of the thread pool group includes at least the sum of the number of available threads of all thread pools in the thread pool group; Based on the information of the service interface to be accessed and the information of the at least one thread pool group, the thread pool matching is performed again on the service interface to be accessed to obtain the matching result.
4. The method according to claim 3, characterized in that, The step of performing thread pool matching again on the service interface to be accessed based on the information of the service interface to be accessed and the information of the at least one thread pool group to obtain the matching result includes: If the sum of the number of available threads in at least one thread pool group is greater than or equal to the number of threads required by the service interface to be accessed, the matching result is determined to be a successful match; wherein, the matching result is also used to indicate all thread pools in the thread pool group that match the service interface to be accessed.
5. The method according to claim 4, characterized in that, The information of the service interface to be accessed also includes the task type; submitting the task of the service interface to be accessed to the task queue of the thread pool indicated in the matching result includes: Mark all thread pools in the thread pool group indicated in the matching results as marked allocation thread pools; Based on the allocable weight of the allocated thread pool, tasks of CPU-intensive type are submitted to the task queues of each allocated thread pool; The task type of I / O intensive task is allocated based on the remaining available threads in each of the allocated thread pools.
6. The method according to any one of claims 1 to 5, characterized in that, The method further includes: If the matching result is a failure, the service interface to be accessed will be assigned to a blocking queue.
7. The method according to any one of claims 1 to 5, characterized in that, Before obtaining the information of the service interface to be accessed and the information of multiple thread pools, the method further includes: Obtain the number of threads required for each business interface and the task type corresponding to each thread; Establish a thread pool based on the number of threads required for each business interface and the task type corresponding to each thread. Generate a thread pool information set, which is used to reflect the number of available threads in each thread pool in real time.
8. The method according to claim 7, characterized in that, The step of establishing a thread pool based on the number of threads required for each business interface and the task type corresponding to each thread includes: A set of thread counts is generated based on the thread requirements of each business interface; wherein, the required thread counts in the set of thread counts are arranged in ascending order. The set of thread counts is divided into multiple subsets of thread counts by equal intervals. Determine the core thread count for each thread pool to be created based on the subset of thread counts. Based on the core thread count of each thread pool to be created, multiple thread pools are created.
9. An electronic device, characterized in that, include: Memory and processor; Memory and processor are coupled; The memory is used to store instructions that can be executed by the processor; When the processor executes the instructions, it performs the method as described in any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that, when executed on a computer, cause the computer to perform the method as described in any one of claims 1 to 8.
