Multi-thread processing method, system and equipment, medium and computer program product
By sorting tasks and dividing thread priority for multi-threaded processing, the problems of long thread execution time and low efficiency in the existing technology are solved, and more efficient task processing is achieved.
Patent Information
- Application Number
- CN202411713709.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-04
AI Technical Summary
The existing multi-threaded processing methods have problems of long execution time and low efficiency, especially in the process of task pause and wake-up, which fails to effectively shorten thread execution time.
By obtaining multiple tasks corresponding to user operations, sorting them according to task dependencies and priorities, and dividing execution priorities of multiple threads, determining the tasks processed by each thread, thereby optimizing the execution order and resource allocation of threads.
This method effectively shortens the execution time of threads, improves the efficiency of thread processing, and ensures that tasks are executed smoothly according to priority and dependency.
Smart Images

Figure CN119557069A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of thread processing, and in particular to a multi-thread processing method, system, device, medium and computer program product. Background Art
[0002] Thread technology is one of the important technologies used in the computer field to improve program execution efficiency. It allows the program to execute multiple tasks at the same time, thereby improving the overall processing performance. At present, the development and application of thread technology are quite mature. The thread processing method in the prior art is applied to an electronic device, and the electronic device includes a condition variable module and multiple threads, and the multiple threads include a first thread and a second thread. It includes: when the first thread suspends the execution of the first task due to waiting for the target condition variable, the first thread obtains the thread information of the second thread. Among them, the second thread is a thread that wakes up the target condition variable by executing the second task. The first thread increases the priority of the second thread. When the second thread wakes up the target condition variable by executing the second task, the first thread continues to execute the first task. However, the existing method is to suspend the execution of the second task when the dependent variable conflicts. This type of execution method does not judge the priority because of the state of the task itself, but only judges the order of execution. In addition, this method does not shorten the execution time of the entire thread, but only reduces the time from suspending to waking up the task, and the thread processing efficiency is not high enough. Summary of the invention
[0003] The technical problem to be solved by the present disclosure is to overcome the defects of long execution time and low execution efficiency of the entire thread in the multi-thread processing mode in the prior art, and to provide a multi-thread processing method, system, device, medium and computer program product.
[0004] The present invention solves the above technical problems through the following technical solutions:
[0005] A first aspect of the present disclosure provides a multi-thread processing method, the processing method comprising:
[0006] Get multiple tasks corresponding to user operations;
[0007] Sorting the multiple tasks according to task dependencies and task priorities to obtain a task sorting result;
[0008] Acquire multiple threads, and divide the execution priorities of the multiple threads to obtain thread execution priorities;
[0009] The task to be processed by each thread is determined according to the thread execution priority and the task sorting result.
[0010] Preferably, the processing method further comprises:
[0011] In response to a factor affecting normal execution of a task occurring in a certain thread, the current thread execution priority of the thread is suspended.
[0012] Preferably, the processing method further comprises:
[0013] Loading data information corresponding to the multiple tasks into a preprocessing task pool;
[0014] and / or,
[0015] The step of obtaining a plurality of threads, dividing the execution priorities of the plurality of threads, and obtaining the thread execution priorities comprises:
[0016] Acquire multiple threads, and divide the execution priorities of the multiple threads according to the resource ratio of each thread to obtain the thread execution priority;
[0017] Among them, the thread execution priority is positively correlated with the resource share of the thread.
[0018] Preferably, the processing method further comprises:
[0019] In response to the disappearance of a factor affecting normal execution of a task of a certain thread, restoring the thread execution priority of the thread;
[0020] and / or,
[0021] The step of sorting the multiple tasks according to task dependencies and task priorities to obtain task sorting results comprises:
[0022] Sorting the multiple tasks according to the task priorities to obtain a priority sorting result;
[0023] The priority sorting result is adjusted according to the dependency to obtain a task sorting result.
[0024] Preferably, the processing method further comprises:
[0025] In response to determining at each preset time interval that the current thread execution priority of a certain thread is still in a paused state, the thread execution priority corresponding to the thread with the next thread priority is increased until the current thread priority of the certain thread is restored from the paused state to the ready state.
[0026] Preferably, in response to a factor affecting the normal execution of a task occurring in a certain thread, the step of suspending the current thread execution priority of the thread comprises:
[0027] In response to a factor affecting the normal execution of a task in a certain thread, the current thread execution priority of the thread is reduced, and the thread execution priority corresponding to the thread with the next thread priority is increased.
[0028] A second aspect of the present disclosure provides a multi-threaded processing system, the processing system comprising:
[0029] A first acquisition module, used to acquire multiple tasks corresponding to user operations;
[0030] A sorting module, used to sort the multiple tasks according to task dependencies and task priorities to obtain a task sorting result;
[0031] A second acquisition module is used to acquire multiple threads and divide the execution priorities of the multiple threads to obtain thread execution priorities;
[0032] The determination module is used to determine the task to be processed by each thread according to the thread execution priority and the task sorting result.
[0033] Preferably, the processing system further comprises:
[0034] The first processing module is used to suspend the current thread execution priority of a thread in response to a factor affecting the normal execution of a task in the thread.
[0035] Preferably, the processing system further comprises:
[0036] A loading module, used for loading the data information corresponding to the plurality of tasks into a preprocessing task pool;
[0037] and / or,
[0038] The second acquisition module is used to acquire multiple threads, and divide the execution priorities of the multiple threads according to the resource ratio of each thread to obtain the thread execution priority;
[0039] Among them, the thread execution priority is positively correlated with the resource share of the thread.
[0040] Preferably, the processing system further comprises:
[0041] A second processing module, configured to restore the thread execution priority of a thread in response to the disappearance of a factor affecting the normal execution of a task of a certain thread;
[0042] and / or,
[0043] The sorting module comprises:
[0044] A sorting unit, used to sort the multiple tasks according to the task priorities to obtain a priority sorting result;
[0045] An adjustment unit is used to adjust the priority sorting result according to the dependency to obtain a task sorting result.
[0046] Preferably, the processing system further comprises:
[0047] The third processing module is used to respond to determining that the current thread execution priority of a certain thread is still in a paused state at each preset time interval, and to increase the thread execution priority corresponding to the thread with the next thread priority until the current thread priority of the certain thread is restored from the paused state to the ready state.
[0048] Preferably, the first processing module is used to reduce the current thread execution priority of a thread in response to factors affecting normal task execution in the thread, and increase the thread execution priority corresponding to the thread with the next thread priority.
[0049] A third aspect of the present disclosure provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and used to run on the processor, wherein the processor implements the multi-threaded processing method described in the first aspect when executing the computer program.
[0050] A fourth aspect of the present disclosure provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the multi-threaded processing method described in the first aspect is implemented.
[0051] A fifth aspect of the present disclosure provides a computer program product, including a computer program, which, when executed by a processor, implements the multi-thread processing method as described in the first aspect.
[0052] On the basis of being in accordance with the common sense in the art, the above-mentioned preferred conditions can be arbitrarily combined to obtain the preferred embodiments of the present disclosure.
[0053] The positive and progressive effects of this disclosure are:
[0054] The present invention sorts multiple tasks corresponding to user operations according to task dependencies and task priorities to obtain task sorting results; then divides the execution priorities of the obtained multiple threads to obtain thread execution priorities; and determines the tasks that each thread processes according to the thread execution priorities and the task sorting results, thereby shortening the execution time of the entire thread and improving the efficiency of thread processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 This is a flowchart of the multi-threaded processing method provided in Example 1 of the present disclosure.
[0056] Figure 2 A module diagram of a multi-threaded processing system provided in Embodiment 2 of the present disclosure.
[0057] Figure 3 This is a schematic diagram of the structure of an electronic device for implementing a multi-threaded processing method according to Embodiment 3 of the present disclosure. DETAILED DESCRIPTION
[0058] The present disclosure is further described below by way of examples, but the present disclosure is not limited to the scope of the examples.
[0059] Prefixes such as "first" and "second" are used in the embodiments of the present disclosure only to distinguish different description objects, and have no limiting effect on the position, order, priority, quantity or content of the described objects. The use of prefixes such as ordinal numbers to distinguish description objects in the embodiments of the present disclosure does not constitute a limitation on the described objects. For the statement of the described objects, please refer to the description in the context of the claims or embodiments, and no unnecessary limitation should be constituted due to the use of such prefixes. In addition, in the description of the present embodiment, unless otherwise specified, the meaning of "plurality" is two or more.
[0060] In the embodiments of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0061] Example 1
[0062] Figure 1 A flowchart of a multi-threaded processing method provided in Embodiment 1 of the present disclosure is shown in FIG. Figure 1 As shown, the processing method includes:
[0063] S1. Obtain multiple tasks corresponding to user operations;
[0064] In this embodiment, multiple operations generated when the user uses the system are obtained; and then, based on the operations generated by the system, multiple tasks generated by each operation are obtained.
[0065] Specifically, an operation can be understood as a user operation. One operation can generate multiple tasks. For example, booking a ticket by a user can be regarded as a user operation. Then the operating system for the user will generate multiple tasks, for example, Task 1: loading the user's personalized homepage; Task 2: loading recommended user attractions; Task 3: loading ticket purchase discount activities.
[0066] S2. Sort the multiple tasks according to task dependencies and task priorities to obtain a task sorting result;
[0067] In this embodiment, for example, if a user wants to book a ticket, the system will generate multiple tasks: Task 1: Loading the user's personalized homepage (priority one | resources required: user location, user permissions, user portrait); Task 2: Loading recommended user attractions (priority one | dependent on task 1); Task 3: Loading ticket purchase discounts (priority two); The above-generated tasks are loaded into the pre-processing task pool, where the resource items required for Task 1: user location, user permissions, and user portrait can be processed in advance in this pre-processing pool. The tasks in the above-mentioned pre-processing task pool are sorted according to task priority and task dependency, ensuring that high-priority tasks are processed first and dependent tasks are processed in order. For example, the sorting result is as follows: Task 1 > Task 2 > Task 3.
[0068] In this embodiment, the multiple tasks to be processed are sorted according to task priorities and task dependencies to ensure that tasks with high priorities are processed first.
[0069] Sort multiple tasks, for example, F = (aX + bY + cZ) / N, where a represents the remaining memory size feature variable, X represents the remaining memory size feature parameter, b represents the memory decay cycle feature variable, Y represents the memory decay cycle feature parameter, c represents the memory change trend feature variable, Z represents the memory change trend feature parameter, and N is a normalization parameter. The number of task resources that can be processed in advance (i.e., the preprocessing pool size) is set according to the interval to which F belongs. F is positively correlated, which is expressed as the number of processing threads in the preprocessing pool.
[0070] S3, obtaining multiple threads, and dividing the execution priorities of the multiple threads to obtain the thread execution priorities;
[0071] In this embodiment, multiple threads are created through a thread pool, and execution priorities of the multiple threads are divided to obtain thread execution priorities.
[0072] Specifically, firstly, the execution priorities of multiple threads created by the thread pool are divided. For example, there are three threads in the thread pool (thread 1, thread 2, and thread 3). The execution priorities of the three threads are divided, and the result of the thread execution priority is: thread 1>thread 2>thread 3.
[0073] It should be noted that by dividing the execution priorities of multiple threads, different threads can be divided from high to low according to the execution priority, and the corresponding CPU execution time is also divided from long to short, so as to make the processing tasks (high priority) more efficient. For example: There are threads 1 and 2 in the current thread pool, and tasks to be processed (task priority is high, processing time is 700ms).
[0074] Scenario 1: Thread execution priority is not distinguished. In the CPU execution time per second, thread 1 and thread 2 share the same amount, that is, thread 1 (500ms), thread 2 (500ms). At this time, thread 1 needs to process the task: 500ms (thread 1 processing time) + 500ms (thread 1 waiting time when the CPU is turned to thread 2) + 200ms (thread 1 processing time) = 1200ms.
[0075] Scenario 2: Differentiate thread execution priorities. In the CPU execution time per second, thread 1 and thread 2 account for 7:3, that is, thread 1 (700ms), thread 2 (300ms). At this time, thread 1 needs: 700ms (thread 1 processing time). 700ms <1200ms. In summary, differentiating thread priorities will improve thread processing efficiency.
[0076] S4. Determine the task to be processed by each thread according to the thread execution priority and the task sorting result.
[0077] In this embodiment, the task sorting result is submitted to the thread pool, and the thread pool determines the task to be processed by each thread according to the thread execution priority and the task sorting result.
[0078] Specifically, the thread execution priority and task priority are combined to determine the execution task corresponding to each thread. For example, the thread execution priority of three threads is: thread 1>thread 2>thread 3. The task priority sorting result is: task 1>task 2>task 3. Then the execution task corresponding to each thread is: thread 1 executes task 1, thread 2 executes task 2, and thread 3 executes task 3. The higher the thread execution priority, the more CPU execution time it occupies.
[0079] In an optional embodiment, the processing method further includes:
[0080] S01. In response to a factor affecting normal execution of a task occurring in a certain thread, suspending the current thread execution priority of the thread.
[0081] In this embodiment, the suspension method is: lowering the execution priority of the thread and increasing the thread execution priority corresponding to the thread with the next execution priority.
[0082] Furthermore, step S01 includes:
[0083] In response to a factor affecting the normal execution of a task in a certain thread, the current thread execution priority of the thread is reduced, and the thread execution priority corresponding to the thread with the next thread priority is increased.
[0084] In this embodiment, when a factor affecting the normal execution of a task occurs in a thread, the current execution priority of the thread is suspended; the suspension method is: lowering the execution priority of the thread and increasing the thread execution priority corresponding to the thread with the next execution priority.
[0085] Specifically, if the thread execution priority ranking result of the three threads is: thread 1>thread 2>thread 3, it means that thread 1 has the highest thread execution priority, thread 2 has the second highest thread execution priority, and thread 3 has the lowest thread execution priority. When thread 1 encounters a factor that affects the normal execution of the task, thread 1 suspends execution and raises the thread execution priority of thread 2 to the highest. Then, the thread execution priorities of the three threads after reordering are: thread 2>thread 1>thread 3. In this way, it is ensured that idle threads with high execution priority will first process tasks with high task priority.
[0086] It should be noted that factors that affect the normal execution of tasks include: blocking or interruption.
[0087] In an optional embodiment, the processing method further includes:
[0088] Load the data information corresponding to multiple tasks into the preprocessing task pool;
[0089] In this embodiment, all resources (eg, data information) corresponding to all tasks are preloaded into the preprocessing task pool in advance.
[0090] Specifically, assume that the system resources required for Task 1 are A, B, and C; the system resources required for Task 2 are D, E, and F; and the system resources required for Task 3 are G, H, and I. Then the system resources AI can all be preloaded into the preprocessing task pool. Take Task 1 of loading the user's personalized homepage as an example. Assuming that the corresponding system resources are: user location, user permissions, and user portrait, the user location, user permissions, and user portrait are loaded into the preprocessing task pool in advance to reduce the time for task execution to load data, thereby improving the efficiency of task processing.
[0091] It should be noted that the role of the preprocessing task pool is to load the data information required by the next batch of task queues in advance to make up for the time difference in loading the next batch of data information by a single thread or multiple threads.
[0092] In an optional embodiment, step S3 includes:
[0093] Get multiple threads, and divide the execution priorities of the multiple threads according to the resource ratio of each thread to obtain the thread execution priority;
[0094] Among them, the thread execution priority is positively correlated with the resource share of the thread.
[0095] In this embodiment, the greater the resource proportion, the higher the thread execution priority.
[0096] In an optional embodiment, the processing method further includes:
[0097] In response to the disappearance of a factor affecting normal execution of a task of a certain thread, restoring the thread execution priority of the thread;
[0098] In this embodiment, when the factors affecting the normal execution of the task of thread 1 disappear, the thread execution priority of thread 1 is restored, that is, the thread execution priorities of the above three threads are restored to: thread 1>thread 2>thread 3.
[0099] In an optional embodiment, step S2 includes:
[0100] Sort multiple tasks according to their priorities to obtain priority sorting results;
[0101] The priority sorting results are adjusted according to the dependencies to obtain the task sorting results.
[0102] In this embodiment, all tasks are first sorted according to task priorities, and then the sorted results are adjusted according to task dependencies, so as to obtain a final task sorting result.
[0103] Specifically, the final task sorting results are sorted according to task priority and task dependency. First, sort by task priority to obtain the preliminary sorting result of the preliminary task. Assume that the preliminary sorting result of the preliminary task is: Task 1>Task 3>Task 2. Secondly, adjust the preliminary sorting results of the preliminary tasks according to the dependencies of the tasks. Assuming that Task 2 depends on Task 1 and Task 3 depends on Task 2, the final task sorting result is: Task 1>Task 2>Task 3. For example: Task 1 that loads the user's personalized homepage and Task 2 that loads recommended user attractions both have a task priority of one; Task 3 that loads ticket purchase discounts has a task priority of two, but Task 2 depends on Task 1. Therefore, the final task priority sorting result is: Task 1>Task 2>Task 3.
[0104] In an optional embodiment, the processing method further includes:
[0105] In response to determining at each preset time interval that the current thread execution priority of a certain thread is still in a paused state, the thread execution priority corresponding to the thread with the next thread priority is increased until the current thread priority of the certain thread is restored from the paused state to the ready state.
[0106] In this embodiment, the pause state of a certain thread is judged at a certain interval. If the judgment result is that the thread is still in the pause state, the thread execution priority corresponding to the thread with the next thread execution priority is raised in sequence until the current thread priority of a certain thread is restored from the pause state to the ready state.
[0107] Specifically, when the task in thread 1 is always suspended, the suspension state of thread 1 is determined at intervals. If the determination result of thread 1 is: still in the suspended state, the thread execution priority of thread 1 will continue to decrease. For example, if the original thread execution priority of thread 1 is greater than the thread execution priority of thread 3, the thread execution priority of thread 1 is lowered so that the thread execution priority of thread 3 is higher than the thread execution priority of thread 1, until thread 1 resumes execution, then the thread execution priority of thread 1 before suspension is restored.
[0108] In a specific implementation process, for example, three operations generated when a user uses the system are obtained, namely operation 1, operation 2, and operation 3.
[0109] The tasks generated by operation 1 are task 1, task 2, and task 3; the tasks generated by operation 2 are task 4, task 5, and task 6; the tasks generated by operation 3 are task 7, task 8, and task 9;
[0110] Add the data information corresponding to the 9 tasks from Task 1 to Task 9 to the preprocessing task pool; sort Task 1 to Task 9 according to task dependency and task priority, and the sorted result is: Task 1> Task 3> Task 5> Task 8> Task 7> Task 9> Task 2> Task 4> Task 6; submit all tasks to the thread pool for processing, and the thread pool will create multiple threads (two independent concepts from the above operation), and different threads have different thread execution priorities. The thread execution priorities of thread 1, thread 2, and thread 3 in the thread pool are determined as: thread 1> thread 2> thread 3. The threads of the above thread pool will execute the tasks sorted according to task dependency and task priority one by one according to the thread execution priority; for example, thread 1 executes task 1, and thread 2 executes task 3. After thread 1 executes task 1, the highest priority of the remaining tasks is task 5, then thread 1 continues to execute task 5, and so on, ensuring that idle threads with high execution priority threads first process tasks with high task priority (it should be noted that the priority of the task being executed is not considered).
[0111] Based on the above steps, when thread 1 executes task 1, if there are factors that affect the normal execution of the task, such as blocking or interruption, etc., the thread execution priority of thread 1 is reduced to thread execution priority 2, and the thread execution priority of thread 2 is increased to thread execution priority 1. Furthermore, when thread 1 resumes execution of task 1, the thread execution priority of thread 1 is restored to the thread execution priority before the downgrade.
[0112] The multithreaded processing method provided by the present disclosure, on the one hand, preloads all the resources corresponding to all tasks into the preprocessing task pool in advance, so that each thread can immediately call the resources in the preprocessing task pool during the task processing process, thereby saving the thread processing time; on the other hand, by sorting all tasks according to task dependencies and task priorities, and dividing the execution priorities of multiple threads, each thread can process tasks with higher priorities in a timely manner, thereby improving the efficiency of thread processing. On the other hand, when factors affecting the normal execution of tasks appear, when variables are affected, the thread with the second thread execution priority immediately takes over, thereby using space in exchange for high efficiency, greatly improving the fluency of the system.
[0113] Example 2
[0114] Corresponding to the aforementioned multi-threaded processing method embodiment, the present disclosure also provides an embodiment of a multi-threaded processing system.
[0115] Figure 2 A schematic diagram of a multi-threaded processing system provided in Embodiment 2 of the present disclosure is shown in FIG. Figure 2 As shown, the processing system includes:
[0116] A first acquisition module 21, used to acquire multiple tasks corresponding to user operations;
[0117] In this embodiment, multiple operations generated when the user uses the system are obtained; and then, based on the operations generated by the system, multiple tasks generated by each operation are obtained.
[0118] Specifically, an operation can be understood as a user operation. One operation can generate multiple tasks. For example, booking a ticket by a user can be regarded as a user operation. Then the operating system for the user will generate multiple tasks, for example, Task 1: loading the user's personalized homepage; Task 2: loading recommended user attractions; Task 3: loading ticket purchase discount activities.
[0119] A sorting module 22 is used to sort multiple tasks according to task dependencies and task priorities to obtain task sorting results;
[0120] In this embodiment, for example, if a user wants to book a ticket, the system will generate multiple tasks: Task 1: Loading the user's personalized homepage (priority one | resources required: user location, user permissions, user portrait); Task 2: Loading recommended user attractions (priority one | dependent on task 1); Task 3: Loading ticket purchase discounts (priority two); The above-generated tasks are loaded into the pre-processing task pool, where the resource items required for Task 1: user location, user permissions, and user portrait can be processed in advance in this pre-processing pool. The tasks in the above-mentioned pre-processing task pool are sorted according to task priority and task dependency, ensuring that high-priority tasks are processed first and dependent tasks are processed in order. For example, the sorting result is as follows: Task 1 > Task 2 > Task 3.
[0121] In this embodiment, the multiple tasks to be processed are sorted according to task priorities and task dependencies to ensure that tasks with high priorities are processed first.
[0122] Sort multiple tasks, for example, F = (aX + bY + cZ) / N, where a represents the remaining memory size feature variable, X represents the remaining memory size feature parameter, b represents the memory decay cycle feature variable, Y represents the memory decay cycle feature parameter, c represents the memory change trend feature variable, Z represents the memory change trend feature parameter, and N is a normalization parameter. The number of task resources that can be processed in advance (i.e., the preprocessing pool size) is set according to the interval to which F belongs. F is positively correlated, which is expressed as the number of processing threads in the preprocessing pool.
[0123] The second acquisition module 23 is used to acquire multiple threads and divide the execution priorities of the multiple threads to obtain the thread execution priorities;
[0124] In this embodiment, multiple threads are created through a thread pool, and execution priorities of the multiple threads are divided to obtain thread execution priorities.
[0125] Specifically, firstly, the execution priorities of multiple threads created by the thread pool are divided. For example, there are three threads in the thread pool (thread 1, thread 2, and thread 3). The execution priorities of the three threads are divided, and the result of the thread execution priority is: thread 1>thread 2>thread 3.
[0126] It should be noted that by dividing the execution priorities of multiple threads, different threads can be divided from high to low according to the execution priority, and the corresponding CPU execution time is also divided from long to short, so as to make the processing tasks (high priority) more efficient. For example: There are threads 1 and 2 in the current thread pool, and tasks to be processed (task priority is high, processing time is 700ms).
[0127] Scenario 1: Thread execution priority is not distinguished. In the CPU execution time per second, thread 1 and thread 2 share the same amount, that is, thread 1 (500ms), thread 2 (500ms). At this time, thread 1 needs to process the task: 500ms (thread 1 processing time) + 500ms (thread 1 waiting time when the CPU is turned to thread 2) + 200ms (thread 1 processing time) = 1200ms.
[0128] Scenario 2: Differentiate thread execution priorities. In the CPU execution time per second, thread 1 and thread 2 account for 7:3, that is, thread 1 (700ms), thread 2 (300ms). At this time, thread 1 needs: 700ms (thread 1 processing time). 700ms <1200ms. In summary, differentiating thread priorities will improve thread processing efficiency.
[0129] The determination module 24 is used to determine the task to be processed by each thread according to the thread execution priority and the task sorting result.
[0130] In this embodiment, the task sorting result is submitted to the thread pool, and the thread pool determines the task to be processed by each thread according to the thread execution priority and the task sorting result.
[0131] Specifically, the thread execution priority and task priority are combined to determine the execution task corresponding to each thread. For example, the thread execution priority of three threads is: thread 1>thread 2>thread 3. The task priority sorting result is: task 1>task 2>task 3. Then the execution task corresponding to each thread is: thread 1 executes task 1, thread 2 executes task 2, and thread 3 executes task 3. The higher the thread execution priority, the more CPU execution time it occupies.
[0132] In an optional embodiment, the processing system further includes:
[0133] The first processing module is used to suspend the current thread execution priority of a thread in response to a factor affecting the normal execution of a task in the thread.
[0134] In this embodiment, the suspension method is: lowering the execution priority of the thread and increasing the thread execution priority corresponding to the thread with the next execution priority.
[0135] Furthermore, the first processing module is used to reduce the current thread execution priority of a thread in response to a factor affecting the normal execution of a task in the thread, and increase the thread execution priority corresponding to the thread with the next thread priority.
[0136] In this embodiment, when a factor affecting the normal execution of a task occurs in a thread, the current execution priority of the thread is suspended; the suspension method is: lowering the execution priority of the thread and increasing the thread execution priority corresponding to the thread with the next execution priority.
[0137] Specifically, if the thread execution priority ranking result of the three threads is: thread 1>thread 2>thread 3, it means that thread 1 has the highest thread execution priority, thread 2 has the second highest thread execution priority, and thread 3 has the lowest thread execution priority. When thread 1 encounters a factor that affects the normal execution of the task, thread 1 suspends execution and raises the thread execution priority of thread 2 to the highest. Then, the thread execution priorities of the three threads after reordering are: thread 2>thread 1>thread 3. In this way, it is ensured that idle threads with high execution priority will first process tasks with high task priority.
[0138] It should be noted that factors that affect the normal execution of tasks include: blocking or interruption.
[0139] In an optional embodiment, the processing system further includes:
[0140] A loading module is used to load data information corresponding to multiple tasks into the preprocessing task pool;
[0141] In this embodiment, all resources (eg, data information) corresponding to all tasks are preloaded into the preprocessing task pool in advance.
[0142] Specifically, assume that the system resources required for Task 1 are A, B, and C; the system resources required for Task 2 are D, E, and F; and the system resources required for Task 3 are G, H, and I. Then the system resources AI can all be preloaded into the preprocessing task pool. Take Task 1 of loading the user's personalized homepage as an example. Assuming that the corresponding system resources are: user location, user permissions, and user portrait, the user location, user permissions, and user portrait are loaded into the preprocessing task pool in advance to reduce the time for task execution to load data, thereby improving the efficiency of task processing.
[0143] It should be noted that the role of the preprocessing task pool is to load the data information required by the next batch of task queues in advance to make up for the time difference in loading the next batch of data information by a single thread or multiple threads.
[0144] In an optional embodiment, the second acquisition module is used to acquire multiple threads, and divide the execution priorities of the multiple threads according to the resource proportion of each thread to obtain the thread execution priority;
[0145] Among them, the thread execution priority is positively correlated with the resource share of the thread.
[0146] In this embodiment, the greater the resource proportion, the higher the thread execution priority.
[0147] In an optional embodiment, the processing system further includes:
[0148] A second processing module, configured to restore the thread execution priority of a thread in response to the disappearance of a factor affecting the normal execution of a task of a certain thread;
[0149] In this embodiment, when the factors affecting the normal execution of the task of thread 1 disappear, the thread execution priority of thread 1 is restored, that is, the thread execution priorities of the above three threads are restored to: thread 1>thread 2>thread 3.
[0150] In an optional embodiment, the sorting module includes:
[0151] A sorting unit, used to sort multiple tasks according to task priorities to obtain a priority sorting result;
[0152] The adjustment unit is used to adjust the priority sorting result according to the dependency to obtain the task sorting result.
[0153] In this embodiment, all tasks are first sorted according to task priorities, and then the sorted results are adjusted according to task dependencies, so as to obtain a final task sorting result.
[0154] Specifically, the final task sorting results are sorted according to task priority and task dependency. First, sort by task priority to obtain the preliminary sorting result of the preliminary task. Assume that the preliminary sorting result of the preliminary task is: Task 1>Task 3>Task 2. Secondly, adjust the preliminary sorting results of the preliminary tasks according to the dependencies of the tasks. Assuming that Task 2 depends on Task 1 and Task 3 depends on Task 2, the final task sorting result is: Task 1>Task 2>Task 3. For example: Task 1 that loads the user's personalized homepage and Task 2 that loads recommended user attractions both have a task priority of one; Task 3 that loads ticket purchase discounts has a task priority of two, but Task 2 depends on Task 1. Therefore, the final task priority sorting result is: Task 1>Task 2>Task 3.
[0155] In an optional embodiment, the processing system further includes:
[0156] The third processing module is used to respond to determining that the current thread execution priority of a certain thread is still in a paused state at each preset time interval, and to increase the thread execution priority corresponding to the thread with the next thread priority until the current thread priority of the certain thread is restored from the paused state to the ready state.
[0157] In this embodiment, the pause state of a certain thread is judged at a certain interval. If the judgment result is that the thread is still in the pause state, the thread execution priority corresponding to the thread with the next thread execution priority is raised in sequence until the current thread priority of a certain thread is restored from the pause state to the ready state.
[0158] Specifically, when the task in thread 1 is always suspended, the suspension state of thread 1 is determined at intervals. If the determination result of thread 1 is: still in the suspended state, the thread execution priority of thread 1 will continue to decrease. For example, if the original thread execution priority of thread 1 is greater than the thread execution priority of thread 3, the thread execution priority of thread 1 is lowered so that the thread execution priority of thread 3 is higher than the thread execution priority of thread 1, until thread 1 resumes execution, then the thread execution priority of thread 1 before suspension is restored.
[0159] In a specific implementation process, for example, three operations generated when a user uses the system are obtained, namely operation 1, operation 2, and operation 3.
[0160] The tasks generated by operation 1 are task 1, task 2, and task 3; the tasks generated by operation 2 are task 4, task 5, and task 6; the tasks generated by operation 3 are task 7, task 8, and task 9;
[0161] Add the data information corresponding to the 9 tasks from Task 1 to Task 9 to the preprocessing task pool; sort Task 1 to Task 9 according to task dependency and task priority, and the sorted result is: Task 1> Task 3> Task 5> Task 8> Task 7> Task 9> Task 2> Task 4> Task 6; submit all tasks to the thread pool for processing, and the thread pool will create multiple threads (two independent concepts from the above operation), and different threads have different thread execution priorities. The thread execution priorities of thread 1, thread 2, and thread 3 in the thread pool are determined as: thread 1> thread 2> thread 3. The threads of the above thread pool will execute the tasks sorted according to task dependency and task priority one by one according to the thread execution priority; for example, thread 1 executes task 1, and thread 2 executes task 3. After thread 1 executes task 1, the highest priority of the remaining tasks is task 5, then thread 1 continues to execute task 5, and so on, ensuring that idle threads with high execution priority threads first process tasks with high task priority (it should be noted that the priority of the task being executed is not considered).
[0162] Based on the above steps, when thread 1 executes task 1, if there are factors that affect the normal execution of the task, such as blocking or interruption, etc., the thread execution priority of thread 1 is reduced to thread execution priority 2, and the thread execution priority of thread 2 is increased to thread execution priority 1. Furthermore, when thread 1 resumes execution of task 1, the thread execution priority of thread 1 is restored to the thread execution priority before the downgrade.
[0163] The multithreaded processing method provided by the present disclosure, on the one hand, preloads all the resources corresponding to all tasks into the preprocessing task pool in advance, so that each thread can immediately call the resources in the preprocessing task pool during the task processing process, thereby saving the thread processing time; on the other hand, by sorting all tasks according to task dependencies and task priorities, and dividing the execution priorities of multiple threads, each thread can process tasks with higher priorities in a timely manner, thereby improving the efficiency of thread processing. On the other hand, when factors affecting the normal execution of tasks appear, when variables are affected, the thread with the second thread execution priority immediately takes over, thereby using space in exchange for high efficiency, greatly improving the fluency of the system.
[0164] As for the system embodiment, since it basically corresponds to the method embodiment, the relevant parts can refer to the partial description of the method embodiment. The system embodiment described above is only illustrative, wherein the units described as separate components may or may not be physically separated, and the components as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the disclosed solution.
[0165] Example 3
[0166] Figure 3 This is a structural diagram of an electronic device shown in Embodiment 3 of the present disclosure, wherein the electronic device includes a memory, a processor, and a computer program stored in the memory and used to run on the processor, and when the processor executes the computer program, the multi-threaded processing method described in any of the above embodiments is implemented. Figure 3 The electronic device 90 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0167] like Figure 3 As shown, the electronic device 90 may be in the form of a general-purpose computing device, for example, it may be a server device. The components of the electronic device 90 may include, but are not limited to: at least one processor 91, at least one memory 92, and a bus 93 connecting different system components (including the memory 92 and the processor 91).
[0168] The bus 93 includes a data bus, an address bus, and a control bus.
[0169] The memory 92 may include a volatile memory, such as a random access memory (RAM) 921 and / or a cache memory 922 , and may further include a read only memory (ROM) 923 .
[0170] The memory 92 may also include a program tool 925 (or utility) having a set (at least one) of program modules 924, such program modules 924 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0171] The processor 91 executes various functional applications and data processing by running the computer programs stored in the memory 92, such as the multi-threaded processing method provided in any of the above embodiments.
[0172] The electronic device 90 may also communicate with one or more external devices 94 (e.g., keyboards, pointing devices, etc.). Such communication may be performed via an input / output (I / O) interface 95. Furthermore, the electronic device 90 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 96. Figure 3 As shown, the network adapter 96 communicates with other modules of the electronic device 90 via the bus 93. It should be understood that, although not shown in the figure, other hardware and / or software modules can be used in conjunction with the electronic device 90, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID (disk array) systems, tape drives, and data backup storage systems, etc.
[0173] It should be noted that although several units / modules or sub-units / modules of the electronic device are mentioned in the above detailed description, this division is merely exemplary and not mandatory. In fact, according to the embodiments of the present disclosure, the features and functions of two or more units / modules described above can be embodied in one unit / module. Conversely, the features and functions of one unit / module described above can be further divided into multiple units / modules to be embodied.
[0174] Example 4
[0175] Embodiment 4 of the present disclosure further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the multi-threaded processing method provided by any of the above embodiments is implemented.
[0176] The readable storage medium may include but is not limited to: a portable disk, a hard disk, a random access memory, a read-only memory, an erasable programmable read-only memory, an optical storage device, a magnetic storage device or any suitable combination of the above.
[0177] Example 5
[0178] Embodiment 5 of the present disclosure further provides a computer program product, including a computer program, which implements any of the multi-threaded processing methods described above when executed by a processor.
[0179] Among them, the program code for executing the computer program product of the present disclosure can be written in any combination of one or more programming languages, and the program code can be executed completely on the user device, partially on the user device, as an independent software package, partially on the user device and partially on a remote device, or completely on the remote device.
[0180] Although the specific embodiments of the present disclosure are described above, those skilled in the art should understand that this is only an example, and the protection scope of the present disclosure is defined by the appended claims. Those skilled in the art may make various changes or modifications to these embodiments without departing from the principles and essence of the present disclosure, but these changes and modifications all fall within the protection scope of the present disclosure.
Claims
1. A multi-threaded processing method, characterized in that: The processing method comprises: Get multiple tasks corresponding to user operations; Sorting the multiple tasks according to task dependencies and task priorities to obtain a task sorting result; Acquire multiple threads, and divide the execution priorities of the multiple threads to obtain thread execution priorities; The task to be processed by each thread is determined according to the thread execution priority and the task sorting result.
2. The multi-thread processing method according to claim 1, characterized in that: The processing method also includes: In response to a factor affecting normal execution of a task occurring in a certain thread, the current thread execution priority of the thread is suspended.
3. The multi-thread processing method according to claim 1, characterized in that: The processing method also includes: Loading data information corresponding to the multiple tasks into a preprocessing task pool; and / or, The step of obtaining a plurality of threads, dividing the execution priorities of the plurality of threads, and obtaining the thread execution priorities comprises: Acquire multiple threads, and divide the execution priorities of the multiple threads according to the resource ratio of each thread to obtain the thread execution priority; Among them, the thread execution priority is positively correlated with the resource share of the thread.
4. The multi-thread processing method according to claim 1, characterized in that: The processing method also includes: In response to the disappearance of a factor affecting normal execution of a task of a certain thread, restoring the thread execution priority of the thread; and / or, The step of sorting the multiple tasks according to task dependencies and task priorities to obtain task sorting results comprises: Sorting the multiple tasks according to the task priorities to obtain a priority sorting result; The priority sorting result is adjusted according to the dependency to obtain a task sorting result.
5. The multi-thread processing method according to claim 4, characterized in that: The processing method also includes: In response to determining at each preset time interval that the current thread execution priority of a certain thread is still in a paused state, the thread execution priority corresponding to the thread with the next thread priority is increased until the current thread priority of the certain thread is restored from the paused state to the ready state.
6. The multi-thread processing method according to claim 2, characterized in that: The step of suspending the current thread execution priority of a thread in response to a factor affecting the normal execution of a task occurring in the thread comprises: In response to a factor affecting the normal execution of a task in a certain thread, the current thread execution priority of the thread is reduced, and the thread execution priority corresponding to the thread with the next thread priority is increased.
7. A multi-threaded processing system, characterized in that: The processing system comprises: A first acquisition module, used to acquire multiple tasks corresponding to user operations; A sorting module, used to sort the multiple tasks according to task dependencies and task priorities to obtain a task sorting result; A second acquisition module is used to acquire multiple threads and divide the execution priorities of the multiple threads to obtain thread execution priorities; The determination module is used to determine the task to be processed by each thread according to the thread execution priority and the task sorting result.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and used to run on the processor, characterized in that: When the processor executes the computer program, the multi-thread processing method according to any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the multi-thread processing method according to any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the multi-thread processing method according to any one of claims 1 to 6 is implemented.