A task execution method and apparatus

By generating a task queue and sorting it in the main queue, threads in the thread pool are reused in a circular manner. The dual-queue mechanism solves the problems of thread blocking and resource waste in the thread pool, improves system stability and CPU utilization, and reduces the risk of memory leaks.

CN111290842BActive Publication Date: 2026-05-19BEIJING JINGDONG SHANGKE INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING JINGDONG SHANGKE INFORMATION TECH CO LTD
Filing Date
2018-12-10
Publication Date
2026-05-19

AI Technical Summary

Technical Problem

In multi-core programming, thread pools can lead to thread blocking, prevent thread reuse, cause excessive threads to waste memory resources, reduce CPU utilization due to frequent switching between threads, and decrease system stability and increase the risk of memory leaks due to frequent thread creation and destruction.

Method used

A task queue is generated based on the dependencies between tasks, and each task queue is sorted in the main queue. Threads in the thread pool are used in a loop to execute tasks in each task queue in the order of the task queues in the main queue. A dual-queue mechanism is used to achieve concurrent execution of tasks in the thread pool environment.

Benefits of technology

It solves the thread blocking problem, improves thread reusability, enhances memory resource and CPU utilization, strengthens system stability, reduces the risk of memory leaks, and improves program execution performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111290842B_ABST
    Figure CN111290842B_ABST
Patent Text Reader

Abstract

The application discloses a task execution method and device, and relates to the technical field of computers. A specific implementation of the method comprises the following steps: generating task queues according to the dependency relationship among tasks, and sorting the task queues in a main queue, wherein the dependency relationship among the tasks determines the execution sequence of the tasks in the task queues, and the main queue determines the execution sequence of the task queues; and cyclically utilizing threads in a thread pool to execute the tasks in the task queues according to the sequence of the task queues in the main queue. The implementation can solve the coupling problem among the tasks, thereby solving the thread blocking problem, improving the thread reusability, avoiding frequent thread switching, improving the memory resource utilization rate and the CPU utilization rate, solving the problem of frequent thread creation and destruction, improving the overall stability of the system, and reducing the risk of memory leakage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a task execution method and apparatus. Background Technology

[0002] Currently, in multi-core program design, thread pools or multithreading are used to achieve concurrent program execution.

[0003] Thread pools are suitable for scenarios where tasks are not highly coupled. However, if multiple tasks are causally related, a thread in the thread pool must wait for the results of other threads, causing that thread to block and reducing program efficiency. For example, if each user establishes a session and the user's actions are causally related, using a thread pool to achieve concurrency will result in many threads being blocked.

[0004] Multithreading employs a strategy of assigning one thread to one session. While this can solve the problem of thread blocking in concurrent situations, it has the following drawbacks:

[0005] Threads are not being reused well. If a session does not currently have any tasks, the thread will be idle and other sessions will not be able to reuse the thread.

[0006] Large programs typically handle tens of thousands of sessions simultaneously, which creates tens of thousands of threads. The operating system allocates at least 8 megabytes of memory for each thread, and the total amount of memory is incalculable.

[0007] Currently, a typical CPU (Central Processing Unit) has at most a few dozen cores. If there are thousands of threads, these threads will take turns to obtain CPU time slices, resulting in frequent switching between threads, frequent entry into the kernel level, a smaller chance of being selected by the cache, and reduced program execution performance.

[0008] Frequent creation and deletion of threads reduces overall system stability and increases the risk of memory leaks.

[0009] In the process of realizing this invention, the inventors discovered at least the following problems in the prior art:

[0010] Using thread pools can lead to thread blocking; using multithreading can result in threads that cannot be reused, too many threads can lead to wasted memory resources, frequent switching between threads can lead to low CPU utilization, and frequent creation and destruction of threads can reduce the overall stability of the system and increase the risk of memory leaks. Summary of the Invention

[0011] In view of this, embodiments of the present invention provide a task execution method and apparatus that can solve the coupling problem between tasks, thereby solving the thread blocking problem, improving thread reusability, eliminating the need for frequent thread switching, increasing memory resource utilization and CPU utilization, and also solving the problem of frequent thread creation and destruction, thereby improving the overall system stability and program execution performance, and reducing the risk of memory leaks.

