Multi-threaded task execution method, device, electronic device and storage medium
By adding multi-threaded tasks to the target task queue and transferring them to the engine task queue at one time, the problems of low execution efficiency and poor stability of multi-threaded tasks are solved, and task execution efficiency is optimized and the stability of open source code is maintained.
Patent Information
- Application Number
- CN202111539621.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-15
- Publication Date
- 2025-08-12
- Estimated Expiration
- 2041-12-15
AI Technical Summary
In the prior art, multi-threaded tasks are inefficient and have poor stability, especially after modifying the API in the quickjs engine, it may lead to unstable operation, affecting the stability of open source code.
Add multi-threaded tasks to the target task queue, and when task execution instructions are detected, the target task queue is added to the engine task queue at one time as the target task, reducing the number of operations in the locked state, optimizing the execution efficiency of multi-threaded tasks, and not modifying the API used to add tasks in the engine.
It improves the execution efficiency of multi-threaded tasks, reduces the number of operations in locked state, avoids modification of engine API, and ensures the stability of open source code.
Smart Images

Figure CN114253720B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a multi-threaded task execution method, device, electronic device and storage medium. Background Art
[0002] Currently, when a domain model needs to execute a task, it is usually added directly to the target task queue of QuickJS. However, the QuickJS engine itself is single-threaded and does not support multi-threaded tasks. The only option is to modify the original API for adding tasks to the engine and lock the target task queue before operating it to prevent multi-threaded competition.
[0003] However, this approach presents the following issues: First, when thread A adds a task, if other threads hold a lock for a long time, the API call will not return in time, preventing thread A from performing other tasks. If module B, to which thread A belongs, has a thread pool with five threads, and all of them are unable to acquire the lock and are in a waiting state when adding a task, module B will face a situation where no threads are available, seriously affecting execution efficiency. Second, the native QuickJS task addition API does not support multi-threaded task calls. Modifying it could lead to unstable operation and undermine the stability of the open source code. Summary of the Invention
[0004] The present invention provides a multi-threaded task execution method, device, electronic device and storage medium, which are used to solve the defects of low efficiency and poor stability of multi-threaded task execution in the prior art.
[0005] The present invention provides a multi-threaded task execution method, comprising:
[0006] Determine a multi-threaded task to be executed, and add the multi-threaded task to a target task queue;
[0007] When a task execution instruction is detected, the target task queue is added as a target task to the engine task queue, and the multi-threaded task is executed.
[0008] According to a multi-threaded task execution method provided by the present invention, adding the target task queue as a target task to the engine task queue includes:
[0009] After setting the task lock of the target task to a locked state, the target task is added to the engine task queue.
[0010] According to a multi-threaded task execution method provided by the present invention, after adding the target task to the engine task queue, the method further includes:
[0011] Switch the task lock of the target task from a locked state to a released state.
[0012] According to a multi-threaded task execution method provided by the present invention, executing the multi-threaded task includes:
[0013] Execute each thread task in sequence according to the arrangement order of the multi-thread tasks in the target task queue.
[0014] According to a multi-threaded task execution method provided by the present invention, after executing the multi-threaded task, the method further includes:
[0015] Obtaining the multi-threaded task return value, and performing statistics on the multi-threaded task return value to obtain an execution result;
[0016] The execution result includes a set of thread tasks that failed to execute, a set of thread tasks that succeeded to execute, the number of the set of thread tasks that failed to execute, and the number of the set of thread tasks that succeeded to execute.
[0017] According to a multi-threaded task execution method provided by the present invention, after obtaining the multi-threaded task return value, the method further includes: writing the multi-threaded task return value back to a database.
[0018] According to a multi-threaded task execution method provided by the present invention, the task execution instruction is generated in the preparation stage of the JS main thread.
[0019] The present invention also provides a multi-threaded task execution device, comprising:
[0020] a determining unit, configured to determine a multi-threaded task to be executed and add the multi-threaded task to a target task queue;
[0021] The execution unit is configured to add the target task queue as a target task to the engine task queue when a task execution instruction is detected, and execute the multi-threaded task.
[0022] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of any of the multi-threaded task execution methods described above are implemented.
[0023] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of any of the multi-threaded task execution methods described above.
[0024] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the steps of any of the multi-threaded task execution methods described above.
[0025] The multi-threaded task execution method, apparatus, electronic device, and storage medium provided by the present invention reduce the number of lock acquisitions required by multi-threaded tasks and optimize their execution efficiency by adding the multi-threaded tasks to a target task queue and then adding the target task queue as the target task to the engine task queue. Furthermore, embodiments of the present invention do not require modifications to the API used to add tasks in the main thread engine, ensuring the stability of the open-source code. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction is given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0027] Figure 1 This is one of the flow charts of the multi-threaded task execution method provided by the present invention;
[0028] Figure 2 It is a flowchart of the multi-threaded task execution method in the traditional method;
[0029] Figure 3 This is the second flow chart of the multi-threaded task execution method provided by the present invention;
[0030] Figure 4 It is a structural diagram of the multi-threaded task execution device provided by the present invention;
[0031] Figure 5 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0032] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0033] Currently, when a domain model needs to execute a task, it is usually added directly to the target task queue of QuickJS. However, the QuickJS engine itself is single-threaded and does not support multi-threaded tasks. The only option is to modify the original API for adding tasks to the engine and lock the target task queue before operating it to prevent multi-threaded competition.
[0034] However, this approach presents the following issues: First, when thread A adds a task, if other threads hold a lock for a long time, the API call will not return in time. Since the interface is synchronous, thread A cannot perform other tasks. If module B, to which thread A belongs, has a thread pool with five threads, and all of them are unable to acquire a lock when adding a task and are in a waiting state, module B will face a situation where no threads are available, seriously affecting execution efficiency. Second, the native QuickJS task addition API does not support multi-threaded task calls. Modifications to this API may lead to unstable operation, undermining the stability of the open source code and failing to cover the existing test cases.
[0035] To this end, the present invention provides a multi-threaded task execution method. Figure 1 It is a flowchart of the multi-threaded task execution method provided by the present invention, such as Figure 1 As shown, the method includes the following steps:
[0036] Step 110: Determine the multi-threaded task to be executed and add the multi-threaded task to the target task queue;
[0037] Step 120: When a task execution instruction is detected, the target task queue is added to the engine task queue as a target task, and the multi-threaded task is executed.
[0038] Here, multithreading is about enabling multiple threads to work in parallel to complete multiple tasks, thereby improving system efficiency. Threads are implemented when multiple tasks need to be completed simultaneously. Multithreading means that users can run multiple applications simultaneously, with each application being called a task. For example, if you are browsing the Internet while listening to MP3s and doing homework in Word, you are running at least three tasks simultaneously.
[0039] It should be noted that in the multi-threaded programming process, the JS (JavaScript) main thread needs to wait for the processing results of multiple child threads before continuing to run. For this task, the traditional method of writing code to lock and control the timing each time is time-consuming and labor-intensive. Figure 2As shown, when there are Thread A, Thread C, Thread X, and Thread Z tasks, the traditional method first sets the task lock of Thread A's task to the locked state, adds Thread A to the engine task queue, and then switches the task lock of Thread A from the locked state to the released state, allowing the JS main thread to execute Thread A's task. After Thread A's task is executed, the task lock of Thread A's task is switched from the released state to the locked state, and the task lock of Thread C's task is set to the locked state. Thread C is then added to the engine task queue, and then the task lock of Thread C is switched from the locked state to the released state, allowing the JS main thread to execute Thread C's task. Similarly, according to the above method, the JS main thread executes Thread X and Thread Z tasks in sequence.
[0040] It can be seen from this that in the traditional method of multi-threaded task execution, when executing thread A tasks, other thread tasks are in a waiting state, and each time a thread task is acquired and executed, it is necessary to lock (lock) and unlock (release), which seriously affects the execution efficiency of the task. In addition, in some scenarios, it is easy to be blocked when calling the "add engine task api", which in turn affects the execution efficiency of the thread task. Moreover, if it is the thread where the engine is located, if you want to get the task from the engine task queue, you also need to obtain a lock, and there is also a risk of being blocked, which means that the efficiency of the engine's task execution is affected. In addition, the QuickJS native api for adding engine tasks does not support multi-threaded calls. After modification, it may face the problem of unstable operation, which will destroy the stability of the open source code, and the original test cases of the open source code cannot be covered.
[0041] Therefore, after determining the multi-threaded task to be executed, the embodiment of the present invention adds the multi-threaded task to the target task queue, so that when the task execution instruction is detected, the target task queue is added as the target task to the engine task queue of the JS main thread, and the multi-threaded task is executed. Figure 3 As shown, when there are thread A, thread C, thread X and thread Z tasks, the embodiment of the present invention first adds thread A, thread C, thread X and thread Z tasks to the target task queue. When the JS main thread enters the prepare work stage, the task lock of the target task queue is set to a locked state. At this time, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time. Then, the task lock of the target task is switched from a locked state to a released state, so that the engine task queue can obtain each thread task from the target task and execute thread A, thread C, thread X and thread Z tasks in sequence.
[0042] It can be seen that in the traditional method, each time a thread task is executed, the state of the task lock needs to be operated. However, the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required on the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0043] The multithreaded task execution method provided by the present invention reduces the number of lock acquisitions required by multithreaded tasks and optimizes their execution efficiency by adding them to a target task queue and then adding the target task queue as the target task to the engine task queue. Furthermore, the present invention eliminates the need to modify the engine's API for adding tasks within the JS main thread, ensuring the stability of the open-source code.
[0044] Based on the above embodiment, adding the target task queue as the target task to the engine task queue includes:
[0045] After setting the task lock of the target task to the locked state, add the target task to the engine task queue.
[0046] Specifically, when the multi-threaded task to be executed is added to the target task queue, when a task execution instruction is detected, the target task queue is added as the target task to the engine task queue, and the multi-threaded task is executed.
[0047] Before the engine task queue obtains the target task, in order to prevent other tasks from being added to the target task, the task lock of the target task is set to a locked state, that is, other tasks cannot be added to the target task at this time, and then the target task can be added to the engine task queue, waiting for the JS main thread to execute the target task.
[0048] It can be seen that in the traditional method, each time a thread task is executed, the state of the task lock needs to be operated. However, the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required on the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0049] The multithreaded task execution method provided by the present invention reduces the number of lock acquisitions required by multithreaded tasks and optimizes their execution efficiency by adding the multithreaded task to a target task queue, setting the target task's task lock to a locked state, and then adding the target task to the engine task queue. Furthermore, the present invention eliminates the need to modify the engine's API for adding tasks within the JS main thread, ensuring the stability of the open-source code.
[0050] Based on any of the above embodiments, after adding the target task to the engine task queue, the method further includes:
[0051] Switch the task lock of the target task from the locked state to the released state.
[0052] Specifically, after the target task is added to the engine task queue, since the task lock of the target task is in a locked state at this time, other tasks cannot be added to the target task queue corresponding to the target task. If the next thread task is to be added to the target task queue corresponding to the target task, the task lock of the target task needs to be switched from a locked state to a released state, so that the next thread task can be added to the target task queue corresponding to the target task.
[0053] Based on any of the above embodiments, executing a multi-threaded task includes:
[0054] Execute each thread task in sequence according to the order in which the multi-thread tasks in the target task queue are arranged.
[0055] like Figure 3 As shown, when there are thread A, thread C, thread X and thread Z tasks, the embodiment of the present invention first adds thread A, thread C, thread X and thread Z tasks to the target task queue. When the JS main thread enters the prepare work stage, the task lock of the target task queue is set to a locked state. At this time, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time. Then, the task lock of the target task is switched from a locked state to a released state, so that the engine task queue can obtain each thread task from the target task and execute thread A, thread C, thread X and thread Z tasks in sequence.
[0056] Thus, the embodiment of the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. It only needs to operate once to change the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0057] The multithreaded task execution method provided by the present invention reduces the number of lock acquisitions required by multithreaded tasks and optimizes their execution efficiency by adding the multithreaded task to a target task queue, setting the target task's task lock to a locked state, and then adding the target task to the engine task queue. Furthermore, the present invention eliminates the need to modify the engine's API for adding tasks within the JS main thread, ensuring the stability of the open-source code.
[0058] Based on any of the above embodiments, after executing the multi-threaded task, the method further includes:
[0059] Get the return value of multi-threaded tasks and count the return values of multi-threaded tasks to get the execution results;
[0060] The execution result includes the thread task set that failed to execute, the thread task set that succeeded to execute, the number of the thread task set that failed to execute, and the number of the thread task set that succeeded to execute.
[0061] Specifically, the multi-threaded task return value may include the execution status information of each thread task, such as the execution status of each thread task (failed or successful). After obtaining the multi-threaded task return value, the multi-threaded task return value may be counted to obtain the execution result, such as the set of failed thread tasks, the set of successfully executed thread tasks, the number of failed thread task sets, and the number of successfully executed thread task sets.
[0062] It is understood that after the execution results are statistically obtained, they can be analyzed so that R&D personnel can improve the program based on the analysis results. For example, the analysis can determine the proportion of successfully executed thread tasks in a multi-threaded task, the proportion of failed thread tasks, and the frequency of errors in executing thread tasks.
[0063] Based on any of the above embodiments, after obtaining the multi-threaded task return value, the method further includes: writing the multi-threaded task return value back to the database.
[0064] Specifically, the process of writing the return value of the multi-threaded task back to the database includes: after all multi-threaded tasks are executed, the execution results of each thread task, whether successful or failed, will be recorded in the corresponding successful execution thread task set or failed execution thread task set. In addition, the recorded content also includes the number of successful execution thread task sets and the number of failed execution thread tasks. The merging of all failed execution thread task sets and successful execution thread task sets is the execution result of the multi-threaded task.
[0065] It is understandable that whether further processing is required can be determined based on actual needs, such as writing exception records for all thread task sets that failed to execute, or updating the status of thread task sets that successfully executed.
[0066] Based on any of the above embodiments, the task execution instruction is generated in the preparation phase of the JS main thread.
[0067] Specifically, when the JS main thread enters the prepare work phase (prepare work phase), a task execution instruction is generated. After detecting the task execution instruction, the task lock of the target task queue is set to a locked state. At this time, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time. Then, the task lock of the target task is switched from a locked state to a released state, so that the engine task queue can obtain each thread task from the target task.
[0068] The embodiment of the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required to change the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to source code stability caused by modifying the API in traditional methods.
[0069] The multithreaded task execution method provided by the present invention reduces the number of lock acquisitions required by multithreaded tasks and optimizes their execution efficiency by adding them to a target task queue and then adding the target task queue as the target task to the engine task queue. Furthermore, the present invention eliminates the need to modify the engine's API for adding tasks within the JS main thread, ensuring the stability of the open-source code.
[0070] The multi-threaded task execution device provided by the present invention is described below. The multi-threaded task execution device described below and the multi-threaded task execution method described above can be referenced to each other.
[0071] Based on any of the above embodiments, the present invention also provides a multi-threaded task execution device, such as Figure 4 As shown, the device includes:
[0072] A determining unit 410 is configured to determine a multi-threaded task to be executed and add the multi-threaded task to a target task queue;
[0073] The execution unit 420 is configured to add the target task queue as a target task to the engine task queue when a task execution instruction is detected, and execute the multi-threaded task.
[0074] Here, multithreading is about enabling multiple threads to work in parallel to complete multiple tasks, thereby improving system efficiency. Threads are implemented when multiple tasks need to be completed simultaneously. Multithreading means that users can run multiple applications simultaneously, with each application being called a task. For example, if you are browsing the Internet while listening to MP3s and doing homework in Word, you are running at least three tasks simultaneously.
[0075] It's important to note that in multi-threaded programming, the JS (JavaScript) main thread must wait for the processing results of multiple child threads before continuing. For this task, traditional methods require writing lock code to control the timing each time, which is time-consuming and labor-intensive. This can also be understood as the traditional multi-threaded task execution method. While a particular thread is executing, other threads are in a waiting state. Furthermore, each time a thread task is retrieved and executed, a lock (lock) and unlock (release) must be performed, severely impacting task execution efficiency. Furthermore, in some scenarios, calling the "Add Engine Task API" is prone to blocking, further impacting thread task execution efficiency. Furthermore, if the engine thread is retrieving a task from the engine task queue, a lock must also be acquired, which also carries the risk of blocking, thus impacting engine task execution efficiency. Furthermore, the native QuickJS API for adding engine tasks does not support multi-threaded calls. Modifications to this API could lead to unstable operation, undermining the stability of the open source code and failing to cover the existing test cases.
[0076] Therefore, after determining the multi-threaded task to be executed, the embodiment of the present invention adds the multi-threaded task to the target task queue, so that when the task execution instruction is detected, the target task queue is added as the target task to the engine task queue at one time, and the multi-threaded task is executed.
[0077] It can be seen that in the traditional method, each time a thread task is executed, the state of the task lock needs to be operated. However, the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required on the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0078] The multi-threaded task execution device provided by the present invention reduces the number of lock acquisitions required by multi-threaded tasks and optimizes their execution efficiency by adding them to a target task queue and then adding the target task queue as the target task to the engine task queue. Furthermore, the present invention eliminates the need to modify the API used to add tasks in the main thread engine, ensuring the stability of the open-source code.
[0079] Based on any of the above embodiments, the execution unit 420 is configured to:
[0080] After setting the task lock of the target task to a locked state, the target task is added to the engine task queue.
[0081] Specifically, when the multi-threaded task to be executed is added to the target task queue, when a task execution instruction is detected, the target task queue is added as the target task to the engine task queue, and the multi-threaded task is executed.
[0082] Before the engine task queue obtains the target task, in order to prevent other tasks from being added to the target task, the task lock of the target task is set to a locked state, that is, other tasks cannot be added to the target task at this time, and then the target task can be added to the engine task queue, waiting for the JS main thread to execute the target task.
[0083] It can be seen that in the traditional method, each time a thread task is executed, the state of the task lock needs to be operated. However, the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required on the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0084] Based on any of the above embodiments, the further comprising:
[0085] The switching unit is configured to switch the task lock of the target task from a locked state to a released state after the target task is added to the engine task queue.
[0086] Specifically, after the target task is added to the engine task queue, since the task lock of the target task is in a locked state at this time, other tasks cannot be added to the target task queue corresponding to the target task. If the next thread task is to be added to the target task queue corresponding to the target task, the task lock of the target task needs to be switched from a locked state to a released state, so that the next thread task can be added to the target task queue corresponding to the target task.
[0087] Based on any of the above embodiments, the execution unit 420 is configured to:
[0088] Execute each thread task in sequence according to the arrangement order of the multi-thread tasks in the target task queue.
[0089] Specifically, if there are thread A, thread C, thread X and thread Z tasks, the embodiment of the present invention first adds thread A, thread C, thread X and thread Z tasks to the target task queue. When the JS main thread enters the prepare work stage, the task lock of the target task queue is set to a locked state. At this time, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time. Then, the task lock of the target task is switched from a locked state to a released state, so that the engine task queue can obtain each thread task from the target task and execute thread A, thread C, thread X and thread Z tasks in sequence.
[0090] Thus, the embodiment of the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. It only needs to operate once to change the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to the stability of the source code caused by modifying the API in the traditional method.
[0091] Based on any of the above embodiments, the further comprising:
[0092] A statistics unit, configured to obtain a return value of the multi-threaded task after executing the multi-threaded task, and to perform statistics on the return value of the multi-threaded task to obtain an execution result;
[0093] The execution result includes a set of thread tasks that failed to execute, a set of thread tasks that succeeded to execute, the number of the set of thread tasks that failed to execute, and the number of the set of thread tasks that succeeded to execute.
[0094] Specifically, the multi-threaded task return value may include the execution status information of each thread task, such as the execution status of each thread task (failed or successful). After obtaining the multi-threaded task return value, the multi-threaded task return value may be counted to obtain the execution result, such as the set of failed thread tasks, the set of successfully executed thread tasks, the number of failed thread task sets, and the number of successfully executed thread task sets.
[0095] It is understood that after the execution results are statistically obtained, they can be analyzed so that R&D personnel can improve the program based on the analysis results. For example, the analysis can determine the proportion of successfully executed thread tasks in a multi-threaded task, the proportion of failed thread tasks, and the frequency of errors in executing thread tasks.
[0096] Based on any of the above embodiments, it also includes: a write-back unit, which is used to write the multi-threaded task return value back to the database after obtaining the multi-threaded task return value.
[0097] Specifically, the process of writing the return value of the multi-threaded task back to the database includes: after all multi-threaded tasks are executed, the execution results of each thread task, whether successful or failed, will be recorded in the corresponding successful execution thread task set or failed execution thread task set. In addition, the recorded content also includes the number of successful execution thread task sets and the number of failed execution thread tasks. The merging of all failed execution thread task sets and successful execution thread task sets is the execution result of the multi-threaded task.
[0098] It is understandable that whether further processing is required can be determined based on actual needs, such as writing exception records for all thread task sets that failed to execute, or updating the status of thread task sets that successfully executed.
[0099] Based on any of the above embodiments, the task execution instruction is generated in the preparation phase of the JS main thread.
[0100] Specifically, when the JS main thread enters the prepare work phase (prepare work phase), a task execution instruction is generated. After detecting the task execution instruction, the task lock of the target task queue is set to a locked state. At this time, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time. Then, the task lock of the target task is switched from a locked state to a released state, so that the engine task queue can obtain each thread task from the target task.
[0101] The embodiment of the present invention transfers multiple thread tasks to the target task queue at one time, and adds the target task queue as the target task to the engine task queue. Only one operation is required to change the state of the task lock, which reduces the waiting time of other threads and optimizes the efficiency of thread execution. At the same time, the JS main thread of the embodiment of the present invention is to obtain the target task, which can be understood as obtaining a single-threaded task. In this way, there is no need to modify the original API of the engine to add tasks, avoiding the problem of unstable operation and damage to source code stability caused by modifying the API in traditional methods.
[0102] Figure 5 Schematic diagram of the structure of the electronic device provided by the present invention, such as Figure 5 As shown, the electronic device may include: a processor 510, a memory 520, a communication interface 530, and a communication bus 540, wherein the processor 510, the memory 520, and the communication interface 530 communicate with each other via the communication bus 540. The processor 510 may call logic instructions in the memory 520 to execute a multi-threaded task execution method, which includes: determining a multi-threaded task to be executed and adding the multi-threaded task to a target task queue; when a task execution instruction is detected, adding the target task queue as a target task to an engine task queue, and executing the multi-threaded task.
[0103] In addition, the logic instructions in the above-mentioned memory 520 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when sold or used as an independent product. 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 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.
[0104] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the multi-threaded task execution method provided by the above methods, which includes: determining a multi-threaded task to be executed, and adding the multi-threaded task to a target task queue; when a task execution instruction is detected, adding the target task queue as a target task to the engine task queue, and executing the multi-threaded task.
[0105] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the multi-threaded task execution method provided above, the method comprising: determining a multi-threaded task to be executed, and adding the multi-threaded task to a target task queue; when a task execution instruction is detected, adding the target task queue as a target task to an engine task queue, and executing the multi-threaded task.
[0106] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0107] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, or of course, by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or certain parts of the embodiments.
[0108] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention.
Claims
1. A multi-threaded task execution method, characterized in that: include: Determine multiple thread tasks to be executed, and add the multiple thread tasks to the target task queue at one time; When a task execution instruction is detected, the target task queue is added as a target task to the engine task queue, and the multiple thread tasks are executed; Adding the target task queue as a target task to the engine task queue includes: After setting the task lock of the target task to a locked state, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time, and then the task lock of the target task is switched from a locked state to a released state, so that the engine task queue obtains each thread task from the target task.
2. The multi-threaded task execution method according to claim 1, characterized in that: After adding the target task to the engine task queue, the method further includes: Switch the task lock of the target task from a locked state to a released state.
3. The multi-threaded task execution method according to claim 1, characterized in that: The executing of the multiple thread tasks includes: Execute each thread task in sequence according to the arrangement order of the multiple thread tasks in the target task queue.
4. The multi-threaded task execution method according to claim 1, wherein: After executing the multiple thread tasks, the method further includes: Obtaining the multiple thread task return values, and performing statistics on the multiple thread task return values to obtain execution results; The execution result includes a set of thread tasks that failed to execute, a set of thread tasks that succeeded to execute, the number of the set of thread tasks that failed to execute, and the number of the set of thread tasks that succeeded to execute.
5. The multi-threaded task execution method according to claim 4, characterized in that: After obtaining the multiple thread task return values, the method further includes: writing the multiple thread task return values back to a database.
6. The multi-threaded task execution method according to any one of claims 1 to 5, characterized in that: The task execution instruction is generated in the preparation phase of the JS main thread.
7. A multi-threaded task execution device, characterized in that: include: a determination unit, configured to determine a plurality of thread tasks to be executed, and add the plurality of thread tasks to a target task queue at one time; an execution unit, configured to add the target task queue as a target task to the engine task queue when a task execution instruction is detected, and execute the multiple thread tasks; Adding the target task queue as a target task to the engine task queue includes: After setting the task lock of the target task to a locked state, other tasks cannot be added to the target task queue, and the target task queue is transferred to the engine task queue as the target task at one time, and then the task lock of the target task is switched from a locked state to a released state, so that the engine task queue obtains each thread task from the target task.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the multi-threaded task execution method according to any one of claims 1 to 6 are implemented.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the multi-threaded task execution method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Task execution method and device
CN111290842A