Thread pool management method, device, computer terminal and storage medium
By creating task thread pool queues and object event loops to manage task threads, the problem of low efficiency and excessive resource utilization in object-oriented design is solved, and the effect of efficiently handling of isolated and scattered tasks is achieved.
Patent Information
- Application Number
- CN202111269518.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-29
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2041-10-29
AI Technical Summary
In object-oriented design, existing thread pools are inefficient and over-use of resources when dealing with orphaned tasks, resulting in frequent thread creation and destruction.
Create a task thread pool queue, listen to task request messages, create task objects and assign task threads, use object event loops and execution functions to execute tasks, and the task thread listens to messages in a dormant state until the task is completed.
It improves the efficiency of task processing, reduces resource usage, improves reaction speed, and adapts to the processing needs of discontinuous tasks.
Smart Images

Figure CN113986502B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a thread pool management method, device, computer terminal and storage medium. Background Art
[0002] Thread pools are widely used today to execute time-consuming tasks, avoiding the need to repeatedly create and destroy threads. However, in object-oriented design, a thread may need to independently handle all tasks for an object. These tasks are not sequential but isolated and fragmented, leading to multiple thread scheduling. This can lead to time-consuming, inefficient, and resource-intensive tasks. Summary of the Invention
[0003] In view of this, the present application provides a thread pool management method, including:
[0004] Create a number of task threads, then add the number of task threads to the thread pool management queue, and listen for task request messages;
[0005] When the task request message is received, a corresponding task object is created, wherein the task object includes an object event loop and an execution function related to the current task;
[0006] A task thread is allocated to the task object from the thread pool management queue, and the object event loop is hosted to the task thread, so that the task thread calls the execution function to execute the current task.
[0007] Furthermore, the task thread includes a thread event loop. When the task thread is created, the thread event loop is started and the task thread is put into a dormant state.
[0008] After the task thread is assigned to the task object, the object event loop of the task object begins to be executed through the thread event loop.
[0009] Furthermore, the step of creating a number of task threads also includes:
[0010] A main thread is created, wherein the main thread includes a main event loop, and the main event loop is used to execute the operation of creating a number of task threads, then adding the number of task threads to the thread pool management queue, and listening for task request messages.
[0011] Furthermore, after the object event loop is entrusted to the task thread, the method further includes:
[0012] The task thread monitors the task response message generated during the task execution process in real time, and calls the corresponding execution function according to the task response message.
[0013] Furthermore, before allocating a task thread to the task object, the method further includes:
[0014] Determine whether the number of task threads allocated in the thread pool management queue has reached the maximum number of threads. If so, block and wait; if not, execute the operation of allocating task threads to the task object.
[0015] Furthermore, it also includes:
[0016] When the executed task is completed, the association between the task thread and the task object is released, the task thread is put into a dormant state, and the task object is destroyed.
[0017] Furthermore, the task request message is sent by an upper layer application, and when the upper layer application is exited, a deregistration function is called to deregister the thread pool.
[0018] Furthermore, an embodiment of the present application also provides a thread pool management device, comprising:
[0019] The thread pool module creates a number of task threads, then adds the number of task threads to the thread pool management queue, and monitors whether there is a task request message;
[0020] A task object module, when receiving the task request message, creates a corresponding task object, wherein the task object includes an object event loop and an execution function related to the current task;
[0021] An execution module is allocated to allocate a task thread to the task object from the thread pool management queue, and the object event loop is hosted to the task thread so that the task thread calls the execution function to execute the current task.
[0022] Furthermore, an embodiment of the present application also provides a computer terminal, including a processor and a memory, wherein the memory stores a computer program, and when the computer program runs on the processor, it executes the above-mentioned thread pool management method.
[0023] Furthermore, an embodiment of the present application also provides a readable storage medium storing a computer program, which executes the above-mentioned thread pool management method when running on the processor.
[0024] The present application provides a thread pool management method, which creates a number of task threads, then adds the number of task threads to the thread pool management queue, and listens for task request messages; when the task request message is received, a corresponding task object is created, and the task object includes an object event loop and an execution function related to the current task; a task thread is allocated to the task object from the thread pool management queue, and the object event loop is entrusted to the task thread so that the task thread calls the execution function to execute the current task. This method makes the thread processing task no longer process-oriented as before, but object-oriented, so that when processing tedious and discontinuous tasks, there is no need to frequently cancel and generate threads, which saves resources, improves response speed, and can better serve upper-level applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the technical solution of the present invention, the following is a brief introduction to the drawings required for use in the embodiments. It should be understood that the following drawings only illustrate certain embodiments of the present invention and should not be regarded as limiting the scope of protection of the present invention. In each of the drawings, similar components are numbered similarly.
[0026] Figure 1 A schematic diagram of a process flow of a thread pool management method according to an embodiment of the present application is shown;
[0027] Figure 2 A schematic diagram showing the structure of the thread pool management method data in an embodiment of the present application is shown;
[0028] Figure 3 A schematic diagram showing a task object in an embodiment of the present application is shown;
[0029] Figure 4 A schematic diagram of a thread pool management device according to an embodiment of the present application is shown. DETAILED DESCRIPTION
[0030] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments.
[0031] The components of the embodiments of the present invention generally described and illustrated in the figures herein may be arranged and designed in a variety of different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the figures is not intended to limit the scope of the claimed invention, but rather merely represents selected embodiments of the present invention. All other embodiments derived by those skilled in the art based on the embodiments of the present invention without inventive effort are intended to be within the scope of protection of the present invention.
[0032] Hereinafter, the terms "including", "having" and their cognates, which may be used in various embodiments of the present invention, are intended only to indicate specific features, numbers, steps, operations, elements, components or combinations of the foregoing items, and should not be understood as first excluding the existence of one or more other features, numbers, steps, operations, elements, components or combinations of the foregoing items or the possibility of adding one or more features, numbers, steps, operations, elements, components or combinations of the foregoing items.
[0033] Furthermore, the terms “first,” “second,” “third,” etc., are merely used for distinguishing descriptions and are not to be understood as indicating or implying relative importance.
[0034] Unless otherwise defined, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by those skilled in the art to which the various embodiments of the present invention pertain. The terms (such as those defined in generally used dictionaries) will be interpreted as having the same meaning as in the context of the relevant technical field and will not be interpreted as having an idealized meaning or an overly formal meaning unless clearly defined in the various embodiments of the present invention.
[0035] Example 1
[0036] This application provides a thread pool management method. In this embodiment, the method is based on Qthread in Qt programming. Figure 1 Come to understand.
[0037] Exemplarily, the thread pool management method includes:
[0038] Step S100: Create a number of task threads, then add the number of task threads to a thread pool management queue, and monitor whether there is a task request message.
[0039] When a program starts, it first creates (using the word "create" in the briefing) the main thread of the program. The main thread has its own main event loop, and the thread pool is maintained by the main event loop. The main thread is mainly used to handle the work of component classes and several related classes; the task thread is a working thread, which is mainly responsible for processing tasks assigned by the main thread. For a program, as long as it is running and interacting with the user, tasks that need to be processed by threads will be generated at all times, such as the special effects when moving the mouse in the interactive interface, mouse clicks, and events triggered by clicks, all of which require threads to process. Therefore, the main thread will create a number of task threads. These task threads have their own task event loops. After creation, the start() function will be executed to start the task thread's event loop, and then these task threads will be added to the thread pool management queue. The thread pool management queue is the thread pool. These task threads in the thread pool management queue are in a dormant state because they have not been assigned tasks yet.
[0040] Specifically, the task event loop of the task thread is implemented by the exec() method, which continuously loops through the event queue inside the task thread. However, when these task threads in the queue are not assigned tasks, these event loops do not actually execute any programs. They can be considered to be constantly listening for task messages to be sent, and can be called at any time when there is an event driver.
[0041] like Figure 2 As shown, n task threads are created in the main thread. These task threads are added to the thread pool in the form of a queue and monitor in real time whether there is event-driven. In the actual program running, the event-driven is reflected in the form of task request messages.
[0042] Step S200, when the task request message is received, creating a corresponding task object, the task object including an object event loop and an execution function related to the current task;
[0043] Combine Figure 3 Understand that when a task request message arrives, the main thread creates a task object related to the task. For example, for an upgrade task, an upgrade-related task object is created. This task object includes its own object event loop and task-related execution functions. These task request messages are sent by upper-level applications and contain the task type and the task steps to be executed. The task object's event loop contains the specific execution steps composed of these execution functions. For example, if a task has three steps, A, B, C, and D, the execution step can be determined through switch or if conditional judgments.
[0044] Specifically, for example, when there is an upgrade task that needs to be executed, a task request message for the upgrade task will come to the main thread. The main thread determines whether the task category is upgrade based on the message content, and specifically which step in the upgrade it is. Based on this, an upgrade task object is created based on the relevant upgrade class in the program. The upgrade class includes a series of execution functions required for the upgrade, such as preparation before the upgrade, various file verifications during the upgrade process, judgment after the upgrade, etc. These different execution functions are targeted at different steps in the actual upgrade operation in the object event loop and are triggered by different judgment conditions.
[0045] In the actual upgrade process, the above process will not be a continuous process, just as the upgrade process also involves data transmission and certain interruption events. That is to say, the task responsible for handling the upgrade event will be suspended due to various system accidents or operator reasons, making the task discontinuous.
[0046] Generally speaking, the task handled by a thread is often the smallest step unit in the task, such as verifying two files or copying a file. After execution, the system will feedback a return value to indicate the task execution status, such as success, failure, or request for retry. Regardless of the success or failure of the task, the original thread will die at the end of its life cycle. Therefore, in an upgrade task, threads will be repeatedly created and destroyed to achieve the task.
[0047] In this embodiment, because the task object is created, the task object contains the execution function required for the task, and the task object is event-driven, that is, the corresponding execution function is executed according to the task message, and it has its own event loop, so that in the actual operation process, it can decide which task step to execute according to external messages, and after executing a step, it will not exit immediately, but will continue to listen to external task response messages, so that the program can execute the correct steps according to the task execution status.
[0048] Step S300: Allocate a task thread to the task object from the thread pool management queue, and delegate the object event loop to the task thread, so that the task thread calls the execution function to execute the current task.
[0049] After the task object is created, a thread can be assigned to it so that the computer has computing resources to execute the task. However, in actual situations, computer resources may be occupied, resulting in insufficient threads. For example, when the number of assigned task threads reaches the preset value, that is, the thread pool management queue is empty, the thread will not be assigned immediately, and the requested task will enter a waiting state until a free thread appears. Only then will the requested task be assigned a thread to execute the task.
[0050] After the task thread and task object are allocated, the task object will host its own object event loop to the task thread. It can be understood that the object event loop of the task object will be executed in the task event loop of the task thread. The task thread in the queue is in a dormant state, and its task event loop is empty. It just keeps listening for messages. When the task object hosts the object event loop to the task thread through the movetothread() function, the task thread will receive the corresponding task request message, and accept the event loop of the task object. It will determine which step of the task should be executed based on the task request message from the outside world, and call the relevant function of the step to implement it.
[0051] In this embodiment, the upgrade task execution process may include three steps A, B and C. At the beginning, the task request message not only includes the type of task being an upgrade, but also includes a message that the current step is step A. Therefore, after receiving the task object hosting, the task thread will know what step A it wants to execute. When step A is executed, this type of program will return a status value to indicate the success or failure of step A. The system confirms whether to continue to execute the next step B based on the status value.
[0052] It should be noted that, in this embodiment, because the task object has its own object event loop, and the event loop is executed in the task event loop of the task thread, after executing step A and returning the status value, the task thread still exists and will not jump out of the object event loop, unless the executed step is the last step and the jump operation is performed. Therefore, in this embodiment, the task thread will not die after executing a step, but will suspend and wait for the next command.
[0053] When the system determines that it can continue to execute step B, it will continue to send a task response message to execute step B. After the main thread receives the message, it will send the message to the task thread. The task thread will continue to execute step B according to the response message. Otherwise, it will suspend and wait for external messages, or until the timeout expires, triggering the thread extinction operation under the timeout mechanism. In other words, in the embodiment of the present application, a discontinuous task can be completed by one thread.
[0054] Further optionally, when the task is completed, the main thread disassociates the task thread from the task object, destroys the task object, releases the memory, and the task thread returns to a dormant state and returns to the thread pool management queue from the end of the queue.
[0055] When the thread pool is no longer needed, the thread pool is deregistered through the deregistration function clear(). The specific situation where the thread pool is not needed may be, for example, when the program is closed or the upper application is stopped.
[0056] For details, please refer to the attached Figure 2 and Figure 3 ,The following describes the complete cycle of a task.
[0057] After the task thread is created in the main thread, it waits for the task request message, creates the corresponding task object according to the task request message, and then assigns the task thread to the task object. The task object is created based on the corresponding task class. The task object includes the corresponding execution function of the task and its own event loop. When the task object hands the event loop to the task thread for hosting, the execution function in the object can be called to complete the task, and any signal sent to the task object will be responded in the task thread, thus realizing the event-driven task.
[0058] Each execution function of a different function can be considered as a solution to a separate task or a step in a task. The corresponding execution function is triggered according to the external task request message and task response to execute the task. For example, the above example includes pre-upgrade judgment, file verification during upgrade, and post-upgrade judgment of success, whether parameters need to be modified due to upgrade, and other operations.
[0059] When a task is successfully completed, or when it is stopped by external messages, or when it exits due to multiple failures reaching a preset value, the task object will be destroyed and the thread will be released. The released thread returns to the thread pool queue and enters a dormant state, waiting for new objects to be connected. Specifically, you can call the removeThread() function to release the thread. When the thread pool is no longer needed, you can call the clear() function to deregister the thread pool. As long as the task object is destroyed and the thread is released, this application does not limit this.
[0060] Example 2
[0061] This application also provides a thread pool management device, such as Figure 4 As shown, the device includes a thread pool module 10, a task object module 11 and an allocation execution module 12, wherein:
[0062] The thread pool module 10 creates a number of task threads, adds the number of task threads to the thread pool management queue, and monitors whether there is a task request message;
[0063] The task object module 11 is configured to create a task object for the requested task upon receiving the task request message, wherein the task object includes an object event loop and an execution function related to the task;
[0064] The allocation execution module 12 allocates a task thread to the task object from the thread pool management queue, and hosts the object event loop to the task thread, so that the task thread calls the execution function to execute the current task.
[0065] Furthermore, an embodiment of the present application also provides a computer terminal, including a processor and a memory, wherein the memory stores a computer program, and when the computer program runs on the processor, it executes the thread pool management method described in any one of the above embodiments.
[0066] Furthermore, an embodiment of the present application also provides a readable storage medium storing a computer program, which executes the thread pool management method described in any one of the above embodiments when the computer program is run on the processor.
[0067] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and structure diagrams in the accompanying drawings show the possible architectures, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of the code, and the module, program segment or a part of the code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in an alternative implementation, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the structure diagram and / or flowchart, and the combination of boxes in the structure diagram and / or flowchart, can be implemented using a dedicated hardware-based system that performs the specified function or action, or can be implemented using a combination of dedicated hardware and computer instructions.
[0068] In addition, the functional modules or units in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.
[0069] 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 this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a smart phone, a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0070] The above description is only a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any technician familiar with this technical field can easily think of changes or replacements within the technical scope disclosed by the present invention, which should be covered by the scope of protection of the present invention.
Claims
1. A thread pool management method, characterized in that: include: Creating a main thread, the main thread including a main event loop, the main event loop being used to create a number of task threads, then adding the number of task threads to a thread pool management queue, and monitoring whether there are task request messages; Create a number of task threads, then add the number of task threads to the thread pool management queue, and listen for task request messages; When the task request message is received, a corresponding task object is created, wherein the task object includes an object event loop and an execution function related to the current task; Allocate a task thread for the task object from the thread pool management queue, and host the object event loop to the task thread. The task thread monitors the task response message generated during the task execution process in real time, and calls the corresponding execution function according to the task response message, so that the task thread calls the execution function to execute the current task; The task thread includes a thread event loop. When the task thread is created, the thread event loop is started and the task thread enters a dormant state. After the task thread is assigned to the task object, the object event loop of the task object begins to be executed through the thread event loop.
2. The thread pool management method according to claim 1, characterized in that: Before allocating a task thread to the task object, the method further includes: Determine whether the number of task threads allocated in the thread pool management queue has reached the maximum number of threads. If so, block and wait; if not, execute the operation of allocating task threads to the task object.
3. The thread pool management method according to claim 1, wherein: Also includes: When the executed task is completed, the association between the task thread and the task object is released, the task thread is put into a dormant state, and the task object is destroyed.
4. The thread pool management method according to claim 1, wherein: The task request message is sent by the upper layer application. When the upper layer application is exited, a deregistration function is called to deregister the thread pool.
5. A thread pool management device, characterized in that: include: The thread pool module creates a main thread, which includes a main event loop. The main event loop is used to create a number of task threads, add the number of task threads to the thread pool management queue, and monitor whether there are task request messages. Create a number of task threads, then add the number of task threads to the thread pool management queue, and listen for task request messages; A task object module, when receiving the task request message, creates a corresponding task object, wherein the task object includes an object event loop and an execution function related to the current task; An allocation execution module allocates a task thread to the task object from the thread pool management queue, and entrusts the object event loop to the task thread. The task thread monitors the task response message generated during the task execution process in real time, and calls the corresponding execution function according to the task response message, so that the task thread calls the execution function to execute the current task; The task thread includes a thread event loop. When the task thread is created, the thread event loop is started and the task thread enters a dormant state. After the task thread is assigned to the task object, the object event loop of the task object begins to be executed through the thread event loop.
6. A computer terminal, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program, and when the computer program is run on the processor, the thread pool management method according to any one of claims 1 to 4 is executed.
7. A readable storage medium, characterized in that: The computer program stores a computer program, which executes the thread pool management method according to any one of claims 1 to 4 when running on a processor.
Citation Information
Patent Citations
Method and device for processing application program abnormity
CN107391295A
Thread pool task processing method in high-availability cluster system
CN107832146A