[0012] To achieve the above objectives, according to one aspect of the present invention, a task execution method is provided.

[0013] A task execution method includes: generating a task queue according to the dependencies between tasks, and sorting each task queue in a main queue, wherein the dependencies between tasks determine the execution order of tasks in the task queues, and the main queue determines the execution order of each task queue; and reusing threads in a thread pool to execute tasks in each task queue according to the order of the task queues in the main queue.

[0014] Optionally, the step of generating a task queue according to the dependencies between tasks and sorting each task queue in the main queue includes: determining whether the input task has a corresponding task queue based on the dependencies between tasks; if so, adding the input task to the corresponding task queue; otherwise, generating a new task queue for the input task and arranging the new task queue in the main queue.

[0015] Optionally, the step of determining whether an input task has a corresponding task queue based on the dependencies between tasks includes: determining whether the queue to which the input task depends is currently empty based on the dependencies between tasks; if so, determining whether the empty queue is currently being executed in the thread; if so, the input task has a corresponding task queue; if not, the input task does not have a corresponding task queue; if the queue to which the input task depends is not currently empty, the input task has a corresponding task queue.

[0016] Optionally, before the step of generating a new task queue for the input task, the method includes: locking the empty queue that is not being executed in the thread; and the step of generating a new task queue for the input task includes: locking the main queue and adding the input task to the empty queue that is not being executed in the thread to generate the new task queue.

[0017] Optionally, the step of reusing threads in the thread pool and executing tasks in each task queue according to the order of the task queues in the main queue includes: when there are idle threads in the thread pool, obtaining task queues from the main queue according to the number of idle threads and the order of the task queues in the main queue; allocating an idle thread to each obtained task queue to execute each task in the task queue; and releasing the thread occupied by the task queue after all tasks in a task queue have been executed, so that the released idle thread can continue to execute the tasks in the remaining task queues in the main queue, until all tasks in the task queues in the main queue have been executed.

[0018] According to another aspect of the present invention, a task execution apparatus is provided.

[0019] A task execution device includes: a queue generation module, configured to generate task queues according to the dependencies between tasks, and sort each task queue in a main queue, wherein the dependencies between tasks determine the execution order of tasks in the task queues, and the main queue determines the execution order of each task queue; and a task execution module, configured to circulate threads in a thread pool and execute tasks in each task queue according to the order of the task queues in the main queue.

[0020] Optionally, the queue generation module is further configured to: determine whether the input task has a corresponding task queue based on the dependency relationship between tasks; if so, add the input task to the corresponding task queue; otherwise, generate a new task queue for the input task and arrange the new task queue in the main queue.

[0021] Optionally, the queue generation module includes a judgment submodule, configured to: determine whether the queue to which the input task depends is currently empty based on the dependencies between tasks; if so, determine whether the empty queue is currently being executed in the thread; if so, the input task has a corresponding task queue; if not, the input task does not have a corresponding task queue; if the queue to which the input task depends is not currently empty, the input task has a corresponding task queue.

[0022] Optionally, the queue generation module includes a locking submodule, configured to: lock the empty queue that is not being executed in the thread; and the queue generation module further includes a task queue generation submodule, configured to: lock the main queue and add the input task to the empty queue that is not being executed in the thread, so as to generate the new task queue.

[0023] Optionally, the task execution module is further configured to: when there are idle threads in the thread pool, according to the number of idle threads and the order of the task queues in the main queue, obtain the number of task queues with the number of idle threads from the main queue; allocate an idle thread to each obtained task queue to execute each task in the task queue; and release the thread occupied by the task queue whenever all tasks in a task queue are completed, so that the released idle thread can continue to execute the tasks of the remaining task queues in the main queue, until all tasks in the main queue are completed.

[0024] According to another aspect of the present invention, an electronic device is provided.

[0025] An electronic device includes: one or more processors; and a memory for storing one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement the task execution method provided by the present invention.

