Task Scheduling Method, Device, Storage Medium and Electronic Device
By marking the threads that draw tasks on the interface in the Android system as preset types and prioritizing scheduling, the system lag caused by thread waiting is solved, and the user experience is improved.
Patent Information
- Application Number
- CN202010266780.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-07
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2040-04-07
AI Technical Summary
In Android systems, because there are many threads waiting for execution in the background, threads with higher priority also face a long wait, resulting in system lag and affecting the user experience.
By marking threads that directly or indirectly perform interface drawing tasks in the task queue as preset types, and preferring these threads with the shortest running time for scheduling, dynamically increasing the priority of threads related to user experience and reducing waiting time.
It effectively reduces system lag, improves the user experience in interactive scenarios, and ensures that threads related to user experience can be scheduled in a timely manner.
Smart Images

Figure CN113495780B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of electronic devices, and particularly to a task scheduling method, apparatus, storage medium, and electronic device. Background Art
[0002] It is mainly applied to the Android system of electronic devices such as smart phones and tablet computers, and its underlying layer is implemented by the Linux kernel. Therefore, each application thread running on the mobile device also follows the scheduling rules of the Linux kernel. For example, the CFS (Complete Fair Schedule) rule is used to schedule threads.
[0003] The principle adopted by this scheduling mechanism is that when running in the kernel, it fairly obtains CPU (Central Processing Unit) resources that match the static priority of the thread. The higher the static priority, the more CPU resources can be occupied.
[0004] However, regardless of the type of thread, scheduling is based on this principle. When there are many threads waiting to be executed in the background, even a thread with a higher priority has to face a long waiting time, resulting in system lag. Summary of the Invention
[0005] Embodiments of this application provide a task scheduling method, apparatus, storage medium, and electronic device, which can reduce the occurrence of system lag.
[0006] In a first aspect, embodiments of this application provide a task scheduling method, including:
[0007] Obtain a task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes an interface drawing task;
[0008] When there is a thread of the preset type in the task queue, use the thread of the preset type in the task queue as the target thread;
[0009] Determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time;
[0010] When there is no thread with a preset label in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue.
[0011] In a second aspect, embodiments of this application further provide a task scheduling apparatus, including:
[0012] A thread monitoring module, configured to obtain a task queue to be executed and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes an interface drawing task;
[0013] A target determination module, configured to use the thread of the preset type in the task queue as a target thread when there is a thread of the preset type in the task queue;
[0014] A task execution module, configured to determine the target thread with the shortest running time from the task queue and execute the task corresponding to the target thread with the shortest running time;
[0015] The task execution module is further configured to execute the task corresponding to the thread with the shortest running time in the task queue when there is no thread with a preset label in the task queue.
[0016] In a third aspect, an embodiment of the present application further provides a storage medium, on which a computer program is stored. When the computer program runs on a computer, the computer is caused to execute the task scheduling method provided in any embodiment of the present application.
[0017] In a fourth aspect, an embodiment of the present application further provides an electronic device, including a processor and a memory. The memory has a computer program, and the processor is configured to execute the task scheduling method provided in any embodiment of the present application by calling the computer program.
[0018] In the technical solution provided by the embodiment of the present application, when selecting a thread from the task queue for execution, first determine whether there is a thread of a preset type in the task queue. If there is a thread of the preset type in the task queue, use the thread of the preset type in the task queue as the target thread, determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time. If there is no thread of the preset type in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue. In this way, the thread that directly or indirectly executes the interface drawing task is marked as the thread of the preset type, and these threads that directly or indirectly execute the interface drawing task will affect the user experience. When the kernel executes the task, it will detect whether there are these threads that affect the user experience in the task queue. If so, it will preferentially select these threads of the preset type and quickly occupy the CPU resources, which can effectively reduce the waiting time of the threads of the preset type and avoid the situation that too many background task threads cause the threads related to the user experience to not be scheduled in time, thereby improving the user experience in the interaction scenario and reducing the generation of system lags. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] To more clearly illustrate the technical solutions in the embodiments of the present application, the following will briefly introduce the accompanying drawings required for the description of the embodiments. Obviously, the accompanying drawings in the following description are only some embodiments of the present application. For those skilled in the art, without creative efforts, other accompanying drawings can be obtained based on these drawings.
[0020] Figure 1 It is the first flowchart of the task scheduling method provided by the embodiment of the present application.
[0021] Figure 2 It is the second flowchart of the task scheduling method provided by the embodiment of the present application.
[0022] Figure 3 It is the schematic diagram of the red - black tree structure in the task scheduling method provided by the embodiment of the present application.
[0023] Figure 4 It is the schematic diagram of the structure of the task scheduling device provided by the embodiment of the present application.
[0024] Figure 5 It is the first schematic diagram of the structure of the electronic device provided by the embodiment of the present application.
[0025] Figure 6 It is the second schematic diagram of the structure of the electronic device provided by the embodiment of the present application. Detailed implementation manners
[0026] The following will clearly and completely describe the technical solutions in the embodiments of the present application in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, rather than all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative efforts belong to the protection scope of the present application.
[0027] Referring to "embodiments" herein means that the specific features, structures, or characteristics described in conjunction with the embodiments can be included in at least one embodiment of the present application. The phrase appears in various positions in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment mutually exclusive with other embodiments. Those skilled in the art understand explicitly and implicitly that the embodiments described herein can be combined with other embodiments.
[0028] The embodiment of the present application provides a task scheduling method. The execution subject of this task scheduling method can be the task scheduling device provided by the embodiment of the present application, or an electronic device integrated with this task scheduling device, where the task scheduling device can be implemented in a hardware or software manner. Among them, the electronic device can be a smart phone, a tablet computer, a handheld computer, a notebook computer, a desktop computer, or other such devices.
[0029] Please refer to Figure 1 , Figure 1 , which is the first process schematic diagram of the task scheduling method provided by the embodiment of the present application. The specific process of the task scheduling method provided by the embodiment of the present application can be as follows:
[0030] In 101, obtain the task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes the interface drawing task.
[0031] In the embodiment of the present application, the operating system of the electronic device can be a system based on the linux kernel. For example, the Android operating system, etc. In the electronic device, when a program runs, the system will create a process for it and allocate resources to this process.
[0032] A thread is an execution path of a process, the smallest unit when a program is executed, and also the basic unit for CPU scheduling and dispatching. A process can have multiple threads, but at least one thread.
[0033] In the embodiment of the present application, when a process has a task to execute, a new thread will be created to execute this task, and the scheduler will select a thread in the ready state to execute. Different schedulers select the most suitable thread to run according to different methods. For example, the CFS (complete fair schedule) scheduler, etc.
[0034] If the processor of the electronic device is a multi-core processor, then each processor core can be regarded as an independent processing unit. For example, if the electronic device is an octa-core processor, then each core is an independent processing unit. Each processing unit has its own corresponding task queue, and this task queue contains the tasks assigned to this processing unit, and each task is executed by a corresponding thread.
[0035] There may be one or more programs running simultaneously in the electronic device. Each program has at least one corresponding process, and a process has at least one thread executing tasks. Therefore, there are multiple threads in the electronic device that need to be executed, and CPU resources need to be allocated for the execution of these threads. The kernel will allocate processing units to threads according to a certain core selection mechanism. After a thread is allocated a processing unit, if the thread enters the ready state and needs to be executed, it will be added to the task queue of the allocated processing unit and wait for the thread scheduler to schedule.
[0036] Among them, the life cycle of a thread can be divided into five states: New state (New): After creating a thread object using the new keyword and the Thread class or its subclasses, the thread object is in the new state. It remains in this state until the program starts this thread. Runnable state (Runnable): When the thread object calls the start() method, the thread enters the runnable state. Threads in the runnable state are added to the ready queue and wait for the thread scheduler to schedule. Running state (Running): If a thread in the runnable state obtains CPU resources, it can execute the run() method, and at this time the thread is in the running state. A thread in the running state can change to the blocked state, the runnable state, and the dead state. Blocked state (Blocked): If a thread executes methods such as sleep (sleep) and suspend (suspend) and loses the resources it occupies, the thread changes from the running state to the blocked state. It can re-enter the runnable state when the sleep time has elapsed or resources are obtained. Dead state (Dead): When a thread in the running state completes a task or other termination conditions occur, the thread switches to the terminated state.
[0037] The CFS scheduler allocates CPU usage time to threads according to the priorities of the threads. For example, if two threads with the same priority are running on a single CPU, then each thread will be allocated 50% of the CPU running time, that is, fair scheduling is achieved. When the priorities of the threads are different, the CPU running time will be allocated according to the proportion of the weights of the threads. Among them, the weight represents the priority of the thread. The greater the weight, the higher the priority, and the greater the proportion of the allocated CPU running time. Generally, the nice value is used to represent the weight of the thread. The nice value is a specific number, and the value range is [-20, 19]. The smaller the value, the greater the priority, and at the same time it also means the greater the weight value.
[0038] The CFS scheduler arranges a virtual clock for each thread in the task queue (ready queue) and records the total virtual time that a scheduling entity (i.e., a thread) has run, denoted as vruntime. If a thread is executed, its vruntime will increase, and the vruntime of a thread that has not been executed remains unchanged. When the CFS scheduler selects a thread from the task queue for execution, it always selects the slowest-running thread, that is, the thread with the smallest vruntime, to execute, in order to achieve the so-called "fully fair". Among them, the higher the priority of the thread, the slower its vruntime grows, so that it can get more running opportunities.
[0039] The relevant CFS scheduler schedules the threads in the task queue based on the above principles. When there are a large number of ready-state threads in the task queue waiting to be scheduled, at this time, even high-priority threads have to face a long waiting time.
[0040] For some threads that directly affect the user experience, such as the threads that directly or indirectly execute the interface drawing tasks, if the waiting time is too long, system jams will occur, bringing a bad experience to users. Therefore, in the embodiments of the present application, when managing the scheduling of threads based on the CFS scheduler, some threads will be marked as threads of a preset type. For example, in some embodiments, the threads of the preset type are ux (user experience) threads. Not only are higher weights assigned to ux threads, but also for the ux threads in the task queue, they will be scheduled preferentially. To shorten the waiting time of ux threads, so that they can quickly be allocated CPU running time to complete the tasks related to interface drawing as soon as possible, thereby improving the user experience in the interactive scenario and reducing the occurrence of system jams.
[0041] In the embodiments of the present application, the system architecture of the electronic device at least includes an application framework layer and a kernel layer. The application framework layer will mark some threads as ux threads, and the kernel layer is responsible for scheduling and executing the threads in the task queue.
[0042] Among them, in the embodiments of the present application, some threads that directly execute the interface drawing tasks are static ux threads, including but not limited to the following types of threads: the foreground UI (user interface) thread, the foreground Render thread, the GL thread, the distribution thread of user input events, the detection thread of user input events, the system animation thread, etc. These threads that directly participate in the interface drawing tasks are collectively referred to as static ux threads. And some threads that indirectly participate in the interface drawing tasks are dynamic ux threads, that is, these threads are not always related to the user experience, but during a certain period of their execution process, they may be associated with static ux threads through resource constraints. At this time, these threads with a resource constraint relationship with static ux threads can be promoted to ux threads. And once this resource constraint relationship ends, the thread will be restored to a non-ux thread. Specific resource constraint relationships include but are not limited to the following: ordinary threads that hold the resources required by static Ux threads, ordinary threads requested by static Ux threads through inter-process communication can also be promoted to ux threads, and so on.
[0043] If the upper-layer application framework layer determines that a certain created thread belongs to the ux thread, it directly marks the thread as the ux thread. After the underlying kernel layer assigns the ux thread to a certain processing unit, when the processing unit schedules threads, it can preferentially schedule the ux thread.
[0044] A multi-core processor refers to a processor that integrates two or more complete computing engines (cores). For an electronic device with a multi-core processor, each core has a corresponding task queue. After a thread finishes execution, it is necessary to select the next task to be executed from the task queue. The CFS scheduler generally selects the thread with the shortest running time to execute. However, in this embodiment, when selecting a thread from the task queue, it is first determined whether there is a ux thread in the task queue. For example, by determining whether there is a thread with a preset label in the task queue. For example, a thread carrying the ux label is a ux thread.
[0045] In 102, when there is a thread of a preset type in the task queue, the thread of the preset type in the task queue is used as the target thread.
[0046] If the kernel detects that there is a ux thread in the task queue, these ux threads are used as the target threads that need to be preferentially executed. When there is a ux thread in the task queue, there may be one or more existing ux threads. If there are multiple ux threads, these ux threads can all be used as target threads.
[0047] In 103, the target thread with the shortest running time is determined from the task queue, and the task corresponding to the target thread with the shortest running time is executed.
[0048] The running time here refers to the virtual running time, that is, vruntime, obtained by the CFS scheduler converting the actual running time according to a certain conversion rule.
[0049] If there are multiple target threads, for these multiple ux threads, still in accordance with the basic principle of the CFS scheduler, the ux thread with the shortest running time is selected for execution. When there are multiple ux threads, the weights of these ux threads are different and the vruntime is different. Therefore, among these multiple ux threads, the one with the smallest vruntime should be preferentially selected to run.
[0050] In 104, when there is no thread with a preset label in the task queue, the task corresponding to the thread with the shortest running time in the task queue is executed.
[0051] If there is no ux thread in the task queue, still in accordance with the basic principle of the CFS scheduler, the thread with the shortest running time is selected and the thread is run to execute the task corresponding to the thread.
[0052] In specific implementation, the present application is not limited by the execution order of the various steps described. Without conflict, some steps can also be carried out in other orders or simultaneously.
[0053] As can be seen from the above, in the task scheduling method provided by the embodiments of the present application, when selecting a thread to execute from the task queue, it is first determined whether there is a thread of a preset type in the task queue. If there is a thread of the preset type in the task queue, the thread of the preset type in the task queue is used as the target thread, the target thread with the shortest running time is determined from the task queue, and the task corresponding to the target thread with the shortest running time is executed. If there is no thread of the preset type in the task queue, the task corresponding to the thread with the shortest running time in the task queue is executed. In this way, the threads directly or indirectly executing the interface drawing task are marked as threads of the preset type, and these threads directly or indirectly executing the interface drawing task will affect the user experience. When the kernel executes a task, it will detect whether there are these threads affecting the user experience in the task queue. If so, these preset type threads will be preferentially selected to quickly occupy the CPU resources, which can effectively reduce the waiting time of the preset type threads and avoid the situation that too many background task threads cause the threads related to the user experience to not be scheduled in time, thereby improving the user experience in the interaction scenario and reducing the occurrence of lags.
[0054] In some embodiments, after executing the task corresponding to the thread with the shortest running time in the task queue, it further includes: when the task is completed, return to execute the task queue corresponding to the target processing unit, and determine whether there is a thread of a preset type in the task queue.
[0055] For the system kernel, as long as there are tasks to be executed in the task queue, the processor has to execute these tasks. Therefore, when the task corresponding to the non-ux thread or ux thread with the shortest running time in the task queue is completed, it is necessary to continue to select a new task to execute from the task queue. At this time, it is necessary to return to 101 and re-execute 101 to 104.
[0056] In some embodiments, the method further includes: when it is detected that a thread of a preset type in the task queue enters a blocked state, detecting whether there is an associated thread in the task queue that has a resource constraint relationship with the thread of the preset type that enters the blocked state; when there is the associated thread in the task queue, marking the associated thread as a thread of the preset type.
[0057] This embodiment proposes a solution for dynamically promoting non-ux threads to ux threads. The kernel monitors the status of ux threads in the task queue in real time to detect whether any ux thread enters a blocked state. When a ux thread enters a blocked state due to resource constraints or binder communication obstruction between this ux thread and other ordinary threads, etc., at this time, it can be detected whether there are associated threads in the task queue that have a resource constraint relationship with the blocked ux thread. If so, these associated threads are promoted to ux threads.
[0058] In this embodiment, by this way of dynamically marking ux threads, threads related to user experience are distinguished from other ordinary threads, further improving the execution speed of ux threads and enhancing the user experience.
[0059] In some embodiments, the method further includes: when a new thread is created, determining whether the new thread is used to execute an interface drawing task; if the new thread is used to execute an interface drawing task, marking the new thread as a thread of a preset type; when the thread is allocated to a target processing unit according to a resource allocation rule, adding the new thread to the task queue of the target processing unit.
[0060] In this embodiment, when a thread is created, it will be identified whether the thread is a ux thread. By determining whether the created thread is used to execute an interface drawing task, it is judged whether the thread is directly related to the user experience. If so, these threads are marked as ux threads. After the system allocates the thread to a certain target processing unit according to a resource allocation rule (such as a core selection mechanism), the ux thread will be added to the task queue of the target processing unit and wait to be scheduled.
[0061] Among them, if the new thread is used to execute an interface drawing task, a preset label is added to the thread to mark the thread as a thread of a preset type. If the new thread is not used to execute an interface drawing task, then when the thread is allocated to a target processing unit according to a resource allocation rule, the thread is added to the task queue of the target processing unit. For example, the preset label is a ux label.
[0062] Among them, Linux uses the task_struct structure to describe and record threads, and each thread has a unique task_struct structure of its own. Information such as the identifier, status, priority, memory pointer, context data, etc. of the thread are recorded in the task_struct. There is a sched_entity structure in each task_struct, and the vruntime and weight of the process are saved in this structure. The kernel can judge whether a new thread is used to execute an interface drawing task based on the task_struct data.
[0063] In some embodiments, each task queue maintains a balanced binary tree, which mounts the threads to be executed in the task queue. The threads in the task queue correspond to the nodes one by one, and the value of the node is the vruntime of the corresponding thread. Among them, in some embodiments, the balanced binary tree can be a red-black tree.
[0064] When a newly created thread enters the ready state, it needs to be added to the task queue, and the initial value of vruntime is generally 0. If the thread is executed, calculate the actual running time of the computing thread, convert it to the virtual running time, update the vruntime according to the virtual running time, and then adjust the position of the thread on the red-black tree according to the updated vruntime.
[0065] Through this maintenance method of the red-black tree, without considering ux threads, the CFS scheduler selects the leftmost thread in the red-black tree to run. As time goes by, the threads that have run on the left before will gradually move to the right of the red-black tree, and the threads on the right will eventually move to the leftmost. Therefore, each thread in the red-black tree has the opportunity to run. In the case of ux threads in the task queue, the CFS scheduler preferentially selects the leftmost ux thread on the red-black tree to run. After the ux thread finishes running, other non-ux threads are run. It can effectively reduce the waiting time of ux threads and avoid the situation that ux threads related to the user experience cannot be scheduled in time due to too many background task threads, thereby improving the user experience in the interactive scenario and reducing the occurrence of system jams.
[0066] According to the method described in the previous embodiments, the following will give examples for further detailed description.
[0067] Please refer to Figure 2 , Figure 2 which is the second process schematic diagram of the task scheduling method provided by the embodiment of the present invention.
[0068] The method includes:
[0069] In 201, when a new thread is created, it is judged whether the new thread is used to execute the interface drawing task.
[0070] In 202, if the new thread is used to execute the interface drawing task, add a ux label to the new thread to mark the new thread as a ux thread.
[0071] In 203, when the thread is allocated to the target processing unit according to the resource allocation rule, add the new thread to the task queue of the target processing unit.
[0072] In this embodiment, when a thread is created, it is identified whether the thread is a ux thread. The application framework layer determines whether the created thread is used to execute the interface drawing task, and judges whether the thread is directly related to the user experience. If so, these threads are marked as ux threads. After the system allocates the thread to a certain target processing unit according to the resource allocation rule (such as the core selection mechanism), the kernel layer adds the ux thread to the task queue of the target processing unit and waits to be scheduled.
[0073] Among them, if the new thread is used to execute the interface drawing task, add a ux label to the thread to mark the thread as a ux thread. If the new thread is not used to execute the interface drawing task, when the thread is allocated to the target processing unit according to the resource allocation rule, the thread is directly added to the task queue of the target processing unit.
[0074] In 204, obtain the task queue of the target processing unit, and judge whether there is a ux thread in the task queue.
[0075] For the kernel, as long as there are tasks in the task queue, these tasks need to be executed. After the previous task is completed, it is necessary to continue to select a new task from the task queue to execute. At this time, obtain the task queue and judge whether there is a ux thread in the task queue. The thread with the ux label is the ux thread.
[0076] Ux threads include static ux threads and dynamic ux threads. Ux threads are some specific types of threads. Dynamic ux threads are generally obtained by marking other threads that were not originally ux threads as ux threads. For specific details, please refer to the above embodiments and will not be elaborated here.
[0077] When there is a ux thread in the task queue, execute 205; when there is no ux thread in the task queue, execute 206.
[0078] In 205, determine the ux thread with the shortest running time from the task queue, and execute the task corresponding to the ux thread with the shortest running time.
[0079] In 206, execute the task corresponding to the thread with the shortest running time in the task queue.
[0080] If the kernel detects that there is a ux thread in the task queue, these ux threads are used as target threads that need to be executed preferentially. When there is a ux thread in the task queue, there may be one or more existing ux threads. If there are multiple ux threads, these ux threads can all be used as target threads.
[0081] If there are multiple ux threads, for these multiple ux threads, still in accordance with the basic principle of the CFS scheduler, select the ux thread with the shortest running time to execute. When there are multiple ux threads, the weights of these ux threads are different and the vruntimes are different. Therefore, within these multiple ux threads, it is also necessary to preferentially select the one with the smallest vruntime to run.
[0082] Please refer to Figure 3 , Figure 3 which is a schematic diagram of the red-black tree structure in the task scheduling method provided by the embodiment of the present invention.
[0083] Each task queue maintains a corresponding red-black tree. This red-black tree mounts the threads to be executed in the task queue. The threads in the task queue correspond one by one with the nodes, and the value of the node is the vruntime of the corresponding thread. When a newly created thread enters the ready state, it needs to be added to the task queue. The initial value of the vruntime is generally 0. If the thread is executed, calculate the actual running time of the thread operation, convert it into the virtual running time, update the vruntime according to this virtual running time, and then adjust the position of the thread on the red-black tree according to the updated vruntime.
[0084] When selecting the ux thread with the smallest vruntime, you can directly select the leftmost ux thread on the red-black tree to execute. When there are ux threads in the task queue, the CFS scheduler preferentially selects the leftmost ux thread on the red-black tree to run. When there are multiple ux threads, in the order of the vruntime of the ux threads from small to large, these multiple ux threads are run in sequence.
[0085] After the ux thread runs to completion, then run other non-ux threads. This can effectively reduce the waiting time of the ux thread and avoid the situation where too many background task threads cause the ux thread related to the user experience to not be scheduled in time, thereby improving the user experience in the interactive scenario and reducing the generation of system jams.
[0086] After the task is executed, return to execute 201. For the kernel, as long as there are tasks in the task queue, the processor has to execute these tasks. Therefore, when the task with the shortest running time among the non-ux threads or ux threads in the task queue is executed to completion, it is necessary to continue to select new tasks from the task queue to execute. At this time, it is necessary to return to 204 and re-execute 204 to 206. At the same time, when a new thread is created, 201 will also start to be executed.
[0087] As can be seen from the above, in the task scheduling method provided by the embodiment of the present invention, the threads directly or indirectly executing the interface drawing task are marked as ux threads, and the running conditions of these ux threads will directly affect the user experience. When the kernel executes a task, it will detect whether there are ux threads in the task queue. If so, these ux threads will be preferentially selected to quickly occupy the CPU resources, which can effectively reduce the waiting time of the ux threads and avoid the situation that too many background task threads cause the threads related to the user experience to not be scheduled in time, thereby improving the user experience in the interaction scenario and reducing the occurrence of system lags.
[0088] In an embodiment, a task scheduling device is further provided. Please refer to Figure 4 , Figure 4 FIG. is a schematic structural diagram of a task scheduling device 300 provided by an embodiment of the present application. The task scheduling device 300 is applied to an electronic device. The task scheduling device 300 includes a thread monitoring module 301, a target determination module 302, and a task execution module 303, as follows:
[0089] The thread monitoring module 301 is configured to obtain a task queue to be executed and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread directly or indirectly executing an interface drawing task;
[0090] The target determination module 302 is configured to, when there is a thread of a preset type in the task queue, use the thread of the preset type in the task queue as a target thread;
[0091] The task execution module 303 is configured to determine the target thread with the shortest running time from the task queue and execute the task corresponding to the target thread with the shortest running time;
[0092] The task execution module 303 is further configured to, when there is no thread with a preset label in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue.
[0093] In some embodiments, the thread monitoring module 301 is further configured to:
[0094] In some embodiments, the task scheduling device 300 further includes a first marking module, and the first marking module is configured to: when it is detected that a thread of a preset type in the task queue enters a blocked state, detect whether there is an associated thread in the task queue that has a resource constraint relationship with the thread of the preset type that enters the blocked state; when there is the associated thread in the task queue, mark the associated thread as a thread of the preset type.
[0095] In some embodiments, the task scheduling device 300 further includes a second marking module, which is configured to: when a new thread is created, determine whether the new thread is used to execute an interface drawing task; if the new thread is used to execute an interface drawing task, mark the new thread as a thread of a preset type; when the thread is allocated to a target processing unit according to a resource allocation rule, add the new thread to the task queue of the target processing unit.
[0096] In some embodiments, the second marking module is further configured to: if the new thread is used to execute an interface drawing task, add a preset label to the thread to mark the thread as a thread of a preset type.
[0097] In some embodiments, the second marking module is further configured to: if the new thread is not used to execute an interface drawing task, add the thread to the task queue of the target processing unit when the thread is allocated to the target processing unit according to a resource allocation rule.
[0098] In some embodiments, the thread monitoring module 301 is further configured to: after the task is completed, return to execute the task of obtaining the task queue corresponding to the target processing unit, and determine whether there is a thread of a preset type in the task queue.
[0099] In specific implementation, each of the above modules may be implemented as an independent entity, or may be combined arbitrarily to be implemented as the same or several entities. For the specific implementation of each of the above modules, reference may be made to the foregoing method embodiments, which will not be elaborated herein.
[0100] It should be noted that the task scheduling device provided in the embodiments of the present application and the task scheduling method in the foregoing embodiments belong to the same concept. Any method provided in the embodiments of the task scheduling method can be run on the task scheduling device. For the specific implementation process, reference may be made to the embodiments of the task scheduling method, which will not be elaborated herein.
[0101] As can be seen from the above, the task scheduling device proposed in the embodiments of the present application includes a thread monitoring module 301, a target determination module 302, and a task execution module 303. When selecting a thread to execute from the task queue, the thread monitoring module 301 first determines whether there is a thread of a preset type in the task queue. If there is a thread of the preset type in the task queue, the target determination module 302 uses the thread of the preset type in the task queue as the target thread. The task execution module 303 determines the target thread with the shortest running time from the task queue and executes the task corresponding to the target thread with the shortest running time. If there is no thread of the preset type in the task queue, the task execution module 303 executes the task corresponding to the thread with the shortest running time in the task queue. By this means, the threads directly or indirectly executing the interface drawing task are marked as threads of the preset type, and these threads directly or indirectly executing the interface drawing task will affect the user experience. When the kernel executes a task, it will detect whether there are these threads affecting the user experience in the task queue. If so, it will preferentially select these threads of the preset type, quickly occupy the CPU resources, which can effectively reduce the waiting time of the threads of the preset type and avoid the situation that too many background task threads cause the threads related to the user experience to not be scheduled in time, thereby improving the user experience in the interaction scenario and reducing the occurrence of system lags.
[0102] The embodiments of the present application also provide an electronic device. The electronic device may be a device such as a smart phone or a tablet computer. Please refer to Figure 5 , Figure 5 which is the first structural schematic diagram of the electronic device provided by the embodiments of the present application. The electronic device 400 includes a processor 401 and a memory 402. Among them, the processor 401 is electrically connected to the memory 402.
[0103] The processor 401 is the control center of the electronic device 400, connects various parts of the entire electronic device through various interfaces and lines, executes various functions of the electronic device and processes data by running or calling the computer program stored in the memory 402 and calling the data stored in the memory 402, thereby performing overall monitoring of the electronic device.
[0104] The memory 402 can be used to store computer programs and data. The computer program stored in the memory 402 contains instructions that can be executed in the processor. The computer program can form various functional modules. The processor 401 executes various functional applications and data processing by calling the computer program stored in the memory 402.
[0105] In this embodiment, the processor 401 in the electronic device 400 will load the instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 will run the computer programs stored in the memory 402 to implement various functions:
[0106] Obtain a task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes an interface drawing task;
[0107] When there is a thread of a preset type in the task queue, use the thread of the preset type in the task queue as the target thread;
[0108] Determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time;
[0109] When there is no thread with a preset label in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue.
[0110] In some embodiments, please refer to Figure 6 , Figure 6 which is the second structural schematic diagram of the electronic device provided by the embodiment of the present application. The electronic device 400 further includes: a radio frequency circuit 403, a display screen 404, a control circuit 405, an input unit 406, an audio circuit 407, a sensor 408, and a power supply 409. Among them, the processor 401 is electrically connected to the radio frequency circuit 403, the display screen 404, the control circuit 405, the input unit 406, the audio circuit 407, the sensor 408, and the power supply 409 respectively.
[0111] The radio frequency circuit 403 is used to receive and transmit radio frequency signals to communicate with a network device or other electronic devices through wireless communication.
[0112] The display screen 404 can be used to display information input by the user or information provided to the user, as well as various graphical user interfaces of the electronic device. These graphical user interfaces can be composed of images, texts, icons, videos, and any combination thereof.
[0113] The control circuit 405 is electrically connected to the display screen 404 and is used to control the display screen 404 to display information.
[0114] The input unit 406 can be used to receive input digital, character information, or user characteristic information (such as fingerprints), and generate keyboard, mouse, joystick, optical, or trackball signal inputs related to user settings and function controls. Among them, the input unit 406 may include a fingerprint recognition module.
[0115] The audio circuit 407 can provide an audio interface between the user and the electronic device through a speaker and a microphone. Among them, the audio circuit 407 includes a microphone. The microphone is electrically connected to the processor 401. The microphone is used to receive the voice information input by the user.
[0116] The sensor 408 is used to collect external environmental information. The sensor 408 may include one or more of sensors such as an ambient light sensor, an acceleration sensor, and a gyroscope.
[0117] The power supply 409 is used to supply power to each component of the electronic device 400. In some embodiments, the power supply 409 may be logically connected to the processor 401 through a power management system, so as to implement functions such as management of charging, discharging, and power consumption management through the power management system.
[0118] Although not shown in the figure, the electronic device 400 may further include a camera, a Bluetooth module, etc., which will not be elaborated here.
[0119] In this embodiment, the processor 401 in the electronic device 400 will load the instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 will run the computer programs stored in the memory 402 to implement various functions:
[0120] Obtain the task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes the interface drawing task;
[0121] When there is a thread of a preset type in the task queue, use the thread of the preset type in the task queue as the target thread;
[0122] Determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time;
[0123] When there is no thread with a preset label in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue.
[0124] As can be seen from the above, the embodiments of the present application provide an electronic device. When the electronic device selects a thread from a task queue for execution, it first determines whether there is a thread of a preset type in the task queue. If there is a thread of the preset type in the task queue, the thread of the preset type in the task queue is used as the target thread, and the target thread with the shortest running time is determined from the task queue, and the task corresponding to the target thread with the shortest running time is executed. If there is no thread of the preset type in the task queue, the task corresponding to the thread with the shortest running time in the task queue is executed. In this way, the threads directly or indirectly executing the interface drawing task are marked as threads of the preset type, and these threads directly or indirectly executing the interface drawing task will affect the user experience. When the kernel executes a task, it will detect whether there are these threads affecting the user experience in the task queue. If so, these threads of the preset type will be preferentially selected to quickly occupy the CPU resources, which can effectively reduce the waiting time of the threads of the preset type and avoid the situation that too many background task threads cause the threads related to the user experience to not be scheduled in time, thereby improving the user experience in the interaction scenario and reducing the occurrence of system jams.
[0125] The embodiments of the present application also provide a storage medium, in which a computer program is stored. When the computer program runs on a computer, the computer executes the task scheduling method described in any one of the above embodiments.
[0126] It should be noted that those of ordinary skill in the art can understand that all or part of the steps in the above methods of the embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the storage medium can include but is not limited to: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disk, etc.
[0127] In addition, the terms "first", "second", "third", etc. in the present application are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or modules is not limited to the listed steps or modules, but some embodiments also include steps or modules that are not listed, or some embodiments also include other steps or modules inherent to these processes, methods, products or devices.
[0128] The above has introduced in detail the task scheduling method, device, storage medium and electronic device provided by the embodiments of the present application. Specific examples are used in this article to elaborate on the principle and implementation manner of the present application. The description of the above embodiments is only used to help understand the method and its core idea of the present application; at the same time, for those skilled in the art, according to the idea of the present application, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present application.
Claims
1. A task scheduling method, characterized in that including: obtain a task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes an interface drawing task; when there is a thread of a preset type in the task queue, use the thread of the preset type in the task queue as the target thread; determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time, where the running time refers to the total virtual time that the thread has run; when there is no thread with a preset label in the task queue, execute the task corresponding to the thread with the shortest running time in the task queue; when it is detected that a thread of a preset type in the task queue enters a blocked state, detect whether there is an associated thread in the task queue that has a resource constraint relationship with the thread of the preset type that enters the blocked state; when there is the associated thread in the task queue, mark the associated thread as a thread of the preset type; when the resource constraint relationship ends, restore the thread marked as a thread of the preset type to a non-preset type thread.
2. The task scheduling method according to claim 1, wherein The determining whether there is a thread of a preset type in the task queue includes: determine whether there is a thread with a preset label in the task queue.
3. The task scheduling method according to claim 1, characterized in that The method further includes: when a new thread is created, determine whether the new thread is used to execute an interface drawing task; if the new thread is used to execute an interface drawing task, mark the new thread as a thread of the preset type; when the thread is allocated to a target processing unit according to a resource allocation rule, add the new thread to the task queue of the target processing unit.
4. The task scheduling method according to claim 3, characterized in that The if the new thread is used to execute an interface drawing task, mark the new thread as a thread of the preset type includes: if the new thread is used to execute an interface drawing task, add a preset label to the thread to mark the thread as a thread of the preset type.
5. The task scheduling method according to claim 3, characterized in that, After determining whether the new thread is used to execute an interface drawing task, it further includes: if the new thread is not used to execute an interface drawing task, when the thread is allocated to a target processing unit according to a resource allocation rule, add the thread to the task queue of the target processing unit.
6. The task scheduling method according to claim 1, characterized in that After executing the task corresponding to the thread with the shortest running time in the task queue, it further includes: when the task is completed, return to execute obtaining the task queue corresponding to the target processing unit, and determine whether there is a thread of a preset type in the task queue.
7. A task scheduling device, characterized in that, including: a thread monitoring module, configured to obtain a task queue to be executed, and determine whether there is a thread of a preset type in the task queue, where the thread of the preset type is a thread that directly or indirectly executes an interface drawing task; a target determining module, configured to use the thread of the preset type in the task queue as the target thread when there is a thread of a preset type in the task queue; a task execution module, configured to determine the target thread with the shortest running time from the task queue, and execute the task corresponding to the target thread with the shortest running time, where the running time refers to the total virtual time that the thread has run; The task execution module is further configured to execute the task corresponding to the thread with the shortest running time in the task queue when there is no thread with a preset tag in the task queue; When it is detected that a thread of a preset type in the task queue enters a blocked state, it is detected whether there is an associated thread in the task queue that has a resource constraint relationship with the thread of the preset type that enters the blocked state; When the associated thread exists in the task queue, the associated thread is marked as a thread of the preset type.
8. A storage medium having a computer program stored thereon, characterized in that, When the computer program runs on a computer, the computer is caused to execute the task scheduling method according to any one of claims 1 to 6.
9. An electronic device, comprising a processor and a memory, the memory storing a computer program, characterized in that, The processor is configured to execute the task scheduling method according to any one of claims 1 to 6 by calling the computer program.
Citation Information
Patent Citations
Ocean control stock valuation process control method and device, computer equipment and storage medium
CN109783242A
A big data task scheduling method in an education cloud platform
CN109947532A