A thread pool management method, electronic equipment and software product
By matching thread pools based on task tags and types in task and thread pool management, the problem of resource waste caused by task and thread coupling is solved, and efficient task execution and resource utilization are achieved.
Patent Information
- Application Number
- CN202511114894.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-11
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-08-11
AI Technical Summary
The coupling relationship between tasks and threads in the existing technology leads to a mismatch between processing requirements and resource usage, resulting in reduced execution efficiency and waste of resources.
By matching IO-intensive, CPU-intensive, or mixed thread pools based on task tags and confirming the target thread execution method according to the task type, refined management of tasks and threads is achieved, including task queue management, thread expansion and recycling, task cancellation and other mechanisms.
It improves task execution efficiency, reduces resource waste, and realizes flexible management of tasks and threads and efficient utilization of resources.
Smart Images

Figure CN120596286B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of data computing, in particular to a thread pool management method, an electronic device and a software product. BACKGROUND
[0002] In the prior art, when an application program calls a thread based on business requirements, a corresponding task is first generated based on business requirements, and then a thread in a thread pool is matched for the task to execute the task.
[0003] However, in the prior art, once the task and the thread are matched, the relationship between the task and the thread is coupled unless the task is executed. However, different tasks have great differences in business scenarios and resource requirements. Such a coupled relationship between the task and the thread leads to an unreasonable match between processing requirements and resource usage. Moreover, the implementation manner of the prior art cannot realize fine management of the task and the thread, thereby reducing execution efficiency and wasting resources. SUMMARY
[0004] The purpose of the present application is to provide a thread pool management method, an electronic device and a software product for improving the management accuracy of tasks and threads, thereby improving execution efficiency and reducing resource waste.
[0005] To achieve the above purpose, the technical solutions adopted by the embodiments of the present application are as follows:
[0006] In a first aspect, the embodiments of the present application provide a task and thread pool management method, comprising:
[0007] Based on the task label of the new task, a target thread pool corresponding to the task label is matched; the target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool or a hybrid thread pool;
[0008] According to the task type of the new task, a target thread execution mode corresponding to the new task is confirmed, and the target thread execution mode is one of timed execution, counted execution or ordinary execution;
[0009] The corresponding worker thread executes the new task according to the target thread execution mode.
[0010] Optionally, before the step of matching the target thread pool corresponding to the task label based on the task label of the new task, the method further comprises:
[0011] According to the processing mode information of the new task, it is judged that the processing mode adopted by the new task is addition or replacement;
[0012] If it is addition, the new task is added to a task queue.
[0013] If the replacement, the new task replaces the task to be replaced in the task queue.
[0014] Optionally, further comprising:
[0015] Judging whether there is a task backlog in the task queue;
[0016] If there is a task backlog, triggering thread expansion.
[0017] Optionally, further comprising:
[0018] Judging whether the task queue meets an idle condition; the idle condition refers to that the state of the task queue remains idle within a preset period;
[0019] If yes, recycling redundant threads; the redundant threads are threads created in the same batch in the corresponding target thread pool except the initial thread.
[0020] Optionally, further comprising:
[0021] When a cancellation indication is obtained through a cancellation interface, removing the corresponding unexecuted task in the task queue according to the task number in the cancellation indication;
[0022] When a batch cancellation indication is obtained through the cancellation interface, removing a plurality of corresponding unexecuted tasks in the task queue according to the batch cancellation indication.
[0023] Optionally, further comprising:
[0024] Timing checking a termination identifier;
[0025] If the termination identifier exists, terminating the executing task in the task queue corresponding to the termination identifier.
[0026] Optionally, after the step of terminating the executing task in the task queue corresponding to the termination identifier, further comprising:
[0027] If the executing task corresponding worker thread does not respond, isolating the worker thread of the executing task to an independent thread group for termination.
[0028] Optionally, the step of executing the new task by the corresponding worker thread according to the target thread execution mode comprises:
[0029] If the target thread execution mode is the timing execution, the new task is executed by the corresponding worker thread at a preset time interval;
[0030] If the target thread execution mode is the counting execution, the new task is executed by a corresponding worker thread, and the execution count is decremented until the worker thread is decremented to 0.
[0031] If the target thread execution mode is the normal execution, the new task is executed by a corresponding worker thread.
[0032] In a second aspect, an embodiment of the present application provides a task and thread pool management apparatus, comprising:
[0033] A task queue manager is configured to match a target thread pool corresponding to a task tag of a new task based on the task tag; the target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool, or a hybrid thread pool.
[0034] The target thread pool is configured to determine a target thread execution mode of the new task according to a task type of the new task, the target thread execution mode being one of a timing execution, a counting execution, or a normal execution; and a corresponding worker thread executes the new task according to the target thread execution mode.
[0035] In a third aspect, an embodiment of the present application provides an electronic device, comprising:
[0036] A memory is configured to store one or more programs.
[0037] A processor.
[0038] When the one or more programs are executed by the processor, a method according to any one of the first aspect is implemented.
[0039] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement a method according to any one of the first aspect.
[0040] In a fifth aspect, an embodiment of the present application provides a program product, and the program product is executed by a processor to implement a method according to any one of the first aspect.
[0041] Compared with the prior art, the thread pool management method, the electronic device and the software product provided by the embodiment of the application match the target thread pool corresponding to the task label of the new task, and the target thread pool is one of an IO intensive thread pool, a CPU intensive thread pool or a hybrid thread pool. Then, in the corresponding target thread pool, the corresponding working thread is matched for the new task, and based on the target thread execution mode corresponding to the new task, such as one of timing execution, counting execution or ordinary execution, the corresponding new task is executed in a suitable execution mode. Since the type of the thread pool and the processing method of the working thread can be flexibly configured based on the characteristics of the task, the management of the task and the working thread is more precise, thereby improving the efficiency of task execution, and since the working thread and the execution mode are more flexible, the waste of resources during idling is reduced.
[0042] In order to make the above-mentioned purposes, features and advantages of the application more obvious and easy to understand, the following preferred embodiments are specifically described below, and the accompanying drawings are described in detail as follows. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions of the embodiments of the application, the following will briefly introduce the drawings needed to be used in the embodiments. It should be understood that the following drawings only show some embodiments of the application, and therefore should not be regarded as a limitation to the scope. For those skilled in the art, other related drawings can also be obtained without creative labor on the basis of these drawings.
[0044] Figure 1 A schematic diagram of a thread pool management mechanism in the prior art;
[0045] Figure 2 A flowchart of a task and thread pool management method provided by the embodiment of the application;
[0046] Figure 3 A flowchart of another task and thread pool management method provided by the embodiment of the application;
[0047] Figure 4 A flowchart of another task and thread pool management method provided by the embodiment of the application;
[0048] Figure 5 A flowchart of another task and thread pool management method provided by the embodiment of the application;
[0049] Figure 6 A flowchart of another task and thread pool management method provided by the embodiment of the application;
[0050] Figure 7A task and thread pool management device provided for an embodiment of the present application;
[0051] Figure 8 A signaling interaction schematic diagram of task and thread pool management provided for an embodiment of the present application;
[0052] Figure 9 A schematic diagram of an interactive interface of a calling party provided for an embodiment of the present application;
[0053] Figure 10 A schematic diagram of an interactive interface of a calling party provided for an embodiment of the present application;
[0054] Figure 11 A schematic diagram of an interactive interface of a calling party provided for an embodiment of the present application;
[0055] Figure 12 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0056] Figure 13 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0057] Figure 14 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0058] Figure 15 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0059] Figure 16 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0060] Figure 17 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0061] Figure 18 Another task and thread pool management architecture schematic diagram provided for an embodiment of the present application;
[0062] Figure 19 Another task and thread pool management device schematic diagram provided for an embodiment of the present application;
[0063] Figure 20 Another task and thread pool management device schematic diagram provided for an embodiment of the present application;
[0064] Figure 21 A structural schematic diagram of an electronic device provided for an embodiment of the present application. DETAILED DESCRIPTION
[0065] In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the following will be combined with the accompanying drawings for the embodiments of the present application to make a clear and complete description of the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments. The components of the embodiments of the present application described and shown in the accompanying drawings can be arranged and designed in various different configurations.
[0066] Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the claimed present application, but only represents selected embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work are within the scope of protection of the present application.
[0067] It should be noted that similar reference numerals and letters in the following drawings represent similar items, and therefore, once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. Meanwhile, in the description of the present application, the terms "first", "second", and the like are only used to distinguish the description, and cannot be understood as indicating or implying relative importance.
[0068] It should be noted that, in this document, the relationship terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between the entities or operations. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or device. Without more limitations, the element defined by the statement "including a" does not exclude the presence of another identical element in the process, method, article or device including the element.
[0069] In the prior art, a thread pool can be understood as a producer-consumer model, for example Figure 1 A schematic diagram of a thread pool management mechanism in the prior art, which is composed of a producer, a task queue, and a thread pool (consumer). The asynchronous decoupling is performed through the task queue, that is, the process of creating a task by the producer and putting the task into the task list is asynchronous and decoupled from the process of processing the task by the thread pool through the working thread.
[0070] In the existing thread pool, the number of existing threads in the thread pool is generally preset when the thread pool is initialized, for example, the number of threads = 2*CPU
number of cores
[0071] Continuing to refer toFigure 1 , producer once put the task into the task queue, even if the current task has not been executed, can not cancel the task in the current queue, or the worker thread in the execution process, the task in the execution process cannot be interrupted, and the worker thread can return only after the work is completed.
[0072] Obviously, after the task is matched to the corresponding worker thread, the relationship between the two is still coupled, and fine management of the task and the thread cannot be achieved, thereby causing the execution efficiency to be reduced and resources to be wasted.
[0073] Therefore, in order to solve the above problems, the application provides a mechanism for dynamically managing task and thread pool, which divides tasks into different types of tasks based on the type of application, and then matches each task to a corresponding thread pool. Optionally, Figure 2 A flowchart of a task and thread pool management method provided by an embodiment of the application is shown in Figure 2 The method comprises the following steps:
[0074] Step 105, based on the task label of the new task, matching the target thread pool corresponding to the task label.
[0075] Among them, the target thread pool is one of the IO-intensive thread pool, the CPU-intensive thread pool or the hybrid thread pool.
[0076] Specifically, for the IO-intensive thread pool, it is used to match tasks with IO-intensive task labels. Optionally, the initial number of threads of the IO-intensive thread pool is 2N+1, where N is the number of CPU cores. Since the tasks corresponding to the worker threads in the IO-intensive thread pool need to be executed based on user interaction, i.e. need to wait for IO interaction. Therefore, in order to make full use of the idle time and resources waiting for IO, for a CPU core, as many worker threads as possible are configured. That is, the initial number of threads is 2N+1. Since the proportion of time that the CPU core executes this type of worker thread is not high, the CPU core can be configured to wait or do some light preparation (for example, thread switching), so as to make full use of the idle time and resources.
[0077] Specifically, for the CPU-intensive thread pool, it is used to match tasks with CPU-intensive task labels. Optionally, the initial number of threads of the CPU-intensive thread pool is N. Since the tasks corresponding to the worker threads in the CPU-intensive thread pool need to continuously occupy computing resources for execution, in order to avoid unnecessary thread switching, the initial number of threads can be set to equal the number of CPU cores, thereby improving the efficiency of each CPU core and reducing congestion.
[0078] Specifically, for the hybrid thread pool, it is used to match tasks with a hybrid task label. Optionally, the initial number of threads of the hybrid thread pool is N. As known from the foregoing, in order to take into account both IO-intensive and CPU-intensive tasks, the initial number of threads of the two is set between the values to take into account both the computing resources and the blocking waiting scenarios.
[0079] It should be noted that for the various thread pools described above, one or a combination thereof can be used in the present application based on the needs of the business scenario, which is not limited here.
[0080] Continuing to refer to Figure 2 , step 106, according to the task type of the new task, confirming the target thread execution mode corresponding to the new task.
[0081] In the present application, based on the execution requirements of the task, the corresponding target thread execution mode can be further divided into one of the following: timed execution, counted execution, or ordinary execution.
[0082] Continuing to refer to Figure 2 , step 107, the corresponding worker thread executes the new task according to the target thread execution mode.
[0083] When the task type of the new task is timed execution, the target thread execution mode can be: based on the preset interval time, the new task is executed at regular intervals; when the task type of the new task is counted execution, the new task is executed for a preset number of times, and then the execution of the new task is ended, and the corresponding target thread is placed in an idle state; when the task type of the new task is ordinary execution, the new task is directly executed by the corresponding target thread.
[0084] For example, in a financial scenario, if a user initiates a "stock buy" request through a client, the "stock buy" can be understood as an ordinary task by the business system. That is, the corresponding worker thread executes the "stock buy" at one time.
[0085] In another example, if a user initiates a "market refresh" or "report query" request through a client, such requests can be understood as timed execution because they require the business system to query at regular intervals. For example, the "market refresh" updates the real-time stock price every 3 seconds; the "report query" refreshes the user's position statistics report every 10 seconds.
[0086] In another example, if the business system initiates a "signature verification" request to the user through the client, for example, batch signature verification, every 100 transactions are digitally signed, and such a request can be understood as counted execution. That is, "signature verification" is executed once every 100 transactions.
[0087] The task and thread pool management method provided by the embodiments of the present application matches the target thread pool corresponding to the task tag of the new task according to the task tag, and the target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool, or a hybrid thread pool. Then, in the corresponding target thread pool, the corresponding worker thread is matched for the new task, and based on the target thread execution mode corresponding to the new task, such as one of timing execution, count execution, or normal execution, the corresponding new task is executed in a suitable execution mode. Since the type of the thread pool and the processing method of the worker thread can be flexibly configured based on the characteristics of the task, the management of the task and the worker thread is more precise, thereby improving the efficiency of task execution. At the same time, since the worker thread and the execution mode are more flexible, the waste of resources during idling is reduced.
[0088] Optionally, for step 106, three possible implementation manners are provided as follows:
[0089] Manner one: if the target thread execution mode is timing execution, the new task is executed by the corresponding worker thread at a preset time interval.
[0090] Manner two: if the target thread execution mode is count execution, the new task is executed by the corresponding worker thread, and the execution count is decremented after the execution is completed, until the worker thread is decremented to 0.
[0091] Manner three: if the target thread execution mode is normal execution, the new task is executed by the corresponding worker thread.
[0092] In some scenarios, when a new task is created, such as the new task in the above example, based on the scenario requirements, it may have a processing relationship with other tasks already existing in the task queue, for example, the new task can replace the previous other tasks, or the new task has no processing logic relationship with other tasks, etc. Therefore, before step 105 is executed, the processing mode of the new task can be confirmed first. Specifically, based on the above, Figure 2 Figure 3 Another flowchart of the task and thread pool management method provided by the embodiments of the present application is shown in FIG. 6. Referring to FIG. 6, Figure 3 before step 105, it further includes:
[0093] Step 102: according to the processing mode information of the new task, it is judged that the processing mode of the new task is addition or replacement.
[0094] Specifically, if the processing mode information is awptAsynNormal, the meaning is "add", and step 103 is performed; if the processing mode information is awptAsynReplace, the meaning is "replace", and step 104 is performed.
[0095] Step 103, adding the new task to the task queue.
[0096] Step 104, replacing the to-be-replaced task in the task queue with the new task.
[0097] An example is provided below to illustrate the replacement mode of step 104. Taking a financial scenario as an example, if a business system provides functions such as "bulk transaction available position" and "bond quotation platform instruction available position", a user selects different combination numbers on a client in a commission interface, and needs to obtain corresponding available positions according to the different combination numbers selected by the user. When a new combination number (new task) is selected, the last obtained available position request has expired, and therefore, the new "obtain position" task replaces the to-be-replaced task of the last "obtain position".
[0098] Optionally, in the prior art, the expansion of the thread pool often needs to use the expansion range of the number of threads to achieve, and such a range is set by pre-setting. However, based on the dynamic changes of the business scenario, for example, the queuing or backlog of the task queue, the thread pool cannot trigger the dynamic expansion. Therefore, the application also provides a mechanism for dynamically expanding threads. Specifically, based on the above, Figure 2 the application further provides another task and thread pool management method provided by the embodiment of the application, which is shown in Figure 4 The method further includes: Figure 4
[0099] Step 108, determining whether there is a task backlog in the task queue.
[0100] If there is a task backlog, step 109 is performed; if there is no task backlog, step 108 is continued.
[0101] Step 109, triggering thread expansion.
[0102] In a possible implementation, the "thread expansion" can be performed on one of the above-mentioned IO-intensive thread pool, CPU-intensive thread pool or hybrid thread pool.
[0103] Optionally, at least two possible implementation modes of thread expansion are provided below:
[0104] The first mode is based on task backlog detection in the task queue. When the length of the task queue continuously exceeds a threshold (for example, the length of the queue > the current number of threads x 2) and the duration reaches a window period (for example, 5 seconds), thread expansion is triggered.
[0105] Optionally, for the example of "the length of the queue > the current number of threads x 2", the length of the queue can be the length of a certain type of task in the task queue, for example, the length of IO-intensive tasks. Correspondingly, the current number of threads is the number of worker threads in the corresponding thread pool, for example, the number of worker threads in the IO-intensive thread pool.
[0106] The second mode is gradual expansion. The number of newly added threads is 50% of the current number of threads (at least one is added), so as to avoid instantaneous resource overload.
[0107] It should be noted that the above two modes can be executed separately or in combination. For the combined execution, one possible implementation is to first perform task backlog detection in "mode one". If the task backlog detection does not meet the expansion condition, "mode two" is executed. If the task backlog detection in mode one meets the expansion condition, thread expansion is triggered through "mode one", and "mode two" is not executed.
[0108] In addition, in order to avoid excessive occupation of resources, in one possible implementation, an upper limit of thread expansion can also be preset, such as a thread expansion upper limit threshold. If the total number of threads after expansion exceeds the threshold, no new thread is created.
[0109] Optionally, in order to facilitate the management of the thread pool in the future, such as dynamically recycling threads, in one possible implementation, the creation batch information of the expanded threads can also be maintained. For example, the threads created in the same batch of expansion have the same creation batch information. In the mechanism of recycling threads, the same batch of threads can be recycled in batches.
[0110] Optionally, in addition to the above thread expansion mechanism, the present application can also recycle the threads in the thread pool, so as to further improve the thread control accuracy and more reasonably utilize resources. Specifically, on the basis of Figure 2 , Figure 5 Another flowchart of a task and thread pool management method provided by the embodiment of the present application is shown in FIG. 10. As shown in FIG. 10, the method further includes the following steps. Figure 5
[0111] Step 110: determining whether the task queue meets the idle condition.
[0112] The idle condition refers to that the state of the task queue remains idle within a preset period. If the idle condition is met, step 111 is performed, otherwise, step 110 is continuously performed.
[0113] Step 111, recycling the redundant threads.
[0114] The redundant threads are threads created in the same batch as the initial thread in the corresponding target thread pool.
[0115] Optionally, for the thread recycling mechanism, the timing detection can be started after the thread pool is initialized. For example, if the task queue is empty for three consecutive detection periods, and the current number of threads exceeds the initial value, the redundant threads are gradually terminated according to the "newly created first, recycled last" principle. Optionally, the creation batch information of the threads can be used to recycle the redundant threads in the same batch. When there are multiple batches, the creation batch information can also be used to determine the order of creation. Moreover, the number of recycled threads should not be lower than the initial value, so as to avoid frequent capacity expansion and contraction jitter.
[0116] In a possible implementation, the recycling of the redundant threads can be across all thread pools, that is, in one thread recycling operation, the redundant threads in all thread pools are recycled.
[0117] In a possible implementation, the redundant threads can be working threads newly added through thread capacity expansion.
[0118] Optionally, for the tasks in the task queue that are not executed, the tasks can be canceled based on business requirements. The following provides several possible cancellation modes:
[0119] Mode one: When a cancellation indication is obtained through a cancellation interface, the corresponding unexecuted task in the task queue is removed according to the task number in the cancellation indication.
[0120] Optionally, the user can issue the cancellation indication through the "cancellation interface" of the API layer. The cancellation indication can include task number, task name, task label, task description and the like, so as to confirm the tasks that can be removed.
[0121] It should be noted that the task label and the task number can be two different parameters. In some business scenarios, the task number can be a simple abbreviation of the corresponding task name, which is mainly used for task cancellation, replacement, and visual display on the client side. The task number can uniquely identify a specific task.
[0122] The task label can be a special field in some scenarios, which can be customized by the calling party, for example, defining the task label to represent a certain type of task. The task label is used to indicate to which type of thread pool the corresponding task should be assigned, for example, matching the IO-intensive thread pool, the CPU-intensive thread pool or the hybrid thread pool according to the task label. The "task type" in the present application is used to identify the mode of the corresponding working thread, for example: timing execution, counting execution and ordinary execution.
[0123] Method 2: When a batch cancellation instruction is obtained through the cancellation interface, multiple unexecuted tasks corresponding to the batch cancellation instruction are removed from the task queue.
[0124] Method 1 provides a mechanism for removing a single unexecuted task, while method 2 provides a mechanism for removing multiple unexecuted tasks.
[0125] Optionally, the cancellation instruction may include a task number indicating which tasks need to be removed, or may be indicated using a task tag.
[0126] Optionally, tasks in the task queue can be terminated based on business needs. Figure 2 On the basis of Figure 6 A flow chart of another task and thread pool management method provided by an embodiment of the present invention is shown in FIG. Figure 6 , the method further comprises:
[0127] Step 112: Check the termination flag regularly.
[0128] Specifically, a termination signal can be sent to a worker thread, and the termination signal includes the termination flag of the task being executed. As can be seen, each worker thread can correspond to a termination flag, and the caller or external party can modify the termination flag to achieve termination control of a worker thread.
[0129] Optionally, if the termination mark exists, execute step 113; if not, continue to execute step 112.
[0130] Step 113: Terminate the executing task corresponding to the termination identifier in the task queue.
[0131] It should be noted that after the task is terminated, the worker thread executing the task in the corresponding thread pool will be terminated, thereby releasing the worker thread's resources.
[0132] The above-mentioned method for terminating an ongoing task can be understood as a collaborative termination mechanism. However, after executing steps 112 and 113, there may be a situation where the worker thread corresponding to the ongoing task does not respond to the termination operation. In this case, in order to ensure that the worker thread is terminated, this application also provides a forced termination mechanism. Specifically, after step 113, it also includes:
[0133] If the worker thread corresponding to the executing task does not respond, the worker thread is isolated into an independent thread group and terminated.
[0134] In order to realize the various steps of the above examples and the corresponding technical effects, the application also provides a task and thread pool management device, specifically, Figure 7 A task and thread pool management device is provided for the embodiments of the application, as shown in Figure 7 The device 20 comprises a caller 21, a task queue manager 22, a task queue 23, a thread pool manager 24, and a federal thread pool, which can include but is not limited to an IO-intensive thread pool 25a, a CPU-intensive thread pool 25b, and a hybrid thread pool 25c.
[0135] The caller 21 can be a functional module of a business system, which is used to interact with the operating system of the business system and can include one or more business functional modules. The business operation of a user is obtained through the business system, and then the corresponding task is generated based on the business operation, and then the task is matched with the corresponding worker thread through the task queue manager 24 and the thread pool manager 24, so as to execute the task.
[0136] In addition, based on the above example, the caller 21 can also remove the unexecuted task and the corresponding worker thread based on the user's business operation through the task queue manager 24 and the thread pool manager 24; or terminate the task and the corresponding worker thread in execution.
[0137] The task queue manager 22 is used to maintain the task queue 23, and then according to the task type of each new task, the target thread execution mode corresponding to the new task is confirmed; and the task queue manager 22 can also be used to detect whether the task queue is accumulated, so that the thread pool manager 24 expands the thread pool or recycles the redundant threads.
[0138] The thread pool manager 24 is used to maintain each thread pool in the federal thread pool, and based on the new task, the corresponding thread pool is matched, and then the worker thread is matched, so that the worker thread executes the new task according to the target thread execution mode.
[0139] In addition, the thread pool manager 24 can also be used to expand the thread pool or recycle the redundant threads.
[0140] Optionally, in combination with Figure 7 The following provides a signaling interaction example for realizing the various steps of the above examples of the application, specifically, Figure 8 A signaling interaction diagram for task and thread pool management is provided for the embodiments of the application, as shown in Figure 8 The signaling interaction includes:
[0141] Step 1, the thread pool manager completes initialization.
[0142] Specifically, the thread pool manager can configure thread attribute information of the working threads in each thread pool through a chain call interface. The thread attribute information includes but is not limited to: working thread priority, working thread label, and the like. The working thread priority is used to identify the priority of the corresponding working thread at the operating system level. The working thread label is used to reflect the creation batch of the corresponding working thread.
[0143] Step 2, the thread pool manager triggers thread pool initialization after completing initialization.
[0144] Specifically, the thread pool initialization includes but is not limited to: configuring the initial number of threads of the IO-intensive thread pool, the CPU-intensive thread pool, or the hybrid thread pool.
[0145] Optionally, when the thread pool is initialized, the binding of each working thread to a CPU core can be completed. Specifically, it can use thread affinity (Processor Affinity) to complete the binding of the working thread to the CPU core. By binding the working thread to the CPU core, the business scenarios with low latency, cache invalidation avoidance, or high real-time requirements can be applied.
[0146] Of course, the new working thread generated in the thread expansion can also use the corresponding interface of the API to realize the binding to the corresponding CPU core.
[0147] Optionally, after completing the thread pool initialization, the working thread enters a waiting state to wait for the wake-up of the new task mentioned above. At this time, for the thread pool manager, it can initialize a thread state list to record the thread state information of all threads in each thread pool. And, the key element items in the table are initialized.
[0148] In one possible implementation, the thread state information includes but is not limited to the following information:
[0149] Thread ID, current processing task, average processing time, average waiting time, processed task list, state (waiting, executing, terminating), creation batch, executing task, binding to CPU core, and the like. The executing task is embodied by a task object, which can include but is not limited to: task number, task name, and the like.
[0150] Among them, the "executing task" can be embodied by a corresponding "task object", for example, the task object can include: task number, task name, and the like.
[0151] Step 3, the thread pool manager updates thread statistical information.
[0152] Optionally, the thread statistics information includes, but is not limited to: the current processing task; the average processing time; the average waiting time; the processed task list; the state of the work thread: waiting, executing, terminating; the task in processing.
[0153] Step 4, the thread pool waits for the assignment of the task.
[0154] Step 5, the calling party makes a task call.
[0155] Step 6, the task queue manager generates a new task.
[0156] Optionally, the task queue manager generates the new task according to the name and description set by the calling party.
[0157] In a possible implementation, the task queue manager is further configured to maintain element information of the new task, and the element information of the new task includes, but is not limited to, the following information:
[0158] 1) name;
[0159] 2) task number;
[0160] 3) receiving processing completion message target object [a handle]: the default value is 0, indicating that it is not needed to receive;
[0161] 4) work line processing start time: updated when the task is started to be executed by the work thread;
[0162] 5) work line processing end time: updated when the task is ended to be executed by the work thread;
[0163] 6) queue joining time: updated when the task is added to the task queue;
[0164] 7) task priority: shaping (0-9 levels), and the default value is 0;
[0165] 8) task processing mode: addition or replacement;
[0166] 9) task label: externally input, which is carried when a work thread execution return is received (such as "urgent" and "batch"), and the role is to indicate the thread pool corresponding to the new task.
[0167] 10) task state: waiting for processing, processing, and processed.
[0168] Step 7, the task queue manager judges the processing mode of the new task to be addition or replacement.
[0169] Specifically, if it is addition, step 8 is executed; if it is replacement, step 9 is executed.
[0170] Step 8, the task queue manager adds the new task to the task queue.
[0171] Optionally, based on the task priority of the new task, it can be added before the task with lower task priority. When the task priority is the same, they are sorted in the order of generation time.
[0172] Step 9, the task queue manager replaces the to-be-replaced task in the task pair with the new task.
[0173] Optionally, the task queue manager can check whether there is a task with the same name as the new task in the task queue. If there is, the existing task is the to-be-replaced task.
[0174] Optionally, the judgment of the same name can be judged by whether the task number is repeated.
[0175] Step 10, the task queue manager initiates a new task arrival notification to wake up the worker thread in the corresponding thread pool.
[0176] Optionally, the arrival notification can be an event (Event) of the operating system bottom layer. As described above, after the initialization of each thread pool is completed, the worker thread enters the sleep state by waiting for a certain time, that is, the arrival notification. When the new task is initiated, the worker thread receives the time to realize the acquisition operation of the new task.
[0177] Step 11, the thread pool receives the arrival notification.
[0178] Step 12, the thread pool acquires the corresponding new task according to the arrival notification.
[0179] Optionally, the thread pool acquires the new task by acquiring the task object of the new task. As described above, the task object can include the task number, the task name, etc.
[0180] Step 13, the thread pool judges whether the acquisition of the new task is completed.
[0181] Specifically, if yes, step 14 is executed, and if no, step 4 is returned to continue waiting.
[0182] Optionally, in one possible implementation, whether the acquisition is completed can be judged by whether the task object obtained is empty. If the task object is not empty, it is confirmed that the acquisition is successful, and if it is empty, the acquisition is unsuccessful.
[0183] Step 14, confirm the task type of the thread pool.
[0184] Further, referring to the foregoing, the work thread is executed in a corresponding timing, count execution or normal execution, to complete the execution of the added task.
[0185] Step 15, after the work thread is woken up in step 10, the task queue manager starts to detect the task queue in a timing manner.
[0186] Optionally, the number of tasks in the task queue can be detected in a timing manner, so that step 16 confirms whether there is a task backlog.
[0187] Step 16, the task queue manager judges whether there is a task backlog in the task queue.
[0188] Specifically, if there is, step 17 is executed; if not, step 19 is executed.
[0189] Step 17, the task queue manager triggers the thread pool manager to expand the thread.
[0190] Step 18, the thread pool manager newly creates a work thread for the corresponding thread pool.
[0191] Optionally, the step 18 can newly create one or more work threads, and for multiple cases, the work threads belong to the same batch.
[0192] Step 19, the thread pool manager triggers the recovery of redundant threads.
[0193] It should be noted that in order to avoid the jitter of frequent expansion and recovery, in a possible implementation manner, the step 19 can be triggered only if the task backlog or the task queue is empty in multiple consecutive periods.
[0194] Step 20, based on some scene requirements, the calling party can trigger the task termination operation.
[0195] Step 21, after receiving the task termination requirement, the task queue manager first judges the task state of the task to be terminated: waiting, executing or terminating. If it is executing, step 22 is executed; if it is waiting, step 24 is executed.
[0196] Step 22, the thread pool manager removes the work thread corresponding to the task to be terminated, and returns to step 3 to update the thread statistical information.
[0197] Step 23, the thread pool manager completes the removal of the corresponding work thread in the corresponding thread pool.
[0198] Step 24, the task queue manager removes the corresponding task in the task queue.
[0199] In some scenarios, the invoker can obtain the task and the working condition of the worker thread through a state query. Continuing to refer to Figure 8 :
[0200] Step 25, the invoker performs a state query.
[0201] Step 26, the thread pool manager obtains corresponding thread statistical information based on the query request.
[0202] Optionally, the thread statistical information can be displayed through the interactive interface of the invoker. Specifically, Figure 9 For a schematic diagram of an interactive interface of an invoker provided by an embodiment of the present application, refer to Figure 9 When the user selects the "thread pool state" in the interactive interface, the lower display area thereof displays the conditions of each worker thread in the thread pool. Specifically, the thread state information shown in the above example can be displayed.
[0203] Among them, Figure 9 The "thread number" shown is the "thread ID" described above; Figure 9 The "state" shown can include the above-mentioned: waiting, executing, terminating, and average processing time, average waiting time, etc.
[0204] Refer to Figure 9 The corresponding "working" of each worker thread shown is "none", that is, there is no task being executed.
[0205] Refer to Figure 9 Among them, the "task priority" is used to display the priority of the task executed by each worker thread.
[0206] Optionally, Figure 10 For another schematic diagram of an interactive interface of an invoker provided by an embodiment of the present application, refer to Figure 10 When the user selects the "task list (to be processed)" in the interactive interface, the lower display area thereof displays the information of each task to be processed in the task list. For example, task number (number), task name (name), task state (state), task priority, and join time. The join time is the time when the task is added to the task queue. Among them, the start time indicates the time when the worker thread obtains the task; the end time indicates the time when the worker thread finishes executing the task.
[0207] Optionally, Figure 11 For another schematic diagram of an interactive interface of an invoker provided by an embodiment of the present application, refer to Figure 11When the user selects "Task List (Processed)" in the interactive interface, the lower display area displays information about each processed task in the task list. For example, the task ID is "CheckLogFile," and its name is "Clear Expired Logs." Its corresponding processing time is 0.02s, start time is 19:49:50, end time is 19:49:50, join time is 19:49:50, and priority is 0.
[0208] In order to achieve the various steps and corresponding technical effects of the above examples, this application also provides a task and thread pool management architecture. Specifically, Figure 7 On the basis of Figure 12 Another task and thread pool management architecture diagram provided by the embodiment of the present invention is shown in FIG. Figure 12 The task and thread pool management architecture includes: a caller 21, a task queue manager 22, a task queue 23, a thread pool manager 24, and a federal thread pool, which may include but is not limited to: an IO-intensive thread pool 25a, a CPU-intensive thread pool 25b, and a hybrid thread pool 25c.
[0209] The caller 21 may be a client for deploying a business system, and the business system may include a plurality of business function modules with different functions, such as Figure 12 In addition, since the caller 21 can also be used to provide an interactive interface for the business system, in order to obtain relevant information about the working threads and tasks for interaction and display, the caller 21 can also include a message receiving and sending module 210, which is used to interact with the task manager 22 and the thread pool manager 24 respectively to obtain relevant information about the tasks and working threads.
[0210] Optionally, in order to realize the interaction between the caller 21 and the task manager 22 and the thread pool manager 24, in one possible implementation method, the task and thread pool management architecture can also include an application programming interface (API) 26, so that when the caller 21 needs to manage and query tasks and working threads, the corresponding functions are completed through each interface.
[0211] Specifically, the API 26 includes but is not limited to: an add interface 260 , a cancel / replace interface 261 , a visual query interface 262 , and an underlying interface 263 .
[0212] The adding interface 260 is used to trigger the task manager 22 to generate a corresponding new task by transmitting the “task call” instruction of the caller 21 when a new task is added.
[0213] Cancel / replace interface 261, used to trigger the task manager 22 to remove the corresponding task through the cancel / replace interface 261 when the task needs to be canceled. Or, when a new task needs to replace a certain to-be-replaced task in the task queue 23, the cancel / replace interface 261 is used to trigger the task manager 22 to replace.
[0214] Visual query interface 262, used to interact with the task manager 22 and the message query module 240 in the thread pool manager 24 to obtain the relevant information of the work thread and the task.
[0215] Correspondingly, in order to be able to report the relevant information of the work thread to the message query module 240, for each work thread, a corresponding message queue can be matched, which is used to collect the processing results of the work thread, such as the state of task execution, the state of the work thread, the state of the task, etc.
[0216] It should be noted that since the message pair adopts a communicating sequential processes (CSP) model: communication through messages, emphasizing "sharing memory through communication, not communicating through shared memory". After the task is executed, the result is sent to a corresponding message queue, which is specially processed by the message queue receiving the message. Compared with the conventional communication mechanism, it has a more efficient and flexible communication mechanism.
[0217] Underlying interface 263, used to receive system messages directly reported by each work thread, such as the message sending module 210 of the calling party 21, the task execution result: success, exception, termination, cancellation, etc.
[0218] It should be noted that the above interface of the API 26 only shows part of the functions, and it can also contain other interfaces for completing the interaction between the calling party and the task manager and the thread pool manager based on the scene requirements, which is not limited here.
[0219] The following is based on Figure 12 , how the task and thread pool management architecture implements each step in the above example to achieve the corresponding technical effects is exemplarily described.
[0220] Optionally, for the addition of a new task, based on Figure 12 , Figure 13 Another task and thread pool management architecture diagram provided by the embodiment of the present application is shown in Figure 13For example, when the user operates on the business function module C of the invoker 21, and triggers a specific task. At this time, the business function module C sends a task call to the add interface 260 of the API 26.
[0221] Then the add interface 260 sends the task call to the task manager 22. So that the task manager 22 generates a new task 4, and adds the new task 4 to the task queue 23. Optionally, if the new task 4 is an IO intensive task, a reaching notification is sent to the IP intensive thread pool 25a to wake up the matched worker thread to execute the task, for example, the worker thread 2 shown in Figure 13
[0222] Optionally, for the new task to replace the task to be replaced, on the basis of Figure 12 , Figure 14 Another task and thread pool management architecture schematic diagram provided by the embodiment of the application is shown in Figure 14 For example, when the user operates on the business function module C of the invoker 21, and triggers a specific task. At this time, the business function module C sends a task call to the cancel / replacement interface 261 of the API 26.
[0223] Then the cancel / replacement interface 261 sends the task call to the task manager 22. So that the task manager 22 generates a new task 2, and replaces the task to be replaced in the task queue 23. After the replacement is completed, if the new task 2 is an IO intensive task, a reaching notification is sent to the IP intensive thread pool 25a to wake up the matched worker thread to execute the task, for example, the worker thread 3 shown in Figure 14
[0224] Optionally, for the cancellation of the task, on the basis of Figure 12 , Figure 15 Another task and thread pool management architecture schematic diagram provided by the embodiment of the application is shown in Figure 15 For example, when the user operates on the business function module C of the invoker 21, and triggers a task cancellation, the business function module C sends a task cancellation to the cancel / replacement interface 261 of the API 26.
[0225] Then the cancel / replacement interface 261 sends the task cancellation to the task manager 22. So that the task manager 22 removes the corresponding task, such as the execution of the task 2 in Figure 15 Then a termination signal is sent to the worker thread in the corresponding thread pool, such as Figure 15 The process then removes worker thread 3 from the IO-intensive thread pool 25a. If worker thread 3 does not respond to the removal request within a certain period of time, it is isolated and forced to terminate in an independent thread group. After the worker thread is terminated, the caller 21's messaging module 210 is notified of the cancellation of the task and worker thread via the underlying interface 263.
[0226] Optionally, for thread pool expansion, Figure 12 On the basis of Figure 16 Another task and thread pool management architecture diagram provided by an embodiment of the present invention is shown in FIG. Figure 16 , the task manager 22 will periodically check the task queue backlog. As mentioned above, tasks can be divided into: IO-intensive tasks, CPU-intensive tasks, and mixed tasks. When checking the task queue backlog, different types of tasks can be checked separately. For example, for an IO-intensive task, its queue length = 5; and the corresponding worker threads in the IO-intensive thread pool = 2.
[0227] Clearly, queue length = 5 > current number of threads × 2 = 4. Assuming this situation persists for a window period of 5 seconds, thread expansion is confirmed. Task manager 22 then sends an expansion request to thread pool manager 24 via expansion interface 264 of API 26. Thread pool manager 24 then expands the I / O-intensive thread pool 25a, adding three new worker threads.
[0228] Optionally, for the recycling of redundant threads, Figure 12 On the basis of Figure 17 Another task and thread pool management architecture diagram provided by an embodiment of the present invention is shown in FIG. Figure 17 , the thread pool manager 24 makes a check request to the task manager 22 through the recycling interface 265 of the API 26. So that the task manager 22 performs a regular check on the task queue 23 to confirm whether it meets the idle condition.
[0229] If the requirements are met, the thread pool manager 24 can recycle the redundant threads in each thread pool. For example, in the IO-intensive thread pool 25a, there are two redundant threads newly added in the same batch; in the CPU-intensive thread pool 25b, there is one redundant thread newly added in the same batch; and in the hybrid thread pool 25b, there are two redundant threads newly added in the same batch. All of these can be recycled through the recycling mechanism.
[0230] Optionally, for the client of the business system deployed by the caller 21, if it is necessary to interact and display the relevant information of the work thread and the task, Figure 12 On the basis of Figure 18Another task and thread pool management architecture schematic diagram provided by the embodiment of the application is shown in Figure 18 When the user selects the "task list (to be processed)" as shown in Figure 10 the interaction interface of the calling party 21, the message module 210 sends a task query request to the task manager 22 through the visual query interface 262 of the API 26, so that the task manager 22 reports the information of each task to be processed in the task list.
[0231] Optionally, when the user selects the "thread pool state" as shown in Figure 9 the interaction interface, the message module 210 sends a thread pool query request to the message query module 240 of the thread pool manager 24 through the visual query interface 262 of the API 26, so that the message query module 240 queries the message queue of each working thread in the thread pool. For example Figure 18 the query of each message queue of the CPU-intensive thread pool 25b as shown in
[0232] Optionally, when the user selects the "task list (processed)" as shown in Figure 11 the interaction interface, the message module 210 sends a thread pool query request to the message query module 240 of the thread pool manager 24 through the visual query interface 262 of the API 26, so that the message query module 240 queries the information of each processed task in the thread pool, that is, the task list (processed).
[0233] Based on the interaction mechanism of the above interaction interface, the visualization tracking of the task, the working thread and the thread pool can be effectively realized, and the maintenance cost is further reduced and the management efficiency is improved.
[0234] In order to execute each step shown in the above example and the corresponding technical effect, the application further provides a possible implementation manner of a task and thread pool management device, specifically, Figure 19 Another task and thread pool management device schematic diagram provided by the embodiment of the application is shown in Figure 19 The device 20 comprises a task queue manager 24 and a thread pool 25.
[0235] The task queue manager is configured to match a target thread pool corresponding to a task tag of a new task based on the task tag; the target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool or a hybrid thread pool.
[0236] The target thread pool is used to determine the target thread execution mode corresponding to the newly added task based on the task type of the newly added task. The target thread execution mode is one of scheduled execution, counted execution, or normal execution. The corresponding worker thread executes the newly added task based on the target thread execution mode.
[0237] Optionally, the task queue manager 24 is also used to: determine whether the processing method adopted for the new task is addition or replacement based on the processing method information of the new task; if it is addition, add the new task to the task queue; if it is replacement, replace the task to be replaced in the task queue with the new task.
[0238] Optionally, the task queue manager 24 is further configured to: determine whether there is a task backlog in the task queue; and if there is a task backlog, trigger thread expansion.
[0239] Optionally, in Figure 19 On the basis of Figure 20 A schematic diagram of another task and thread pool management device provided by an embodiment of the present invention, see Figure 20 , the device 20 also includes: a thread pool manager 24.
[0240] Furthermore, the task queue manager 24 is further configured to determine whether the task queue meets an idle condition.
[0241] If the conditions are met, the thread pool manager 24 is used to recycle the redundant threads.
[0242] Optionally, the task queue manager 24 is also used to: when a cancellation indication is obtained through the cancellation interface, remove the corresponding unexecuted tasks in the task queue according to the task number in the cancellation indication; when a batch cancellation indication is obtained through the cancellation interface, remove the corresponding multiple unexecuted tasks in the task queue according to the batch cancellation indication.
[0243] Optionally, the task queue manager 24 is further configured to: periodically check a termination flag; and if a termination flag exists, terminate the executing task corresponding to the termination flag in the task queue.
[0244] Optionally, the thread pool manager 24 is further configured to isolate the working thread of the executing task into an independent thread group for termination if the working thread corresponding to the executing task does not respond.
[0245] Optionally, the thread pool 25 is also used for: if the target thread execution mode is scheduled execution, the newly added task is executed regularly by the corresponding working thread at a preset time interval; if the target thread execution mode is counted execution, the newly added task is executed by the corresponding working thread, and the execution count is decremented after the execution is completed until the working thread count is decremented to 0; if the target thread execution mode is normal execution, the newly added task is executed by the corresponding working thread.
[0246] The electronic device provided by the embodiments of the present application can execute the steps of all the above examples to achieve the corresponding technical effects. Figure 21 A structural schematic diagram of an electronic device provided by the embodiments of the present application is shown in FIG. 3. Figure 21 The electronic device 30 comprises a memory 301 and a processor 300.
[0247] The memory 301 is configured to store one or more programs.
[0248] The processor 300.
[0249] When the one or more programs are executed by the processor, when the electronic device 30 is used to execute the steps shown in the above various method examples, the various steps and the corresponding technical effects can be achieved.
[0250] In the embodiments provided by the present application, it should be understood that the disclosed apparatus and method can also be implemented by other ways. The apparatus embodiments described above are only schematic, and for example, the flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architectures, functions and operation of the apparatus, method and computer program product according to the embodiments of the present application. In this regard, each block in the flowcharts or block diagrams can represent a module, a segment or a portion of code, which comprises one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions shown in the blocks can be performed in a different order from that shown in the accompanying drawings. For example, two consecutive blocks can actually be executed substantially in parallel, or they can be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and the combination of blocks in the block diagrams and / or flowcharts, can be implemented by a dedicated hardware-based system, or can be implemented by a combination of special-purpose hardware and computer instructions.
[0251] In addition, the various functional modules in the embodiments of the present application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0252] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present application can be embodied in the form of a program product stored in a computer readable storage medium, including a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a magnetic disk or an optical disk, and various storage media that can store program codes.
[0253] The thread pool initialization thread number in the prior art cannot be dynamically adjusted according to task load, resulting in response delay when tasks are accumulated, waste when resources are idle, lack of cancellation mechanism for tasks submitted to the queue or being executed, inability to cope with user-initiated termination requirements or error task processing, lack of real-time feedback of the internal running state of the thread pool (such as the number of active threads, the length of the task queue, and the task execution time), and difficulty in operation and maintenance debugging.
[0254] Compared with the prior art, the above-mentioned example of the present application provides a task and thread pool management mechanism, which not only solves the problems existing in the original thread pool technology, but also realizes the display of the system running state through the continuous collection and dynamic calculation of thread pool and task information, reduces the problem troubleshooting difficulty through visual display, and provides a quantitative basis for performance tuning.
[0255] The above is only a preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
[0256] It is obvious for those skilled in the art that the present application is not limited to the details of the above exemplary embodiments, and the present application can be implemented in other specific forms without departing from the spirit or essential characteristics of the present application. Therefore, the embodiments should be regarded as exemplary and non-limiting, and the scope of the present application is defined by the appended claims rather than the above description, and all changes falling within the meaning and scope of the equivalent elements of the claims are intended to be included in the present application. Any reference signs in the claims should not be regarded as limiting the claims.
Claims
1. A task and thread pool management method, characterized in that: include: Based on the task tag of the newly added task, match the target thread pool corresponding to the task tag; The target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool, or a hybrid thread pool; Determine, according to the task type of the newly added task, a target thread execution mode corresponding to the newly added task, wherein the target thread execution mode is one of scheduled execution, counted execution, or normal execution; The corresponding working thread executes the newly added task according to the target thread execution mode.
2. The method according to claim 1, characterized in that Before the step of matching the target thread pool corresponding to the task tag based on the task tag of the newly added task, the method further includes: According to the processing method information of the newly added task, determining whether the processing method adopted by the newly added task is addition or replacement; If it is to be added, the newly added task is added to the task queue; If it is a replacement, the newly added task replaces the task to be replaced in the task queue.
3. The method according to claim 2, characterized in that Also includes: Determine whether there is a task backlog in the task queue; If there is a task backlog, thread expansion is triggered.
4. The method according to claim 2, characterized in that Also includes: Determine whether the task queue meets the idle condition; The idle condition refers to that the task queue remains idle within a preset period; If the conditions are met, the redundant threads are recycled; the redundant threads are threads created in the same batch in the corresponding target thread pool, excluding the initial thread.
5. The method according to claim 1, wherein Also includes: When a cancellation instruction is obtained through the cancellation interface, the corresponding unexecuted task in the task queue is removed according to the task number in the cancellation instruction; When a batch cancellation instruction is obtained through the cancellation interface, the corresponding unexecuted tasks in the task queue are removed according to the batch cancellation instruction.
6. The method according to claim 1, characterized in that Also includes: Regularly check the termination mark; If the termination identifier exists, the executing task corresponding to the termination identifier in the task queue is terminated.
7. The method according to claim 6, characterized in that After the step of terminating the executing task corresponding to the termination identifier in the task queue, the method further includes: If the working thread corresponding to the task being executed does not respond, the working thread of the task being executed is isolated to an independent thread group and terminated.
8. The method according to claim 1, characterized in that The step of the corresponding working thread executing the newly added task according to the target thread execution mode includes: If the target thread execution mode is the scheduled execution, the newly added task is executed regularly by the corresponding working thread at a preset time interval; If the target thread execution mode is the counted execution, the newly added task is executed by the corresponding working thread, and the execution count is decremented after the execution is completed until the execution count of the working thread is decremented to 0; If the target thread execution mode is the normal execution, the newly added task is executed by the corresponding working thread.
9. A task and thread pool management device, characterized in that: include: The task queue manager is configured to match a target thread pool corresponding to a task tag of a newly added task based on the task tag of the task; the target thread pool is one of an IO-intensive thread pool, a CPU-intensive thread pool, or a hybrid thread pool; The target thread pool is used to confirm the target thread execution mode corresponding to the newly added task according to the task type of the newly added task, and the target thread execution mode is one of scheduled execution, counted execution or normal execution; the corresponding working thread executes the newly added task according to the target thread execution mode.
10. An electronic device, characterized in that: include: a memory for storing one or more programs; processor; When the one or more programs are executed by the processor, the method according to any one of claims 1 to 8 is implemented.
11. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
12. A program product, characterized in that When the program product is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
System and method for managing thread use in a thread pool
US20100153957A1
Task execution with non-blocking calls
US20200210223A1