[0026] According to another aspect of the present invention, a computer-readable medium is provided.

[0027] A computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements the task execution method provided by the present invention.

[0028] One embodiment of the above invention has the following advantages or beneficial effects: A task queue is generated according to the dependencies between tasks, and each task queue is sorted in the main queue; threads in the thread pool are reused cyclically, and tasks in each task queue are executed according to the order of the task queues in the main queue. This invention uses a dual-queue approach to achieve concurrent execution of coupled tasks in a thread pool environment. A main queue records all task queues to be executed, and each task queue records its own queued tasks. This solves the coupling problem between tasks, thereby resolving thread blocking issues. It offers good thread reusability, eliminates the need for frequent thread switching, and achieves high memory and CPU utilization. It also solves the problem of frequent thread creation and destruction, improving overall system stability and program execution performance, and reducing the risk of memory leaks.

[0029] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description

[0030] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:

[0031] Figure 1 This is a schematic diagram of the main steps of a task execution method according to an embodiment of the present invention;

[0032] Figure 2 This is a schematic diagram of a session processing flow according to an embodiment of the present invention;

[0033] Figure 3 This is a schematic diagram of the thread execution flow based on a dual-queue mechanism according to an embodiment of the present invention;

[0034] Figure 4 This is a schematic diagram of the task enqueueing process in a non-empty task queue according to an embodiment of the present invention;

[0035] Figure 5 This is a schematic diagram of the task enqueueing process in an empty queue according to an embodiment of the present invention;

[0036] Figure 6 This is a schematic diagram of the task enqueue lock process according to an embodiment of the present invention;

[0037] Figure 7 This is a schematic diagram of the main modules of a task execution device according to an embodiment of the present invention;

[0038] Figure 8 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;

[0039] Figure 9 This is a schematic diagram of the structure of a computer system suitable for implementing terminal devices or servers of the present invention. Detailed Implementation

[0040] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0041] Those skilled in the art will recognize that embodiments of the present invention can be implemented as a system, apparatus, device, method, or computer program product. Therefore, this disclosure can be specifically implemented in the following forms: entirely hardware, entirely software (including firmware, resident software, microcode, etc.), or a combination of hardware and software.

[0042] Figure 1 This is a schematic diagram of the main steps of a task execution method according to an embodiment of the present invention.

[0043] like Figure 1 As shown, the task execution method of this embodiment mainly includes the following steps S101 to S102.

[0044] Step S101: Generate a task queue according to the dependencies between tasks, and sort each task queue in the main queue.

[0045] The dependencies between tasks determine the execution order of tasks in the task queue.

[0046] Dependencies between tasks can be categorized as follows: for example, the execution of task a and task b may have a causal relationship where the execution of task a depends on the result of task b. Therefore, task a and task b are two tasks with a dependency relationship. This dependency can be a causal logical dependency as described above, or it can be other dependencies, such as a time-dependent relationship where task b can only be executed after task a has finished. In this embodiment of the invention, when generating the task queue, two or more tasks with various dependencies are placed into the same task queue.

[0047] The main queue determines the execution order of each task queue. Specifically, the task queues are sorted in the main queue so that each task queue can be executed sequentially according to the order in which they are arranged in the main queue.

[0048] Specifically, step S101 involves determining whether the input task has a corresponding task queue based on the dependencies between tasks; if so, the input task is added to the corresponding task queue; otherwise, a new task queue is generated for the input task, and the generated new task queue is placed in the main queue.

[0049] The step of determining whether an input task has a corresponding task queue based on the dependencies between tasks can specifically include:

[0050] Based on the dependencies between tasks, determine whether the queue that the input task depends on is currently empty. If it is, determine whether the empty queue is being executed in a thread. If it is, the input task has a corresponding task queue; otherwise, the input task does not have a corresponding task queue. If the queue that the input task depends on is not currently empty, the input task has a corresponding task queue.

[0051] Queues that have not been added to after creation and empty task queues can both be called empty queues. An empty task queue is a queue that has no tasks after all tasks in a task queue have been executed in a thread (at this time, the queue still occupies one thread).

