A main thread lag processing method, device, equipment and storage medium
By prioritizing and executing tasks in the task queue, the main thread lag issue was resolved, task backlog was reduced, and the user experience was improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- PENINSULA INFORMATION TECH INC
- Filing Date
- 2021-12-09
- Publication Date
- 2026-07-03
AI Technical Summary
Mobile applications are prone to lag when processing time-consuming tasks, which can lead to delays in user interaction and negatively impact the user experience.
Add time-consuming tasks to the task queue, determine the priority tasks in the task queue, add them to the main thread for execution according to the task mode, and delete them from the queue after completion. This ensures that only one task is in the queue at any given time, reducing task backlog on the main thread.
It reduces the accumulation of time-consuming tasks in the main thread, reduces application lag, and optimizes the user experience.
Smart Images

Figure CN114168291B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for handling main thread stuttering. Background Technology
[0002] In mobile applications, the main thread is primarily used to handle tasks such as rendering user interfaces and responding to user action events. The main thread typically executes tasks in a serial manner, performing one task at a time. After a task is completed, subsequent tasks added to the main thread must wait for the previous task to complete.
[0003] The main thread often needs to handle a large number of tasks. However, when the main thread experiences time-consuming anomalies or accumulates a large number of time-consuming tasks, it can lead to delayed user responses and a stuttering effect. As mobile applications iterate and expand, the number of time-consuming tasks will increase and gradually accumulate in the main thread, causing frequent stuttering and severely impacting the user experience. Summary of the Invention
[0004] This application provides a method, apparatus, device, and storage medium for handling main thread lag, in order to solve the problem in the prior art where time-consuming tasks accumulate in the main thread, causing frequent lag and seriously affecting user experience, thereby reducing the accumulation of time-consuming tasks in the main thread, reducing application lag, and optimizing user experience.
[0005] In a first aspect, embodiments of this application provide a method for handling main thread stuttering, including:
[0006] When all time-consuming tasks in the time-consuming task queue are in a waiting state, based on the task mode of each time-consuming task in the time-consuming task queue, the time-consuming task to be executed first is determined from the time-consuming task queue. The time-consuming task is obtained by extracting the time-consuming tasks on the main thread.
[0007] The time-consuming tasks that are prioritized for execution are added to the main thread, and the main thread executes the time-consuming tasks that are prioritized for execution.
[0008] Once the main thread has finished executing the priority time-consuming task, the priority time-consuming task is removed from the time-consuming task queue.
[0009] In a second aspect, embodiments of this application provide a main thread stuttering handling device, including a scheduling response module, a task scheduling module, and a queue update module, wherein:
[0010] The scheduling response module is used to determine the task mode of each time-consuming task in the time-consuming task queue when all time-consuming tasks in the time-consuming task queue are in a waiting state. The time-consuming tasks are obtained by extracting time-consuming tasks on the main thread.
[0011] The task determination module is used to determine the time-consuming task to be executed first from the time-consuming task queue based on the task mode of each time-consuming task in the time-consuming task queue.
[0012] The task scheduling module is used to add the priority time-consuming tasks to the main thread, and the main thread executes the priority time-consuming tasks.
[0013] The queue update module is used to delete the priority time-consuming task from the time-consuming task queue after the main thread has completed executing the priority time-consuming task.
[0014] In a third aspect, embodiments of this application provide a main thread stuttering processing device, including: a memory and one or more processors;
[0015] The memory is used to store one or more programs;
[0016] When the one or more programs are executed by the one or more processors, the one or more processors implement the main thread stuttering handling method as described in the first aspect.
[0017] In a fourth aspect, embodiments of this application provide a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform the main thread stuttering handling method as described in the first aspect.
[0018] This embodiment adds time-consuming tasks from the main thread to a time-consuming task queue. When all time-consuming tasks in the queue are in a waiting state, a priority time-consuming task is determined from the queue based on the task mode of each task. This priority time-consuming task is then added to the main thread for execution. At this time, there are time-consuming tasks in the queue that are not in a waiting state. After the main thread completes the execution of the priority time-consuming task, it is removed from the queue. The time-consuming task queue then returns to a state where all time-consuming tasks are in a waiting state, allowing for the scheduling and execution of the next time-consuming task. This ensures that at most one priority time-consuming task is placed in the main thread, reducing the accumulation of time-consuming tasks in the main thread, reducing concentrated execution of time-consuming tasks by the main thread, and reducing application lag. Attached Figure Description
[0019] Figure 1This is a flowchart of a main thread stuttering handling method provided in an embodiment of this application;
[0020] Figure 2 This is a flowchart of another main thread lag handling method provided in the embodiments of this application;
[0021] Figure 3 This is a schematic diagram of a process for inserting a time-consuming task into a time-consuming task queue, provided in an embodiment of this application;
[0022] Figure 4 This is a schematic diagram of a process for determining the time-consuming tasks to be executed first from a time-consuming task queue, provided in an embodiment of this application.
[0023] Figure 5 This is a flowchart illustrating how to add time-consuming tasks that require priority execution to the main thread, as provided in an embodiment of this application.
[0024] Figure 6 This is a schematic diagram of the structure of a main thread stuttering processing device provided in an embodiment of this application;
[0025] Figure 7 This is a schematic diagram of the structure of a main thread stuttering processing device provided in an embodiment of this application. Detailed Implementation
[0026] To make the objectives, technical solutions, and advantages of this application clearer, specific embodiments of this application will be described in further detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are merely for explaining this application and not for limiting it. It should also be noted that, for ease of description, only the parts relevant to this application are shown in the drawings, not all of them. Before discussing exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe operations (or steps) as sequential processes, many of these operations can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the operations can be rearranged. The process can be terminated when its operation is completed, but additional steps not included in the drawings may also be present. The above processes can correspond to methods, functions, procedures, subroutines, subroutines, etc.
[0027] Figure 1 A flowchart of a main thread stuttering handling method provided in an embodiment of this application is given. The main thread stuttering handling method provided in this embodiment of the application can be executed by a main thread stuttering handling device, which can be implemented by hardware and / or software and integrated into the main thread stuttering handling equipment.
[0028] The following description uses the main thread stuttering handling mechanism to illustrate the main thread stuttering handling method. (Reference) Figure 1 The main thread lag handling methods include:
[0029] S101: When all time-consuming tasks in the time-consuming task queue are in a waiting state, based on the task mode of each time-consuming task in the time-consuming task queue, determine the time-consuming task to be executed first from the time-consuming task queue. The time-consuming task is obtained by extracting the time-consuming tasks on the main thread.
[0030] This embodiment provides a cache queue as a time-consuming task queue to cache time-consuming tasks (time-consuming functions) detected and extracted on the main thread. It should be explained that the task status of a time-consuming task includes an executing state and a waiting state. The executing state can be understood as the state where the time-consuming task (e.g., a time-consuming task prioritized for execution on the main thread) has been added to the main thread from the time-consuming task queue. At this time, the time-consuming task may have already started execution by the main thread, or it may still be waiting for execution. Correspondingly, the waiting state can be understood as the state where the time-consuming task is still in the time-consuming task queue waiting to be added to the main thread. It is understood that since this solution only determines the time-consuming task to be executed first when all time-consuming tasks in the time-consuming task queue are in the waiting state, there can be at most one time-consuming task in the executing state in the time-consuming task queue.
[0031] For example, if there are time-consuming tasks in the time-consuming task queue, and all time-consuming tasks in the time-consuming task queue are in a waiting state (i.e., there are no time-consuming tasks that have been added to the main thread for priority execution in the time-consuming task queue), the time-consuming task to be executed first is determined from the time-consuming task queue according to the task mode of each time-consuming task in the time-consuming task queue.
[0032] The task mode indicates the execution timing requirements of a time-consuming task. This solution prioritizes the time-consuming task with the highest execution timing requirement (i.e., requiring faster execution) from the time-consuming task queue. Furthermore, when multiple time-consuming tasks with the highest execution timing requirements exist for different task modes, the task added to the time-consuming task queue first is selected as the priority execution task. The task mode can be determined based on the task type (different task types have different execution time requirements; for example, the execution time requirement for loading third-party SDKs is lower than that for user touch response tasks on the user interface), or it can be determined based on the time requirement information or task type information carried by the time-consuming task itself according to its execution timing requirements.
[0033] S102: Add the time-consuming tasks that are prioritized to the main thread, and let the main thread execute the time-consuming tasks that are prioritized.
[0034] For example, after determining the time-consuming tasks to be executed first, these tasks are thrown into the main thread for processing. It is understood that when a time-consuming task is thrown into the main thread, its task status is updated to "executing".
[0035] In one embodiment, when a time-consuming task is extracted in the main thread, the relevant code of the time-consuming task is packaged in the form of a code closure. Correspondingly, when adding a time-consuming task that is to be executed first to the main thread, the code closure corresponding to the time-consuming task is thrown to the main thread. The main thread executes the tasks in the order it receives them. When executing the time-consuming task, the corresponding code closure is parsed and the corresponding task processing operation is executed.
[0036] S103: Once the time-consuming task that was prioritized for execution by the main thread has been completed, remove the time-consuming task from the time-consuming task queue.
[0037] For example, after a priority time-consuming task is thrown into the main thread, the main thread waits to execute the priority time-consuming task. When the main thread finishes executing the priority time-consuming task, the priority time-consuming task is removed from the time-consuming task queue. At this time, all remaining time-consuming tasks in the time-consuming task queue are in a waiting state. The priority time-consuming task can be re-determined from the time-consuming task queue based on the task mode of each time-consuming task in the queue, and the newly determined priority time-consuming task is thrown into the main thread for processing, until all time-consuming tasks in the time-consuming task queue have been executed by the main thread.
[0038] When a time-consuming task is found to have been completed by the main thread, it is removed from the queue of completed time-consuming tasks. If no time-consuming task is found to have been completed by the main thread, the process continues to check whether all time-consuming tasks in the queue are in a waiting state. If so, a new time-consuming task is determined to be executed first. If a time-consuming task is in an executing state, the process continues to check whether the time-consuming task has been completed. This process is repeated until all time-consuming tasks in the queue have been executed. When a new time-consuming task is added to the queue, the process of determining the time-consuming task to be executed first is initiated.
[0039] In one embodiment, the execution waiting time of time-consuming tasks in the main thread, the scheduling waiting time in the time-consuming task queue, the subsequent execution status of time-consuming tasks in the main thread, and the lag situation can be collected and uploaded to the service backend to facilitate developers in analyzing the causes of lag and improving the efficiency of lag resolution, thereby improving development efficiency.
[0040] As described above, by adding time-consuming tasks from the main thread to a time-consuming task queue, and when all time-consuming tasks in the queue are in a waiting state, the priority time-consuming task is determined from the queue based on the task mode of each task, and then added to the main thread for execution. At this time, there are time-consuming tasks in the queue that are not in a waiting state. After the main thread completes the execution of the priority time-consuming task, it is removed from the queue. At this point, the time-consuming task queue returns to a state where all time-consuming tasks are in a waiting state, and the scheduling and execution of the next time-consuming task can proceed. This process ensures that at most one priority time-consuming task is placed in the main thread from the queue, reducing the accumulation of time-consuming tasks in the main thread, reducing the concentrated execution of time-consuming tasks by the main thread, and reducing application lag.
[0041] Based on the above embodiments, Figure 2 A flowchart of another main thread stuttering handling method provided in an embodiment of this application is given, which is a concretization of the above-described main thread stuttering handling method. (Reference) Figure 2 The main thread lag handling methods include:
[0042] S201: Detect time-consuming tasks in the main thread, extract time-consuming tasks that meet the set task scheduling conditions from the main thread, and insert the extracted time-consuming tasks into the time-consuming task queue.
[0043] The task scheduling conditions indicate whether a time-consuming task is suitable to be moved to the time-consuming task queue for processing. Whether a time-consuming task meets the task scheduling conditions can be determined based on the task type or the scheduling configuration information carried by the time-consuming task itself. Specifically, tasks added to the main thread are monitored in real time to detect time-consuming tasks in the main thread. When a time-consuming task is detected in the main thread (and is not a priority time-consuming task added to the main thread in this solution), it is determined whether the time-consuming task meets the set task scheduling conditions. If the time-consuming task does not meet the task scheduling conditions, it does not need to be extracted into the time-consuming task queue. The time-consuming task queue can continue to wait for processing, be transferred to another thread, or be used for exception repair in the main thread. If the time-consuming task meets the task scheduling conditions, it is extracted from the main thread to reduce the accumulation of too many time-consuming tasks in the main thread and cause lag. The extracted time-consuming task is then inserted into the time-consuming task queue.
[0044] The determination of time-consuming tasks can be done in real-time during task execution, or by pre-determining the task type. During application runtime, tasks added to the main thread by other functional modules are monitored, and their type is used to determine if they are time-consuming tasks. Specifically, the determination of time-consuming tasks can be achieved through detection of stuttering stacks collected by a stuttering detection system configured in the main thread stuttering handling mechanism, detection of task consumption time based on profiling tools (performance tuning tools), or detection during application monitoring during development. For example, a detection flag can be set in the main thread, and the flag can be checked at set time intervals to see if it is reset. If it is not reset, the main thread is considered to have stuttered, and the task type corresponding to the time-consuming task is determined. Alternatively, the function stack at the time of stuttering can be collected using performance monitoring tools (APM tools) provided by the application or system, and the time-consuming task and its corresponding task type can be determined based on the function stack. By collecting and updating the task types corresponding to time-consuming tasks in real time, it is possible to subsequently determine whether a time-consuming task exists in the main thread based on the task type.
[0045] In one embodiment, task scheduling conditions can be determined based on whether the task meets thread transfer requirements and its sensitivity to execution timing. That is, if a time-consuming task does not meet the set thread transfer requirements and is not sensitive to the timing of task execution, it is considered to meet the task scheduling conditions. Whether a time-consuming task meets the thread transfer requirements and is not sensitive to the timing of task execution can be determined based on the task type of the time-consuming task (the main thread lag handling device pre-records task types that meet the thread transfer requirements and are not sensitive to the timing of task execution). For example, for file read / write tasks, even if they take a long time, such tasks are not suitable for being transferred to the time-consuming task queue for processing. The task can be transferred to other sub-threads or kept in the main thread.
[0046] For example, during application runtime, when a time-consuming task is detected, the cause of the time-consuming task or lag can be analyzed based on the configured lag handling system, and attempts can be made to repair the time-consuming function (for example, if the cause of the time-consuming task or lag is found to be a code exception, the code of the time-consuming task can be repaired based on the set code repair strategy). If the repair is successful, there is no need to determine whether to add the time-consuming task to the time-consuming task queue. If the repair fails, it is determined whether the time-consuming task meets the thread transfer requirements, that is, whether the time-consuming task can be transferred to another thread. If it can, it is transferred to another thread; otherwise (i.e., the thread transfer requirements are not met), it is further determined whether the time-consuming task is not sensitive to the timing of task execution. If it is, the time-consuming task is inserted into the time-consuming task queue; otherwise, the time-consuming task is ignored. In one embodiment, the subsequent execution status and lag status of the time-consuming task can be collected to facilitate developers in analyzing the cause of lag and improving the efficiency of lag resolution, thereby improving development efficiency.
[0047] In one embodiment, the insertion position of a newly added time-consuming task in the time-consuming task queue can be determined based on the priority of the time-consuming task. Specifically, inserting an extracted time-consuming task into the time-consuming task queue includes: inserting the extracted time-consuming task into the time-consuming task queue based on its priority. The priority of the time-consuming task can be determined according to its task type, with different task types corresponding to different priorities.
[0048] In one embodiment, the priority of a time-consuming task can be determined based on its task mode, which includes a first task mode and a second task mode, with the first task mode having a higher priority than the second task mode. For example, the first task mode might be a normal mode, and the second task mode might be an idle mode. Time-consuming tasks in the idle mode have lower requirements for execution timing compared to those in the normal mode; they are less sensitive to execution timing and can be delayed if other tasks are available. They can be processed after all time-consuming tasks in the normal mode have been completed. The task mode of a time-consuming task can also be determined based on its task type; different task types correspond to different task modes. Therefore, as... Figure 3 The provided flowchart illustrates a process for inserting time-consuming tasks into a time-consuming task queue. When inserting extracted time-consuming tasks into the queue based on their priority, this solution includes at least one of the following:
[0049] S2011: When the task mode of a time-consuming task is the first task mode, the extracted time-consuming task is inserted into the last position of the time-consuming task corresponding to the first task mode in the time-consuming task queue.
[0050] S2012: When the task mode of a time-consuming task is the second task mode, the extracted time-consuming task is inserted into the last position of the time-consuming task corresponding to the second task mode in the time-consuming task queue, and the time-consuming task corresponding to the first task mode in the task queue is placed before the time-consuming task corresponding to the second task mode.
[0051] Specifically, when a new time-consuming task is retrieved from the main thread, it is determined whether the task mode is the first task mode or the second task mode. If the task mode is the first task mode (e.g., normal mode), the task is inserted at the end of the time-consuming task queue corresponding to the first task mode. In this case, the newly inserted task is the last among all time-consuming tasks corresponding to the first task mode in the time-consuming task queue. If the task mode is the second task mode (e.g., idle mode), the task is inserted at the end of the time-consuming task queue corresponding to the second task mode. In this case, the newly inserted task is the last among all time-consuming tasks corresponding to the second task mode in the time-consuming task queue.
[0052] Understandably, the first task mode has a higher priority than the second task mode. When time-consuming tasks corresponding to both the first and second task modes exist in the time-consuming task queue, the time-consuming task corresponding to the first task mode is placed before all time-consuming tasks corresponding to the second task mode. In this case, the time-consuming task added first in the first task mode is placed at the front of the time-consuming task queue, and the time-consuming task added first in the second task mode is placed after the time-consuming task added last in the first task mode.
[0053] It is understandable that the steps of detecting, extracting, and inserting time-consuming tasks in the main thread into the time-consuming task queue are independent of the process of determining the priority time-consuming tasks in the time-consuming task queue. That is, the process of real-time detection of time-consuming tasks in the main thread and the process of throwing the priority time-consuming tasks in the time-consuming task queue into the main thread can be processed in parallel.
[0054] In one embodiment, a task manager class (MainThreadTaskManager) and a main thread task class (MainThreadTask) can be set in the main thread lag handling device. The task manager class and the main thread task class communicate with each other. The task manager class is responsible for collecting and scheduling time-consuming tasks, as well as detecting the main thread's busy level and collecting task waiting time. It also serves as the module entry point for the main thread lag handling device. The main thread task class is responsible for storing the code closures of time-consuming tasks, as well as information such as the task type, priority, and task mode of the time-consuming tasks. When a time-consuming task that meets the set task scheduling conditions is retrieved, the code closure corresponding to the time-consuming task is added to the task manager class, and the newly added time-consuming task is represented by an instance of the main thread task class. The task manager class manages the scheduling of time-consuming tasks, while the main thread task class executes them. The setup of the task manager class and the main thread task class provides developers with good code development convenience and maintainability, improving development efficiency.
[0055] S202: When all time-consuming tasks in the time-consuming task queue are in a waiting state, determine the time-consuming task to be executed first from the time-consuming task queue based on the task mode of each time-consuming task in the time-consuming task queue.
[0056] In one embodiment, the task mode of a time-consuming task includes a first task mode and a second task mode. The time-consuming task can be prioritized for execution based on its corresponding task mode. Based on this, such as... Figure 4 The provided flowchart illustrates a method for determining priority time-consuming tasks from a time-consuming task queue. When determining priority time-consuming tasks based on their task patterns within the queue, this method includes at least one of the following:
[0057] S2021: When there is a time-consuming task with the first task mode in the time-consuming task queue, the time-consuming task with the first task mode that was added first in the time-consuming task queue shall be determined as the time-consuming task to be executed first.
[0058] S2022: When there are only time-consuming tasks of the second task mode in the time-consuming task queue, the time-consuming task of the second task mode that was added first in the time-consuming task queue is determined as the time-consuming task to be executed first, and the priority of the first task mode is higher than the priority of the second task mode.
[0059] Specifically, the system searches for suitable time-consuming tasks in the time-consuming task queue and adds them to the main thread for execution. When the number of time-consuming tasks in the queue is zero, the search process ends until a new time-consuming task is added. If the number of time-consuming tasks in the queue is greater than zero, the system checks if any time-consuming tasks are currently executing. If so, the current search process ends, and a new suitable time-consuming task is searched.
[0060] Furthermore, if there are no time-consuming tasks in the time-consuming task queue that are currently executing (i.e., all time-consuming tasks in the queue are in a waiting state), then it is further determined whether a time-consuming task of the first task mode exists in the queue. If a time-consuming task of the first task mode exists, the time-consuming task of the first task mode that was added first in the queue is determined as the priority time-consuming task to be executed. If there are no time-consuming tasks of the first task mode, then it is further determined whether a time-consuming task of the second task mode exists in the queue. If there are time-consuming tasks of the second task mode, the time-consuming task of the second task mode that was added first in the queue is determined as the priority time-consuming task to be executed. If there are no time-consuming tasks of the second task mode, then the current time-consuming task search process ends, and a suitable time-consuming task is searched again.
[0061] S203: Add the time-consuming tasks that are prioritized to the main thread, and let the main thread execute the time-consuming tasks that are prioritized.
[0062] This solution determines when to add time-consuming tasks to the main thread based on the task mode of the time-consuming task. Based on this, such as... Figure 5 The provided flowchart illustrates a method for adding time-consuming tasks with priority execution to the main thread. When adding time-consuming tasks with priority execution to the main thread, this solution includes at least one of the following:
[0063] S2031: When the priority time-consuming task is in the first task mode, the priority time-consuming task is asynchronously added to the main thread.
[0064] S2032: When the priority time-consuming task is in the second task mode and the main thread's busy level is lower than the set busy threshold, the priority time-consuming task will be asynchronously added to the main thread.
[0065] Specifically, when the priority time-consuming task is in task mode 1, it can be directly added asynchronously to the main thread for processing. When the priority time-consuming task is in task mode 2, it first checks if the main thread's workload is below a set workload threshold. If the main thread's workload is above the threshold, the opportunity to add the priority time-consuming task to the time-consuming task queue is abandoned, and a new priority time-consuming task is searched for. When the main thread's workload is below the threshold, the priority time-consuming task is added asynchronously to the main thread for processing.
[0066] The busy level of the main thread can be determined by sending test tasks to the main thread through the Task Manager and judging the busy level based on the time it takes for the main thread to return the execution result of the test task. For example, if a return time threshold is set, the main thread is considered to be less busy than the set busy threshold if it returns the corresponding execution result within the return time threshold; otherwise, it is considered to be more busy than the set busy threshold.
[0067] Understandably, this solution asynchronously throws time-consuming tasks into the main thread. The execution of the task manager class and the operation of the main thread do not interfere with each other. Compared to throwing two time-consuming tasks into the main thread simultaneously, the task manager class must wait for the time-consuming task to finish executing in the main thread before it can process other tasks. After throwing the time-consuming task into the main thread, the task manager class can end the current task scheduling process, reducing the impact on the task manager class's distribution of time-consuming tasks.
[0068] S204: Once the time-consuming task that was prioritized for execution by the main thread has been completed, the time-consuming task that was prioritized for execution is removed from the time-consuming task queue.
[0069] As described above, by adding time-consuming tasks from the main thread to a time-consuming task queue, and when all time-consuming tasks in the queue are in a waiting state, the priority time-consuming task is determined from the queue based on the task mode of each task, and then added to the main thread for execution. At this time, there are time-consuming tasks in the queue that are not in a waiting state. After the main thread completes the execution of the priority time-consuming task, it is removed from the queue. At this point, the time-consuming task queue returns to a state where all time-consuming tasks are in a waiting state, and the scheduling and execution of the next time-consuming task can proceed. This process ensures that at most one priority time-consuming task is placed in the main thread from the queue, reducing the accumulation of time-consuming tasks in the main thread, reducing the concentrated execution of time-consuming tasks by the main thread, and reducing application lag. Simultaneously, time-consuming tasks that meet the task scheduling conditions are added to the time-consuming task queue. Tasks that meet thread transfer requirements and are sensitive to task execution timing will retain their original processing methods, reducing the possibility of tasks requiring guaranteed execution timing being incorrectly extracted into the time-consuming task queue, leading to untimely task processing. Furthermore, the insertion position of time-consuming tasks in the time-consuming task queue is determined based on their priority, facilitating quick identification of priority time-consuming tasks based on their position in the queue. This allows for more flexible scheduling of time-consuming tasks in the queue, reducing waiting time for higher-priority tasks and optimizing user experience. By scheduling time-consuming tasks according to their priority, task mode, and main thread busyness, concentrated execution of time-consuming tasks is effectively avoided, reducing main thread lag.
[0070] Figure 6 A schematic diagram of a main thread stuttering handling device provided in an embodiment of this application is given. (Reference) Figure 6 The main thread lag handling device includes a scheduling response module 31, a task scheduling module 32, and a queue update module 33.
[0071] The scheduling response module 31 is used to determine the priority time-consuming task from the time-consuming task queue based on the task mode of each time-consuming task in the queue when all time-consuming tasks in the queue are in a waiting state. The time-consuming task is obtained by extracting the time-consuming tasks on the main thread. The task scheduling module 32 is used to add the priority time-consuming task to the main thread and execute the priority time-consuming task. The queue update module 33 is used to delete the priority time-consuming task from the time-consuming task queue after the main thread has finished executing the priority time-consuming task.
[0072] As described above, by adding time-consuming tasks from the main thread to a time-consuming task queue, and when all time-consuming tasks in the queue are in a waiting state, the priority time-consuming task is determined from the queue based on the task mode of each task, and then added to the main thread for execution. At this time, there are time-consuming tasks in the queue that are not in a waiting state. After the main thread completes the execution of the priority time-consuming task, it is removed from the queue. At this point, the time-consuming task queue returns to a state where all time-consuming tasks are in a waiting state, and the scheduling and execution of the next time-consuming task can proceed. This process ensures that at most one priority time-consuming task is placed in the main thread from the queue, reducing the accumulation of time-consuming tasks in the main thread, reducing the concentrated execution of time-consuming tasks by the main thread, and reducing application lag.
[0073] This application also provides a main thread stuttering processing device, which can integrate the main thread stuttering processing apparatus provided in this application. Figure 7 This is a schematic diagram of the structure of a main thread stuttering processing device provided in an embodiment of this application. (Reference) Figure 7 The main thread stuttering processing device includes: an input device 43, an output device 44, a memory 42, and one or more processors 41; the memory 42 is used to store one or more programs; when one or more programs are executed by one or more processors 41, the one or more processors 41 implement the main thread stuttering processing method provided in the above embodiments. The main thread stuttering processing device, equipment, and computer provided above can be used to execute the main thread stuttering processing method provided in any of the above embodiments, and have corresponding functions and beneficial effects.
[0074] This application also provides a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform the main thread stuttering handling method provided in the above embodiments. Of course, the computer-executable instructions in the storage medium containing computer-executable instructions provided in this application are not limited to the main thread stuttering handling method provided above; they can also perform related operations in the main thread stuttering handling method provided in any embodiment of this application. The main thread stuttering handling apparatus, device, and storage medium provided in the above embodiments can execute the main thread stuttering handling method provided in any embodiment of this application. Technical details not described in detail in the above embodiments can be found in the main thread stuttering handling method provided in any embodiment of this application.
[0075] The above description is merely a preferred embodiment and the technical principles employed in this application. This application is not limited to the specific embodiments provided herein, and various obvious changes, readjustments, and substitutions that can be made by those skilled in the art will not depart from the scope of protection of this application. Therefore, although this application has been described in detail through the above embodiments, this application is not limited to the above embodiments, and may include many other equivalent embodiments without departing from the concept of this application, the scope of which is determined by the scope of the claims.
Claims
1. A method for handling main thread stuttering, characterized in that, include: Detect time-consuming tasks in the main thread, extract time-consuming tasks that meet the set task scheduling conditions from the main thread, and package the relevant code of the time-consuming tasks through code closures; Insert the extracted time-consuming tasks into the time-consuming task queue; When all time-consuming tasks in the time-consuming task queue are in a waiting state, if there is a time-consuming task of the first task mode in the time-consuming task queue, the time-consuming task of the first task mode that was added first in the time-consuming task queue is determined as the time-consuming task with priority execution; if there is only a time-consuming task of the second task mode in the time-consuming task queue, the time-consuming task of the second task mode that was added first in the time-consuming task queue is determined as the time-consuming task with priority execution, and the priority of the first task mode is higher than the priority of the second task mode; the time-consuming tasks are obtained by extracting time-consuming tasks on the main thread, wherein there is at most one time-consuming task in the time-consuming task queue that is in an execution state; When the priority time-consuming task is in the first task mode, the code closure corresponding to the priority time-consuming task is asynchronously thrown to the main thread; when the priority time-consuming task is in the second task mode and the main thread's busy level is lower than a set busy threshold, the code closure corresponding to the priority time-consuming task is asynchronously thrown to the main thread; the main thread parses the code closure corresponding to the priority time-consuming task to execute the priority time-consuming task. Once the main thread has finished executing the priority time-consuming task, the priority time-consuming task is removed from the time-consuming task queue.
2. The main thread stuttering handling method according to claim 1, characterized in that, The time-consuming task satisfies the task scheduling conditions when it does not meet the set thread transfer requirements and is not sensitive to the timing of task execution.
3. The main thread stuttering handling method according to claim 1, characterized in that, The step of inserting the extracted time-consuming task into the time-consuming task queue includes: The extracted time-consuming tasks are inserted into the time-consuming task queue based on their priority.
4. The main thread stuttering handling method according to claim 3, characterized in that, The step of inserting the extracted time-consuming tasks into the time-consuming task queue based on their priority includes at least one of the following: When the task mode of the time-consuming task is the first task mode, the extracted time-consuming task is inserted into the last position of the time-consuming task corresponding to the first task mode in the time-consuming task queue; When the task mode of the time-consuming task is the second task mode, the extracted time-consuming task is inserted into the last position of the time-consuming tasks corresponding to the second task mode in the time-consuming task queue, and the time-consuming tasks corresponding to the first task mode in the task queue are located before the time-consuming tasks corresponding to the second task mode.
5. A device for handling main thread stuttering, characterized in that, It includes a scheduling response module, a task scheduling module, and a queue update module, among which: The scheduling response module is used to detect time-consuming tasks in the main thread, extract time-consuming tasks that meet the set task scheduling conditions from the main thread, and package the relevant code of the time-consuming tasks through code closure; insert the extracted time-consuming tasks into the time-consuming task queue; when all time-consuming tasks in the time-consuming task queue are in a waiting state, if there are time-consuming tasks of the first task mode in the time-consuming task queue, the time-consuming task of the first task mode added first in the time-consuming task queue is determined as the time-consuming task with priority execution; if there are only time-consuming tasks of the second task mode in the time-consuming task queue, the time-consuming task of the second task mode added first in the time-consuming task queue is determined as the time-consuming task with priority execution, and the priority of the first task mode is higher than the priority of the second task mode; the time-consuming tasks are obtained by extracting time-consuming tasks on the main thread, wherein there is at most one time-consuming task in the time-consuming task queue that is in an executing state; The task scheduling module is configured to asynchronously throw the code closure corresponding to the priority time-consuming task to the main thread when the task mode of the priority time-consuming task is the first task mode; and asynchronously throw the code closure corresponding to the priority time-consuming task to the main thread when the task mode of the priority time-consuming task is the second task mode and the busy level of the main thread is lower than a set busy threshold; the main thread then parses the code closure corresponding to the priority time-consuming task to execute the priority time-consuming task. The queue update module is used to delete the priority time-consuming task from the time-consuming task queue after the main thread has completed executing the priority time-consuming task.
6. A device for handling main thread stuttering, characterized in that, include: Memory and one or more processors; The memory is used to store one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the main thread stuttering handling method as described in any one of claims 1-4.
7. A storage medium containing computer-executable instructions, characterized in that, The computer-executable instructions, when executed by a computer processor, are used to perform the main thread stuttering handling method as described in any one of claims 1-4.