Thread pool management method and device, and electronic device
By selecting the parent task in the thread pool call chain and executing the child task in the thread pool, and by dynamically adjusting the thread pool, the deadlock problem between parent and child tasks in thread pool management is solved. This achieves automated management and resource optimization of the thread pool, improving task execution efficiency and resource utilization.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING 58 INFORMATION TTECH CO LTD
- Filing Date
- 2022-12-23
- Publication Date
- 2026-05-01
AI Technical Summary
Existing thread pool management mechanisms may cause parent and child tasks to enter the same thread pool and compete for thread resources, leading to deadlock issues.
By selecting the thread pool following the parent task when determining the thread pool to execute a task in the thread pool call chain, the parent and child tasks are prevented from entering the same thread pool. New thread pools are created when necessary to meet the requirements. At the same time, the use of thread resources is optimized by periodically detecting and adjusting the maximum number of threads and the lifespan of the thread pool.
It solves the deadlock problem between parent and child tasks, realizes automated thread pool management, improves task execution efficiency and hardware resource utilization, and reduces system resource waste.
Smart Images

Figure CN116244088B_ABST
Abstract
Description
Thread pool management methods, devices, and electronic equipment Technical Field
[0001] This invention relates to the field of thread pool technology, and in particular to a thread pool management method, apparatus, and electronic device. Background Technology
[0002] A thread pool is a thread usage pattern. Too many threads can lead to scheduling overhead, which in turn affects cache locality and overall performance. A thread pool maintains multiple threads, waiting for a supervisor to assign concurrently executable tasks. This avoids the cost of creating and destroying threads when handling short-lived tasks.
[0003] Currently, thread pools have a general management mechanism. When the number of active threads in the thread pool has not reached the maximum number of threads, if a new task is submitted, the number of active threads can be continuously increased to process more tasks until the number of active threads equals the maximum number of threads. At this point, if a new task is submitted, it must wait for idle threads to become available in the thread pool before it can be executed.
[0004] However, the current thread pool management mechanism may cause parent and child tasks to enter the same thread pool and compete for thread resources when dealing with parent-child task processing scenarios, thus leading to deadlock problems. Summary of the Invention
[0005] In view of the above problems, embodiments of the present invention are proposed to provide a thread pool management method, apparatus, and electronic device that overcomes or at least partially solves the above problems.
[0006] In a first aspect, embodiments of the present invention provide a method for managing a thread pool, the method comprising:
[0007] Upon detecting the first task, the first thread pool for executing the second task is determined from the thread pool call chain, wherein the thread pool call chain is a call chain composed of multiple thread pools, and the first task is a subtask submitted during the execution of the second task;
[0008] The first task is executed by the second thread pool in the thread pool call chain, wherein the second thread pool is located at the first position in the thread pool call chain after the first thread pool is located at the second position in the thread pool call chain.
[0009] Optionally, the first thread pool and the second thread pool are adjacent in the thread pool call chain.
[0010] Optionally, the method further includes:
[0011] If no thread pool exists after the first thread pool in the thread pool call chain, a new thread pool is created and added to the thread pool call chain.
[0012] Optionally, after determining the first thread pool for executing the second task from the thread pool call chain, the method further includes:
[0013] If the number of active threads in the second thread pool equals the maximum number of threads, then an execution failure message is sent to the second task.
[0014] Optionally, after reporting the execution failure information to the second task, the method further includes:
[0015] The second thread pool records the execution failure message;
[0016] Periodically check the execution failure messages recorded by each thread pool in the thread pool call chain;
[0017] If an execution failure message is detected for the second thread pool in the previous cycle, the maximum number of threads in the second thread pool is increased.
[0018] Optionally, the method further includes:
[0019] Periodically check the running status of each thread pool in the thread pool call chain;
[0020] If the duration for which the number of active threads in the target thread pool is less than the maximum number of threads exceeds a duration threshold, the lifespan of the target thread pool is shortened, wherein the target thread pool is a thread pool in the thread pool call chain.
[0021] Optionally, after periodically detecting the running status of each thread pool in the thread pool call chain, the method further includes:
[0022] If the duration for which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed the duration threshold, the lifespan of the target thread pool is extended.
[0023] Secondly, embodiments of the present invention also provide a thread pool management device, the device comprising:
[0024] The management module is used to determine the first thread pool to execute the second task from the thread pool call chain when the first task is detected, wherein the thread pool call chain is a call chain composed of multiple thread pools, and the first task is a subtask submitted during the execution of the second task;
[0025] The selection module is used to select the second thread pool in the thread pool call chain to execute the first task, wherein the second thread pool is in the first position in the thread pool call chain after the first thread pool is in the second position in the thread pool call chain.
[0026] Optionally, the first thread pool and the second thread pool are adjacent in the thread pool call chain.
[0027] Optionally, the device further includes:
[0028] A new module is added to the thread pool call chain when there is no thread pool after the first thread pool in the thread pool call chain.
[0029] Optionally, the device further includes:
[0030] The feedback module is used to provide execution failure information to the second task when the number of active threads in the second thread pool equals the maximum number of threads.
[0031] Optionally, the device further includes:
[0032] The recording module is used to record the execution failure message for the second thread pool;
[0033] The first detection module is used to periodically detect the execution failure messages recorded by each thread pool in the thread pool call chain.
[0034] The first adjustment module is used to increase the maximum number of threads in the second thread pool when an execution failure message is detected for the second thread pool in the previous cycle.
[0035] Optionally, the device further includes:
[0036] The second detection module is used to periodically detect the running status of each thread pool in the thread pool call chain;
[0037] The second adjustment module is used to shorten the lifespan of the target thread pool when the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a duration threshold, wherein the target thread pool is a thread pool in the thread pool call chain.
[0038] Optionally, the device further includes:
[0039] The third adjustment module is used to extend the lifespan of the target thread pool if the duration during which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed a duration threshold.
[0040] Thirdly, embodiments of the present invention also provide an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps in the thread pool management method described above.
[0041] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in the thread pool management method described above.
[0042] In this embodiment of the invention, during the execution of the second task, if the first task, which is a subtask of the second task, is detected, the first thread pool for executing the second task is first determined from the thread pool call chain. Then, the second thread pool following the first thread pool is selected from the thread pool call chain to execute the first task. This ensures that the first and second tasks are processed by different thread pools, resolving the deadlock problem caused by parent and child tasks competing for thread resources in the same thread pool. Furthermore, using a chained thread pool call chain for thread pool management facilitates the determination of the thread pool for each task and the selection of the thread pool required for executing subtasks, achieving automated thread pool management. Attached Figure Description
[0043] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0044] Figure 1 is a flowchart of the steps of a thread pool management method provided in an embodiment of the present invention;
[0045] Figure 2 is a schematic diagram of the application architecture of a thread pool management method provided in an embodiment of the present invention;
[0046] Figure 3 is a structural block diagram of a thread pool management device provided in an embodiment of the present invention;
[0047] Figure 4 is a structural block diagram of the electronic device provided in an embodiment of the present invention. Detailed Implementation
[0048] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0049] It should be understood that the phrase "one embodiment" or "an embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of the invention. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification do not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments.
[0050] In various embodiments of the present invention, it should be understood that the sequence number of each process described below does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0051] Referring to Figure 1, this embodiment of the invention provides a method for managing a thread pool, which may include:
[0052] Step 101: If the first task is detected, determine the first thread pool to execute the second task from the thread pool call chain.
[0053] In this step, the thread pool call chain is a chain of call pools composed of multiple thread pools. It can be understood as a single-chain structure of call pools pre-created with a certain number of thread pools. Each node in the thread pool call chain represents a thread pool, and the number of nodes can be fixed or dynamic; no limitation is made here. For example, the thread pool call chain can include 10 thread pools, and this number will not change during task execution. Alternatively, the thread pool call chain can include 10 pre-created thread pools, with the number of thread pools gradually increasing during task execution. It is understandable that there is a certain order between the thread pools in the thread pool call chain. For example, if three consecutive nodes in the thread pool call chain are thread pool A, thread pool B, and thread pool C, then the next thread pool after thread pool A in the thread pool call chain is thread pool B, and the next thread pool after thread pool B in the thread pool call chain is thread pool C.
[0054] The first task is a subtask submitted during the execution of the second task. Therefore, the first and second tasks can be referred to as parent and child tasks, meaning the first task is a subtask of the second task, and the second task is the parent task of the first task. In other words, the execution result information of the first task is needed during the execution of the second task. It is worth noting that each submitted task has context information representing its runtime environment. This context information can determine which thread pool in the thread pool call chain it comes from, or whether it comes from the main thread.
[0055] Step 102: Select the second thread pool in the thread pool call chain to execute the first task.
[0056] It should be noted that the second thread pool is positioned first in the thread pool call chain after the first thread pool is positioned second in the thread pool call chain. In other words, the second thread pool is any thread pool that follows the first thread pool. This avoids parent and child tasks being in the same thread pool. Furthermore, since the number of tasks with dependencies may be three or more, to prevent dependent tasks from being in the same thread pool, the thread pool for executing a subtask is selected after the thread pool executing its parent task each time a subtask is chosen. For example, task A's subtask includes task B, and task B's subtask includes task C, thus tasks A, B, and C are all paired. Assume the thread pool call chain includes three consecutive thread pools: thread pool E, thread pool F, and thread pool G. If thread pool E is determined to execute task A, thread pool F (following thread pool E) can be chosen to execute task B, and thread pool G (following thread pool F) can be chosen to execute task C. This ensures that no two dependent tasks are in the same thread pool, avoiding deadlock. Also, when selecting a thread pool for each task, only the thread pool executing its parent task needs to be considered.
[0057] In this embodiment of the invention, during the execution of the second task, if the first task, which is a subtask of the second task, is detected, the first thread pool for executing the second task is first determined from the thread pool call chain. Then, the second thread pool following the first thread pool is selected from the thread pool call chain to execute the first task. This ensures that the first and second tasks are processed by different thread pools, resolving the deadlock problem caused by parent and child tasks competing for thread resources in the same thread pool. Furthermore, using a chained thread pool call chain for thread pool management facilitates the determination of the thread pool for each task and the selection of the thread pool required for executing subtasks, achieving automated thread pool management.
[0058] Optionally, the first thread pool and the second thread pool are adjacent in the thread pool call chain.
[0059] It should be noted that the second thread pool is the thread pool following the first thread pool in the thread pool call chain. Understandably, the number of tasks with dependencies may be three or more.
[0060] Ensure that three or more dependent tasks are handled by different thread pools, while maintaining an excessively long thread pool call chain. When selecting a thread pool for each task, focus only on the thread pool executing its parent task and choose the next thread pool after that. Other thread pools after the parent task's thread pool can be reserved for the child tasks' child tasks.
[0061] In this embodiment of the invention, when selecting a thread pool for each subtask, the next thread pool after the thread pool that executes its parent task will be selected, thereby avoiding excessively long thread pool call chains.
[0062] 0. Optionally, if there is no thread pool after the first thread pool in the thread pool call chain, create a new thread pool and add it to the thread pool call chain.
[0063] It should be noted that the number of thread pools following the first thread pool in the thread pool call chain may be zero, one, or more. In other words, there may be instances where there are no thread pools following the first thread pool in the thread pool call chain.
[0064] In the case of a thread pool, the thread pool call chain can be dynamically adjusted by creating a new thread pool and adding it to the end of the thread pool call chain, thus generating a new thread pool call chain for subsequent use.
[0065] In this embodiment of the invention, when there are not enough thread pools in the thread pool call chain, new thread pools can be created to dynamically adjust the thread pool call chain, thereby ensuring that there are enough thread pools available.
[0066] 0. Optionally, after determining the first thread pool to execute the second task from the thread pool call chain, the...
[0067] The method also includes:
[0068] If the number of active threads in the second thread pool equals the maximum number of threads, then send an execution failure message to the second task.
[0069] It should be noted that the maximum number of threads indicates the maximum number of threads the thread pool can accommodate. The maximum of 5 threads serves as the upper limit for the number of threads in the thread pool and is a reference indicator of the thread pool's capacity. The number of active threads indicates the number of threads currently running in the thread pool; the remaining threads are idle threads. This can be used as a reference indicator of the program's workload. If the number of active threads in a thread pool equals the maximum number of threads, the thread pool is considered full and cannot process new tasks. New tasks can only be processed after the number of active threads in the thread pool falls below the maximum number of threads.
[0070] Since the first task is a subtask of the second task, the second task needs to use the execution result of the first task. Normally, the second task will wait for the execution result of the first task to complete and then use that result to continue execution. Here, because the second thread pool is full, it needs to wait for an idle thread to become available before executing the first task. Therefore, if the second task continues to wait for the execution result of the first task, it will have to wait for an indeterminate amount of time. In this case, a failure message can be sent directly to the second task, informing it that it no longer needs to wait for the execution result of the first task.
[0071] In this embodiment of the application, when the second thread pool is full, the failure information of the first task is directly fed back, thereby reducing the waiting time of the second task.
[0072] Optionally, after reporting the execution failure information to the second task, the method further includes:
[0073] For the second thread pool, execution failure messages are recorded;
[0074] Periodic checks are performed on the execution failure messages recorded by each thread in the thread pool call chain;
[0075] If an execution failure message is detected for the second thread pool in the previous cycle, increase the maximum number of threads in the second thread pool.
[0076] It should be noted that periodic checks can determine whether each thread pool in the thread pool call chain has recorded execution failure messages; and if execution failure messages are recorded, it can also determine in which period the execution failure message was recorded. The detection period can be any length, for example, 1 minute, but is not limited to this.
[0077] Understandably, if a failure message for the target thread pool is detected in the current cycle, it indicates that the target thread pool was full in the previous cycle, causing a certain task to not be executed. To reduce the occurrence of this situation, the maximum number of threads in the target thread pool can be increased. The target thread pool can be any thread pool in the thread pool call chain. Specifically, when increasing the maximum number of threads, a fixed amount can be added. If the situation still occurs after increasing the amount, the same amount is added again next time, thus gradually increasing the maximum number of threads and avoiding the problem of an excessively large maximum number of threads. Preferably, the fixed amount added can be two, three, or four, but is not limited to these. Preferably, a reasonable amount can also be determined based on the CPU utilization in the current system environment, and the maximum number of threads can be increased by this reasonable amount to avoid the problem of excessive CPU utilization after increasing the maximum number of threads.
[0078] In this embodiment of the invention, when the second thread pool is full and the execution of the first task is abandoned, the probability of subsequent tasks being executed can be increased by increasing the maximum number of threads in the second thread pool.
[0079] Optionally, the method further includes:
[0080] Periodically check the running status of each thread pool in the thread pool call chain;
[0081] If the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a time threshold, the lifespan of the target thread pool will be shortened. Here, the target thread pool is the thread pool in the thread pool call chain.
[0082] It should be noted that a separate daemon thread can be set up to periodically check the running status of each thread pool in the thread pool call chain. This daemon thread can also be used to shorten the lifespan of the target thread pool. Understandably, if the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a certain threshold, it indicates that there are idle threads in the target thread pool, and these idle threads have been present for an excessively long time. This duration threshold is a predetermined fixed duration, such as two minutes, three minutes, or five minutes, but not limited to these. This threshold is used to measure whether the duration of idle threads is excessive. Exceeding this threshold is considered an excessively long duration of idle threads. The lifespan of the target thread pool is the lifespan of its threads; that is, idle threads in the target thread pool are reclaimed every certain lifespan. When shortening the lifespan, it can be shortened by a small fixed duration each time, such as 10 seconds, 20 seconds, or 30 seconds, but not limited to these.
[0083] In this embodiment of the invention, when there are idle threads in the target thread pool and the idle threads persist for too long, the idle threads can be recycled as quickly as possible by shortening the lifespan of the target thread pool, thereby reducing the waste of thread resources.
[0084] Optionally, after periodically checking the running status of each thread pool in the thread pool call chain, the method further includes:
[0085] If the duration for which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed the duration threshold, extend the lifespan of the target thread pool.
[0086] It should be noted that if the number of active threads in the target thread pool is less than the maximum number of threads for a duration that does not exceed the duration threshold, it indicates that there are idle threads in the target thread pool, and the duration of these idle threads is not excessive. In this case, the thread resources of the target thread pool may be relatively scarce, leading to frequent creation of new threads. Extending the lifespan of the target thread pool can reduce the number of times idle threads are reclaimed, allowing them to survive longer for use. When extending the lifespan, a small fixed extension can be added each time, such as 10 seconds, 20 seconds, or 30 seconds, but it is not limited to these.
[0087] In this embodiment of the invention, the occurrence of excessive recycling of idle threads can be reduced, thereby avoiding the problem of wasting system resources due to the frequent creation of new threads.
[0088] As shown in Figure 2, this embodiment of the invention provides an application architecture diagram of a thread pool management method. It can provide an asynchronous interface method for external calls in the form of a Java thread pool, and sets up a set of wrapped Java thread pools (equivalent to the thread pool call chain in the above embodiment of the invention; only thread pool E and thread pool F are shown in Figure 2, but it is not limited to two thread pools) and a daemon thread G that monitors the operation of the thread pools.
[0089] The main thread submits program A (equivalent to the second task in the above embodiment) through an external calling program. Using the current context information, program A is assigned to thread pool E. After program A submits subroutines C1 and C2 (equivalent to the first task in the above embodiment), subroutines C1 and C2 are assigned to the next thread pool (thread pool F) of thread pool E based on the current context information to run. During this process, the context information contains the program's runtime environment, allowing the determination of whether the submitted program was submitted from the external main thread or from a specific thread pool.
[0090] If thread pool F does not exist, a new thread pool will be created and included in the thread pool call chain. Before running subroutines C1 and C2, if there are insufficient available thread resources, thread pool F will record an instance of insufficient resources and immediately return the result of the failure of subroutines C1 and C2 to program A.
[0091] The daemon thread G runs a program at fixed intervals to check each step of the thread pool's call chain.
[0092] The thread pool's operational status. If the maximum number of threads exceeds the number of active threads for more than X minutes, reduce the thread lifetime to automatically recycle threads; otherwise, slightly extend the thread lifetime to avoid...
[0093] After too many threads are recycled, new threads are frequently created due to the scarcity of thread resources, wasting system resources.
[0094] X can be a predetermined, relatively small value. If a record of a thread pool reaching full capacity and abandoning program execution is found in the previous cycle, the maximum number of threads in the thread pool is increased.
[0095] In this embodiment of the invention, the response speed to user access can be improved, resulting in a smoother user experience; manual intervention can be reduced, lowering the management cost of the thread pool. Through relatively universal and automated thread expansion and contraction, program execution efficiency and hardware resource utilization can be improved.
[0096] The above describes the thread pool management method provided by the embodiments of the present invention. The thread pool management device provided by the embodiments of the present invention will be described below with reference to the accompanying drawings.
[0097] As shown in Figure 3, this embodiment of the invention provides a thread pool management device, which includes: a management module 31, used to determine the first thread pool that executes the second task from the thread pool call chain when a first task is detected, wherein the thread pool call chain is a call chain composed of multiple thread pools, and the first task is a subtask submitted during the execution of the second task;
[0098] Selection module 32 is used to select the second thread pool in the thread pool call chain to execute the first task.
[0099] In the process, the first position of the second thread pool in the thread pool call chain is after the second position of the first thread pool in the thread pool call chain.
[0100] Optionally, the first thread pool and the second thread pool are adjacent in the thread pool call chain.
[0101] Optionally, the device further includes:
[0102] A new module has been added to create a new thread pool and add it to the thread pool call chain if no thread pool exists after the first thread pool in the thread pool call chain.
[0103] 5. Optionally, the device further includes:
[0104] The feedback module is used to send execution failure information to the second task when the number of active threads in the second thread pool equals the maximum number of threads.
[0105] Optionally, the device further includes:
[0106] The logging module is used to log execution failure messages for the second thread pool;
[0107] The first detection module is used to periodically detect the execution failure messages recorded by each thread pool in the thread pool call chain;
[0108] The first adjustment module is used to increase the maximum number of threads in the second thread pool if an execution failure message is detected for the second thread pool in the previous cycle.
[0109] Optionally, the device further includes:
[0110] The second detection module is used to periodically detect the running status of each thread pool in the thread pool call chain;
[0111] The second adjustment module is used to shorten the lifespan of the target thread pool when the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a duration threshold. Here, the target thread pool is a thread pool in the thread pool call chain.
[0112] Optionally, the device further includes:
[0113] The third adjustment module is used to extend the lifespan of the target thread pool if the duration during which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed the duration threshold.
[0114] In this embodiment of the invention, during the execution of the second task, if the first task, which is a subtask of the second task, is detected, the first thread pool for executing the second task is first determined from the thread pool call chain. Then, the second thread pool following the first thread pool is selected from the thread pool call chain to execute the first task. This ensures that the first and second tasks are processed by different thread pools, resolving the deadlock problem caused by parent and child tasks competing for thread resources in the same thread pool. Furthermore, using a chained thread pool call chain for thread pool management facilitates the determination of the thread pool for each task and the selection of the thread pool required for executing subtasks, achieving automated thread pool management.
[0115] The thread pool management device provided in this application embodiment can implement the various processes implemented in the method embodiments of Figures 1 and 2, and achieve the same technical effect. To avoid repetition, it will not be described again here.
[0116] On the other hand, embodiments of the present invention also provide an electronic device, including a memory, a processor, a bus, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps in the above-described thread pool management method.
[0117] For example, Figure 4 shows a schematic diagram of the physical structure of an electronic device.
[0118] As shown in Figure 4, the electronic device may include a processor 410, a communication interface 420, a memory 430, and a communication bus 440. The processor 410, communication interface 420, and memory 430 communicate with each other via the communication bus 440. The processor 410 can call logical instructions from the memory 430 to execute the following methods:
[0119] If the first task is detected, the first thread pool to execute the second task is determined from the thread pool call chain, where the thread pool call chain is a call chain composed of multiple thread pools, and the first task is a subtask submitted during the execution of the second task.
[0120] The second thread pool in the thread pool call chain is selected to execute the first task, wherein the first position of the second thread pool in the thread pool call chain is after the second position of the first thread pool in the thread pool call chain.
[0121] Furthermore, the logical instructions in the aforementioned memory 430 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, essentially, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0122] Furthermore, embodiments of the present invention also provide a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the thread pool management methods provided in the above embodiments, including, for example:
[0123] If the first task is detected, the first thread pool to execute the second task is determined from the thread pool call chain, where the thread pool call chain is a call chain composed of multiple thread pools, and the first task is a subtask submitted during the execution of the second task.
[0124] The second thread pool in the thread pool call chain is selected to execute the first task, wherein the first position of the second thread pool in the thread pool call chain is after the second position of the first thread pool in the thread pool call chain.
[0125] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0126] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0127] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for managing a thread pool, characterized in that, The method includes: upon detecting a first task, determining a first thread pool from a thread pool call chain to execute a second task, wherein the thread pool call chain is a single-chain call chain composed of a certain number of pre-created thread pools, and the first task is a subtask submitted during the execution of the second task; each task has context information characterizing its runtime environment, the context information being used to determine the thread pool or main thread corresponding to the task in the thread pool call chain; selecting a second thread pool from the thread pool call chain to execute the first task, wherein the second thread pool is located at a first position in the thread pool call chain after the first thread pool is located at a second position in the thread pool call chain; the first thread pool and the second thread pool are adjacent in the thread pool call chain; if no thread pool follows the first thread pool in the thread pool call chain, creating a new thread pool and adding it to the thread pool call chain; wherein the method further includes: periodically detecting the running status of each thread pool in the thread pool call chain; and shortening the lifespan of the target thread pool if the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a duration threshold, wherein the target thread pool is a thread pool in the thread pool call chain.
2. The method according to claim 1, characterized in that, After determining the first thread pool for executing the second task from the thread pool call chain, the method further includes: if the number of active threads in the second thread pool is equal to the maximum number of threads, feeding back execution failure information to the second task.
3. The method according to claim 2, characterized in that, After feeding back the execution failure information to the second task, the method further includes: recording the execution failure information for the second thread pool; periodically detecting the execution failure information recorded for each thread pool in the thread pool call chain; and increasing the maximum number of threads in the second thread pool if the execution failure information was detected for the second thread pool in the previous period.
4. The method according to claim 1, characterized in that, After periodically detecting the running status of each thread pool in the thread pool call chain, the method further includes: extending the lifespan of the target thread pool if the duration for which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed a duration threshold.
5. A thread pool management device, characterized in that, The device includes: a management module, configured to, upon detecting a first task, determine a first thread pool from a thread pool call chain to execute a second task, wherein the thread pool call chain is a single-chain call chain composed of a pre-created number of thread pools, and the first task is a subtask submitted during the execution of the second task; each task has context information characterizing its runtime environment, the context information being used to determine the thread pool or main thread corresponding to the task in the thread pool call chain; and a selection module, configured to select a second thread pool from the thread pool call chain to execute the first task, wherein the second thread pool is located at a first position in the thread pool call chain within the first thread pool call chain. The first thread pool and the second thread pool are adjacent in the thread pool call chain; a new module is used to create a new thread pool and add it to the thread pool call chain when there is no thread pool after the first thread pool in the thread pool call chain; wherein, the device further includes: a second detection module, used to periodically detect the running status of each thread pool in the thread pool call chain; a second adjustment module, used to shorten the survival time of the target thread pool when the number of active threads in the target thread pool is less than the maximum number of threads for a duration exceeding a duration threshold, wherein the target thread pool is a thread pool in the thread pool call chain.
6. The apparatus according to claim 5, characterized in that, The device further includes a feedback module, used to provide execution failure information to the second task when the number of active threads in the second thread pool is equal to the maximum number of threads.
7. The apparatus according to claim 6, characterized in that, The device further includes: a recording module for recording the execution failure information for the second thread pool; a first detection module for periodically detecting the execution failure information recorded for each thread pool in the thread pool call chain; and a first adjustment module for increasing the maximum number of threads in the second thread pool when the execution failure information recorded for the second thread pool in the previous period is detected.
8. The apparatus according to claim 5, characterized in that, The device further includes: a second detection module, used to periodically detect the running status of each thread pool in the thread pool call chain; and a second adjustment module, used to shorten the lifespan of the target thread pool when the duration for which the number of active threads in the target thread pool is less than the maximum number of threads exceeds a duration threshold, wherein the target thread pool is a thread pool in the thread pool call chain.
9. The apparatus according to claim 5, characterized in that, The device further includes a third adjustment module, used to extend the lifespan of the target thread pool when the duration for which the number of active threads in the target thread pool is less than the maximum number of threads does not exceed a duration threshold.
10. An electronic device comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, characterized in that, When the computer program is executed by the processor, it implements the steps of the thread pool management method as described in any one of claims 1 to 4.
11. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, which, when executed by a processor, implements the steps of the thread pool management method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Heterogeneous database migration and data comparison method and device, equipment and storage medium
CN114490581A
Task execution using multiple pools of processing threads, each pool dedicated to execute different types of sub-tasks
US8209702B1