[0052] Taking a session processing scenario as an example, the input task is a task of a certain session, and the task queue corresponding to the input task is the task queue of that session. The task queue of that session can be a non-empty task queue (i.e., there are still unfinished tasks in the task queue) or an empty task queue.

[0053] The queue to which the input task depends can be a queue of tasks that have a dependency relationship with the input task. When the queue is not being executed by a thread, it is a non-empty task queue. When the queue is currently in a thread, it can be either a non-empty task queue or an empty task queue, depending on the execution status of the tasks within it. For example, in a session, the session's task queue to which the input task belongs can serve as the queue to which the input task depends.

[0054] If no task (including completed tasks within a thread) depends on the input task, then the input task will have an empty queue (with no tasks added to it). This empty queue is used to place the input task. This empty queue with no tasks added can also be called the queue that the input task depends on. For example, an empty queue created after a session is established, containing no tasks for that session, can also serve as the queue that the input task depends on.

[0055] In the context of the above session processing scenario, if the queue that the input task depends on is currently empty and is being executed in a thread, it means that the input task has a task queue belonging to its session, and the empty queue is an empty task queue that has not yet been released. In this case, the input task is added to the empty task queue so that the input task can continue to be executed in the thread. When the input task is completed, if there are no other newly added tasks in the session, the thread occupied by the session is released.

[0056] If the queue that the input task depends on is currently empty but not being executed in a thread, it means that no task under the session to which the input task belongs has been added to the empty queue. In this case, the input task is added to the empty queue, thereby creating the task queue for that session. The task queue for that session is then placed in the main queue to wait for the thread to execute.

[0057] If the queue that the input task depends on is not currently empty, it means that the input task has a task queue belonging to its session (it is being executed in a thread, but there are still tasks in the queue that have not been completed; or it is waiting to be entered into the main queue for execution). In this case, the input task can be added to the task queue of the session and wait for execution.

[0058] In addition, each task queue needs to be marked with an atomicity flag, which indicates whether the task queue is currently being executed by a thread or is currently in an idle state (i.e. waiting to be executed by a thread).

[0059] Before adding an input task to its corresponding task queue, that task queue needs to be locked. This lock is a mutex lock, used to ensure the uniqueness of task queue execution. Similarly, before generating a new task queue for an input task, an empty queue that is not being executed in a thread also needs to be locked, so that the newly generated task queue has the mutex lock, thereby ensuring the uniqueness of task queue execution.

[0060] The steps for generating a new task queue for input tasks may specifically include: locking the main queue (using a mutex lock), and adding the input tasks to an empty queue that is not being executed in a thread, thus generating a new task queue. Adding a mutex lock to the main queue ensures its uniqueness.

[0061] Each task queue is sorted in the main queue according to its creation time (the time the first task was enqueued). For example, after a session is established, the session corresponds to an empty queue (no tasks have been added). When the first task of the session is received, the task is added to the empty queue (task enqueuing), resulting in the task queue for that session. The session node is then attached to the main queue, thus placing the task queue for that session at the current last position in the main queue. If another session node is subsequently attached, the task queue for that other session will be placed after the task queue for the current session.

[0062] Step S102: Utilize the threads in the thread pool in a loop and execute the tasks in each task queue according to the order of the task queues in the main queue.

[0063] Specifically, when there are idle threads in the thread pool,

[0064] Based on the number of idle threads, a task queue with the number of idle threads is obtained from the main queue in the order of the task queues in the main queue. An idle thread is allocated to each obtained task queue to execute the tasks in the task queue. Whenever all tasks in a task queue are completed, the thread occupied by that task queue is released so that the released idle thread can continue to execute the tasks in the remaining task queues in the main queue (a task queue with the number of idle threads is obtained from the main queue in the order of the task queues in the main queue) until all tasks in the task queues in the main queue are completed.

[0065] Figure 2 This is a schematic diagram of a session processing flow according to an embodiment of the present invention.

[0066] In session processing scenarios, there are usually dependencies between tasks within each session. The task execution method of this invention will be further described below according to the session processing flow. It should be noted that the task execution method of this invention is not only applicable to session processing, but can also be used in other task execution scenarios with high task coupling.

[0067] like Figure 2 As shown, a thread pool is created to handle tasks. The thread pool has a fixed number of threads; this example uses four threads. A main queue is allocated to record all sessions that are about to be queued.

[0068] Each session is assigned a queue, as shown in queues 1 through 9 in the diagram. Figure 2 Queues 1 to 7 are the task queues for sessions 1 to 7, respectively. Each task queue records the tasks queued for this session. Queues 1 to 4 are the task queues currently being executed in the thread pool, and queues 5 to 7 are the task queues waiting to be executed, i.e., the task queues of sessions waiting in the main queue. Queues 8 and 9 are the empty queues corresponding to sessions 8 and 9, respectively (and no tasks have been added to them).

[0069] Each thread in the thread pool executes a task queue for a session. A thread is released only after all tasks in a session's task queue have been completed. After a thread is released, the idle thread retrieves a task queue to be executed from the main queue. For example... Figure 2 In this process, the task queues for sessions 5 to 7 are the execution queues. Idle threads retrieve tasks from the main queue in sequence according to the order of the session's task queues and assign them to idle threads for execution. Assuming there is currently an idle thread, it will retrieve the task queue for session 5 from the main queue to execute the tasks in queue 5.

[0070] This invention employs a dual-queue mechanism for thread execution. After completing all tasks in the current session, the main session queue is checked. If all tasks in the current session have been completed, a session is retrieved from the main session queue for execution. The thread execution flow based on the dual-queue mechanism in this invention is as follows: Figure 3 As shown. Figure 3 As shown, the process first checks if there are any sessions to be executed in the main session queue. If so, the session is removed from the main queue and execution begins. Otherwise, the process blocks and waits for the main session queue. When executing tasks in the task queues of sessions removed from the main queue, after completing a task, the process checks if there are any unexecuted tasks in the current task queue. If so, tasks are retrieved from the current session queue and executed. If there are no unexecuted tasks in the current session's task queue (i.e., all tasks in the current session's queue have been executed), the process returns to the step of checking if there are any sessions to be executed in the main session queue, in order to execute tasks in the task queues of other sessions.

[0071] When an input task is received, it can be determined whether the task's session has any other tasks attached, i.e., whether the task's session has a non-empty task queue. For example, Figure 2Queues 1 through 7 are the non-empty task queues for sessions 1 through 7, respectively. The tasks in queues 1 through 7 are the tasks attached to sessions 1 through 7. If the input task has tasks attached to its corresponding session, the input task will be directly attached to the task queue of that session. Figure 4 The task enqueueing process of a non-empty task queue according to an embodiment of the present invention is illustrated exemplarily, such as... Figure 4 As shown, when the input task is task 4, and task 4 is a task of session 5, task 4 is attached to the task queue of session 5 and arranged after task 3.

[0072] If the input task has no attached tasks under its session (i.e., the queue it depends on is currently empty and no tasks have been added), then when enqueuing the input task, in addition to attaching the task to the empty queue under its session, the session node also needs to be attached to the main queue so that it can be obtained and executed by an idle thread. Figure 5 The task enqueueing process for an empty queue according to an embodiment of the present invention is illustrated exemplarily, such as... Figure 5 As shown, session 5 corresponds to an empty queue. When task 1 of session 5 is received, task 1 is added to the empty queue to generate the task queue of session 5. Then, session 5 is attached to the main queue to arrange the task queue of session 5 into the main queue, waiting for an idle thread to acquire and execute it.

[0073] If the queue that the input task depends on is currently an empty task queue, the enqueue process for the empty task queue is as follows: add the input task to the empty task queue, so that the input task can be executed before the thread occupied by the empty task queue is released.

[0074] In the dual-queue task processing flow of this invention embodiment, a special concurrent lock needs to be designed. Specifically, two mutex locks are needed to control the uniqueness of the total session queue and the task queue of each session, respectively. In addition, an atomic flag is needed to indicate whether each session is currently executing in a thread. The atomic flag can indicate whether the task queue is currently being executed in a thread or is currently in an idle state (i.e. waiting to be executed in a thread).

[0075] The task enqueue lock process in this embodiment of the invention is as follows: Figure 6 As shown. The current queue is locked, where the current queue is either the task queue of the session to which the input task belongs or an empty queue (no tasks have been added to this session). Figure 6 Taking the task queue of the session to which the task in the current queue belongs (i.e., the queue of this session or the task queue of this session) as an example; determine if the current queue is empty. If the current queue is not empty, add the input task to the current queue and end the enqueueing process; if the current queue is empty, determine if the task in the current queue is being executed in a thread (i.e., ...). Figure 6If the current queue is being executed in a thread, it means that the current queue is an empty task queue. In this case, the input task is added to the empty task queue, and the enqueueing ends. If the current queue is not being executed in a thread, it means that the current queue is an empty queue that has not been added to. In this case, the main queue (also known as the total session queue) is locked, the input task is added to the empty queue, thereby generating the task queue for this session. Then, the session is placed in the main queue to wait for an idle thread in the thread pool to take out the task queue for this session and execute the task, and the enqueueing ends.

[0076] The session processing flow in this embodiment of the invention employs a dual-queue approach to achieve concurrent session-coupled tasks within a thread pool environment. One main queue records all sessions currently requiring execution, while the other queue (task queue) records tasks currently queued for a session. This approach dispatches session tasks and assigns threads according to sessions, solving the thread reuse problem and ensuring that each thread operates efficiently. It also addresses the issue of excessive threads, typically resulting in a program having twice the number of hardware cores. Furthermore, it resolves the problem of frequent multi-threaded switching and low CPU utilization. Additionally, it addresses the issue of frequent thread creation and destruction. While isolating the coupling between tasks, it fully utilizes the thread pool, enabling thread pool reuse and resolving thread blocking issues (reducing the probability of blocking and significantly decreasing blocking time), thereby accelerating program concurrency and improving work efficiency.

[0077] Figure 7 This is a schematic diagram of the main modules of a task execution device according to an embodiment of the present invention.

[0078] The task execution device 700 of this invention mainly includes a queue generation module 701 and a task execution module 702.

[0079] The queue generation module 701 is used to generate task queues according to the dependencies between tasks and to sort the task queues in the main queue. The dependencies between tasks determine the execution order of tasks in the task queues, and the main queue determines the execution order of each task queue.

[0080] The queue generation module 701 can be used to: determine whether the input task has a corresponding task queue based on the dependency relationship between tasks; if so, add the input task to the corresponding task queue; otherwise, generate a new task queue for the input task and arrange the new task queue in the main queue.

[0081] The queue generation module 701 may include a judgment submodule, used to: determine whether the queue that the input task depends on is currently empty based on the dependency relationship between tasks; if so, determine whether the empty queue is being executed in a thread; if so, the input task has a corresponding task queue; if not, the input task does not have a corresponding task queue; if the queue that the input task depends on is not currently empty, the input task has a corresponding task queue.

[0082] The queue generation module 701 may also include a locking submodule for locking empty queues that are not being executed in a thread (i.e., empty queues that are not currently being executed in a thread).

[0083] The queue generation module 701 may also include a task queue generation submodule, which is used to: lock the main queue and add the input tasks to an empty queue that is not being executed in a thread to generate a new task queue.

[0084] The task execution module 702 is used to reuse the threads in the thread pool and execute the tasks in each task queue according to the order of the task queues in the main queue.

[0085] Task execution module 702 can be specifically used when there are idle threads in the thread pool.

[0086] Based on the number of idle threads, and in accordance with the order of the task queues in the main queue, a task queue with the number of idle threads is obtained from the main queue. An idle thread is allocated to each obtained task queue to execute the tasks in the obtained task queue. Whenever all tasks in a task queue are completed, the thread occupied by that task queue is released so that the released idle thread can continue to execute the tasks in the remaining task queues in the main queue, until all tasks in the task queues in the main queue are completed.

[0087] Furthermore, the specific implementation details of the task execution device described in the embodiments of the present invention have been described in detail in the task execution method described above, so the details will not be repeated here.

[0088] Figure 8 An exemplary system architecture 800 is shown that can be applied to the task execution method or task execution apparatus of the present invention.

[0089] like Figure 8 As shown, system architecture 800 may include terminal devices 801, 802, and 803, a network 804, and a server 805. Network 804 serves as the medium for providing communication links between terminal devices 801, 802, and 803 and server 805. Network 804 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.

[0090] Users can use terminal devices 801, 802, and 803 to interact with server 805 via network 804 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 801, 802, and 803, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).

[0091] Terminal devices 801, 802, and 803 can be various electronic devices with displays and web browsing capabilities, including but not limited to smartphones, tablets, laptops, and desktop computers.

[0092] Server 805 can be a server providing various services, such as a backend management server supporting shopping websites browsed by users using terminal devices 801, 802, and 803 (for example only). The backend management server can analyze and process data such as received product information query requests, and feed back the processing results (such as target push information and product information—for example only) to the terminal devices.

[0093] It should be noted that the task execution method provided in the embodiments of the present invention is generally executed by the server 805, and correspondingly, the task execution device is generally set in the server 805.

[0094] It should be understood that Figure 8 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.

[0095] The following is for reference. Figure 9 It shows a schematic diagram of the structure of a computer system 900 suitable for implementing terminal devices or servers in the embodiments of this application. Figure 9 The terminal device or server shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0096] like Figure 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 902 or programs loaded from storage section 908 into random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the system 900. The CPU 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0097] The following components are connected to I / O interface 905: an input section 906 including a keyboard, mouse, etc.; an output section 907 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 908 including a hard disk, etc.; and a communication section 909 including a network interface card such as a LAN card, modem, etc. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to I / O interface 905 as needed. A removable medium 911, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 910 as needed so that computer programs read from it can be installed into storage section 908 as needed.

[0098] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 909, and / or installed from removable medium 911. When the computer program is executed by central processing unit (CPU) 901, it performs the functions defined above in the system of this application.

[0099] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this application, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this application, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.

[0100] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0101] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be housed in a processor; for example, a processor may be described as including a queue generation module and a task execution module. The names of these modules do not necessarily limit the module itself; for example, a queue generation module may also be described as "a module for generating task queues according to the dependencies between tasks and sorting each task queue in a main queue."

[0102] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, which, when executed by the device, cause the device to include: generating task queues according to inter-task dependencies, and sorting each task queue in a main queue, wherein the inter-task dependencies determine the execution order of tasks in the task queues, and the main queue determines the execution order of each task queue; and reusing threads from a thread pool to execute tasks in each task queue according to the order of the task queues in the main queue.

[0103] According to the technical solution of this invention, a task queue is generated according to the dependencies between tasks, and each task queue is sorted in the main queue; threads in the thread pool are reused to execute tasks in each task queue according to the order of the task queues in the main queue. This invention uses a dual queue to achieve concurrent execution of coupled tasks in a thread pool environment. A main queue records all task queues to be executed, and each task queue records its own queued tasks. This solves the coupling problem between tasks, thereby solving the thread blocking problem. It has good thread reusability, eliminates the need for frequent thread switching, and achieves high memory resource utilization and CPU utilization. It also solves the problem of frequent thread creation and destruction, improves the overall system stability, and reduces the risk of memory leaks.

[0104] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A task execution method, characterized in that, include: A task queue is generated based on the dependencies between tasks, and each task queue is sorted in the main queue. The dependencies between tasks determine the execution order of tasks in the task queue. The main queue determines the execution order of each task queue according to the creation time of the task queue, which is the enqueue time of the first task in the task queue. Each task queue is given an atomicity flag, which indicates whether the task queue is currently being executed by a thread or is currently in an idle state. The threads in the thread pool are reused in a loop, and the tasks in each task queue are executed in the order of the task queues in the main queue. Each thread in the thread pool executes one task queue, and the thread occupied by the task queue is released after all the tasks in the task queue have been executed. A task queue is generated based on the dependencies between tasks, and each task queue is sorted in the main queue, including: determining whether the input task has a corresponding task queue based on the dependencies between tasks; If yes, the input task is added to the corresponding task queue; otherwise, a new task queue is generated for the input task, and the new task queue is arranged in the main queue.

2. The method according to claim 1, characterized in that, The steps to determine whether an input task has a corresponding task queue based on the dependencies between tasks include: Based on the dependencies between tasks, determine whether the queue to which the input task depends is currently empty. If yes, then determine whether the empty queue is being executed in the thread. If yes, then the input task has a corresponding task queue; otherwise, the input task does not have a corresponding task queue. If the queue that the input task depends on is not currently empty, then the input task has a corresponding task queue.

3. The method according to claim 2, characterized in that, Before the step of generating a new task queue for the input task, the following steps are included: Lock the empty queue that is not being executed in the thread; and, The step of generating a new task queue for the input task includes: The main queue is locked, and the input task is added to the empty queue that is not being executed in the thread, to generate the new task queue.

4. The method according to claim 1, characterized in that, The steps of reusing threads in the thread pool and executing tasks in each task queue according to the order of the task queues in the main queue include: When there are idle threads in the thread pool Based on the number of idle threads, and in accordance with the order of the task queues in the main queue, retrieve the task queue containing the number of idle threads from the main queue; Allocate an idle thread to each acquired task queue to execute the tasks in the task queue; Once all tasks in a task queue have been completed, the thread occupied by that task queue is released so that the released idle thread can continue to execute the tasks in the remaining task queues in the main queue, until all tasks in the main queue have been completed.

5. A task execution device, characterized in that, include: The queue generation module is used to generate task queues according to the dependencies between tasks, and sort each task queue in the main queue. The dependencies between tasks determine the execution order of tasks in the task queues. The main queue determines the execution order of each task queue according to the generation time of the task queue, which is the enqueue time of the first task in the task queue. Each task queue is given an atomicity flag, which indicates whether the task queue is currently being executed by a thread or is currently in an idle state. The task execution module is used to reuse the threads in the thread pool and execute the tasks in each task queue according to the order of the task queues in the main queue. Each thread in the thread pool executes one task queue, and the thread occupied by the task queue is released after all the tasks in the task queue have been executed. The queue generation module is further configured to: determine whether the input task has a corresponding task queue based on the dependency relationship between tasks; if so, add the input task to the corresponding task queue; otherwise, generate a new task queue for the input task and arrange the new task queue in the main queue.

6. The apparatus according to claim 5, characterized in that, The queue generation module includes a judgment submodule, used for: Based on the dependencies between tasks, determine whether the queue to which the input task depends is currently empty. If yes, then determine whether the empty queue is being executed in the thread. If yes, then the input task has a corresponding task queue; otherwise, the input task does not have a corresponding task queue. If the queue that the input task depends on is not currently empty, then the input task has a corresponding task queue.

7. The apparatus according to claim 6, characterized in that, The queue generation module includes a locking submodule, used for: Lock the empty queue that is not being executed in the thread; and, The queue generation module further includes a task queue generation submodule, used for: The main queue is locked, and the input task is added to the empty queue that is not being executed in the thread, to generate the new task queue.

8. The apparatus according to claim 5, characterized in that, The task execution module is also used for: When there are idle threads in the thread pool Based on the number of idle threads, and in accordance with the order of the task queues in the main queue, retrieve the task queue containing the number of idle threads from the main queue; Allocate an idle thread to each acquired task queue to execute the tasks in the task queue; Once all tasks in a task queue have been completed, the thread occupied by that task queue is released so that the released idle thread can continue to execute the tasks in the remaining task queues in the main queue, until all tasks in the main queue have been completed.

9. An electronic device, characterized in that, include: One or more processors; Memory, used to store one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the method as described in any one of claims 1-4.

10. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-4.