Thread scheduling method, device, electronic device and storage medium

By adjusting thread priority according to thread type in the target application, the problem of preemptive execution of important task threads in the prior art is solved, and the operation effect of the program is improved.

CN114816748BActive Publication Date: 2025-04-08BEIJING DAJIA INTERNET INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210429634.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-22
Publication Date
2025-04-08
Estimated Expiration
2042-04-22

AI Technical Summary

Technical Problem

In the prior art, the program cannot interfere with thread scheduling during operation, resulting in threads of important tasks being preempted by high-priority threads, resulting in slow execution and poor program operation effect.

Method used

By responding to user operations, starting the target application, and during the application running, determine the thread type of each thread in the current running scenario, adjust its priority according to the thread type, and ensure that the target thread obtains the highest priority to prioritize core tasks.

Benefits of technology

It effectively avoids the situation where important task threads are preempted and executed too slowly, improves the operation effect of the program, and ensures that core tasks can be executed efficiently.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114816748B_ABST
    Figure CN114816748B_ABST
Patent Text Reader

Abstract

The present disclosure is about a thread scheduling method, device, electronic device and storage medium, including: starting a target application in response to a user's operation instruction; during the operation of the target application, determining the thread type of each thread in the current operation scenario among the multiple threads corresponding to the target application; adjusting the priority of each thread based on the thread type of each thread in the current operation scenario, wherein the thread type includes a target thread and a non-target thread corresponding to the current operation scenario, and each operation scenario is pre-registered with a corresponding target thread and a target task executed by the target thread. In this way, during the operation of the target application, the priority of each thread can be adjusted based on the thread type of each thread in the current operation scenario, so as to avoid the situation where the thread where the more important task is located is preempted by other threads with higher priority and executed too slowly, and the program operation effect is better.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and more specifically, to a thread scheduling method, device, electronic device and storage medium. Background Art

[0002] Threads are the basic unit of program operation. During the program operation, there may be dozens of threads running at the same time. Moreover, the time slices allocated to threads by each device at the same time are limited. Depending on the priority of the thread, each thread will get a different time slice. Threads of the same priority can be allocated the same time slice, and low-priority threads will be preempted by high-priority threads.

[0003] In the related art, developers can only specify the initial priority of the thread and cannot intervene in the scheduling of the thread during the program running. The program may dynamically adjust the priority of each thread during the running process. In this case, the thread where the more important task is located may be preempted by other threads with higher priority and execute too slowly, resulting in poor program running performance. Summary of the invention

[0004] The present disclosure provides a thread scheduling method, device, electronic device and storage medium to at least solve the problem of poor program running effect in the above-mentioned related technologies.

[0005] According to a first aspect of an embodiment of the present disclosure, a thread scheduling method is provided, comprising: in response to a user's operation instruction, starting a target application; during the running of the target application, determining the thread type of each thread among multiple threads corresponding to the target application in a current running scenario; based on the thread type of each thread in the current running scenario, adjusting the priority of each thread, wherein the thread type includes a target thread and a non-target thread corresponding to the current running scenario, the target application includes multiple running scenarios, each running scenario is pre-registered with a corresponding target thread and a target task executed by the target thread.

[0006] Optionally, adjusting the priority of each thread based on the thread type of each thread in the current running scenario includes: if the thread type of each thread in the current running scenario is a non-target thread and the priority of each thread is at the highest priority, lowering the priority of the non-target thread in the current running scenario whose priority is at the highest priority.

[0007] Optionally, lowering the priority of the non-target thread with the highest priority in the current running scenario includes: lowering the priority of the non-target thread with the highest priority in the current running scenario by one level according to a predetermined priority order.

[0008] Optionally, after the step of adjusting the priority of each thread based on the thread type of each thread in the current running scenario, the thread scheduling method also includes: detecting whether there is a task to be executed in the current running scenario; if there is a task to be executed in the current running scenario, determining the task type of the task to be executed; and selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed.

[0009] Optionally, selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed includes: when the task type of the task to be executed is a user interface UI task, selecting a main thread from the multiple threads to execute the UI task.

[0010] Optionally, selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed includes: when the task type of the task to be executed is an input / output (IO) task, selecting a sub-thread with the highest priority from the multiple threads to execute the IO task.

[0011] Optionally, selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed includes: when the task type of the task to be executed is a central processing unit (CPU) intensive task, detecting whether there is a spare sub-thread for processing CPU intensive tasks in the multiple threads; if there is a spare sub-thread for processing CPU intensive tasks in the multiple threads, using the spare sub-thread for processing CPU intensive tasks to execute the CPU intensive task; otherwise, controlling the task to be executed to queue and wait until there is a spare sub-thread for processing CPU intensive tasks in the multiple threads to execute the task to be executed.

[0012] Optionally, the thread scheduling method further includes: when the lock function corresponding to the current running scenario is triggered, searching for a thread holding the lock function from the multiple threads; and raising the priority of the thread holding the lock function to the highest priority.

[0013] According to a second aspect of an embodiment of the present disclosure, a thread scheduling device is provided, comprising: a startup module, configured to start a target application in response to a user's operation instruction; a thread type determination module, configured to determine, during the running process of the target application, the thread type of each thread among multiple threads corresponding to the target application in a current running scenario; an adjustment module, configured to adjust the priority of each thread based on the thread type of each thread in the current running scenario, wherein the thread type includes a target thread and a non-target thread corresponding to the current running scenario, the target application includes multiple running scenarios, and each running scenario is pre-registered with a corresponding target thread and a target task executed using the target thread.

[0014] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the thread scheduling method according to the present disclosure.

[0015] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the thread scheduling method according to the present disclosure.

[0016] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the thread scheduling method according to the present disclosure is implemented.

[0017] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:

[0018] During the running of the target application, the priority of each thread can be adjusted based on the thread type of each thread in the current running scenario to avoid the situation where the thread with more important tasks is preempted by other higher-priority threads and executes too slowly, so that the program runs better.

[0019] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.

[0021] Figure 1 is a flowchart illustrating a thread scheduling method according to an exemplary embodiment of the present disclosure;

[0022] Figure 2 is a flowchart showing a specific implementation of a thread scheduling method according to an exemplary embodiment of the present disclosure;

[0023] Figure 3 is a block diagram showing a thread scheduling device according to an exemplary embodiment of the present disclosure;

[0024] Figure 4 is a block diagram illustrating an electronic device according to an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0025] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.

[0026] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The implementation methods described in the following examples do not represent all implementation methods consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the attached claims.

[0027] It should be noted that the phrase "at least one of the items" in the present disclosure includes three types of parallel situations: "any one of the items", "a combination of any number of the items", and "all of the items". For example, "including at least one of A and B" includes the following three parallel situations: (1) including A; (2) including B; (3) including A and B. Another example is "executing at least one of step 1 and step 2" which means the following three parallel situations: (1) executing step 1; (2) executing step 2; (3) executing step 1 and step 2.

[0028] Figure 1 is a flowchart illustrating a thread scheduling method according to an exemplary embodiment of the present disclosure.

[0029] Reference Figure 1 In step 101, in response to a user's operation instruction, a target application may be started. For example, a user may click an icon of a target application on a terminal with a finger, thereby starting the target application.

[0030] In step 102, during the operation of the target application, the thread type of each thread in the multiple threads corresponding to the target application in the current operation scene can be determined. The thread type may include a target thread and a non-target thread corresponding to the current operation scene. The target application may include multiple operation scenes, and each operation scene may be pre-registered with a corresponding target thread and a target task executed by the target thread.

[0031] For example, the target application may include three running scenarios that run in sequence, namely a theme interface running scenario, a data request running scenario, and a video playback running scenario.

[0032] In the theme interface running scenario, it is necessary to display the theme interface of the target application to the user. At this time, the user interface (UI) task is the main task, that is, the task of rendering the theme interface is the main task. The user interface (UI) task can be registered for the theme interface running scenario as the target task under the running scenario, and the main thread used to process the user interface (UI) task can be registered as the target thread under the running scenario.

[0033] In the request data running scenario, data needs to be pulled. At this time, the input output (IO) task is the main task, that is, the task of sending and receiving data is the main task. The input output (IO) task can be registered as the target task under the running scenario for the request data running scenario, and the sub-thread used to process the input output (IO) task can be registered as the target thread under the running scenario.

[0034] In the video playback operation scenario, the pulled video frames need to be decoded. At this time, the central processing unit (CPU) intensive task is the main task, that is, the task of decoding the video frames is the main task. The task of decoding the video frames can be registered as the target task under the operation scenario for the video playback operation scenario, and the sub-thread used to decode the video frames can be registered as the target thread under the operation scenario.

[0035] In step 103, the priority of each thread may be adjusted based on the thread type of each thread in the current running scenario.

[0036] According to an exemplary embodiment of the present disclosure, when there is a target thread corresponding to the current running scene in multiple threads, the priority of the target thread can be adjusted to the highest priority among the priorities of the multiple threads. For example, when the current running scene is the theme interface running scene, the priority of the main thread for processing the user interface (UI) task corresponding to the theme interface running scene can be adjusted to the highest priority; when the current running scene is the request data running scene, the priority of the sub-thread for processing the input and output (IO) task corresponding to the request data running scene can be adjusted to the highest priority; when the current running scene is the play video running scene, the priority of the sub-thread for decoding the video frame corresponding to the play video running scene can be adjusted to the highest priority. In this way, by adjusting the priority of the target thread corresponding to the current running scene to the highest priority among the priorities of multiple threads, it can be ensured that the target task in the current running scene can be executed with high priority, that is, it can be ensured that the core task in the current running scene can be executed with priority, avoiding the situation where the target thread where the core task in the current running scene is located is preempted by other threads with higher priority and executed too slowly, and the program running effect is better.

[0037] According to an exemplary embodiment of the present disclosure, it should be noted that if there are non-target threads with the highest priority, the target thread no longer has a priority advantage over these non-target threads with the highest priority, which will result in a reduction in the time slice allocated to the target thread. Therefore, if the thread type of each thread in the current running scenario is a non-target thread, and the priority of each thread is the highest priority, the priority of the non-target thread with the highest priority in the current running scenario can be lowered to ensure that the target thread becomes the only thread with the highest priority. In this way, the non-target thread with the highest priority can be prevented from competing with the target thread for the time slice, and the target task on the target thread can be executed as quickly as possible.

[0038] According to the exemplary embodiments of the present disclosure, it should be noted that the priority of a thread may include four levels, namely background, low, default and high, and these four levels are gradually increased, that is, background is the lowest priority and high is the highest priority. The priority of a non-target thread with the highest priority and in the current running scenario may be reduced by one level in a predetermined priority order, that is, the priority of a non-target thread with the highest priority high may be reduced by one level, that is, reduced to default.

[0039] According to an exemplary embodiment of the present disclosure, it is possible to detect whether there is a task to be executed in the current running scenario. In the case where there is a task to be executed in the current running scenario, the task type of the task to be executed can be determined. Then, according to the task type of the task to be executed, a thread can be selected from multiple threads to execute the task to be executed.

[0040] According to an exemplary embodiment of the present disclosure, the task type may include at least one of a user interface (UI) task, an input / output (IO) task, and a central processing unit (CPU) intensive task. The user interface (UI) task is a task for rendering a user interface, that is, the user interface (UI) task is a task for rendering a theme interface of an application; the input / output (IO) task is a task for sending and receiving data, and the central processing unit (CPU) intensive task is a task that requires the CPU to continuously perform calculations, for example, the central processing unit (CPU) intensive task may be a task for decoding video frames.

[0041] According to an exemplary embodiment of the present disclosure, when the task type of the task to be executed is a user interface (UI) task, a main thread among multiple threads may be selected to execute the user interface (UI) task.

[0042] According to an exemplary embodiment of the present disclosure, when the task type of the task to be executed is an input output (IO) task, a sub-thread with the highest priority can be selected from multiple threads to execute the input output (IO) task. It should be noted that since the input output (IO) task does not occupy the time slice of the thread, it is not necessary to control the number of concurrent threads.

[0043] According to an exemplary embodiment of the present disclosure, when the task type of the task to be executed is a central processing unit (CPU) intensive task, the execution status of the central processing unit (CPU) intensive task is related to the number of CPU cores. For example, the number of CPU cores may be 4, but there are only 2 active cores. If there are 3 or 4 central processing unit (CPU) intensive tasks to be processed, which exceeds the number of active cores of the CPU, 2, then each central processing unit (CPU) intensive task will grab the time slice, and an active core of a CPU cannot always calculate a central processing unit (CPU) intensive task, resulting in each central processing unit (CPU) intensive task being unable to be fully executed.

[0044] In order to solve the above problems, the thread scheduling method disclosed in the present invention can detect whether there are spare sub-threads for processing CPU-intensive tasks in multiple threads when the task type of the task to be executed is a central processing unit (CPU) intensive task. If there are spare sub-threads for processing CPU-intensive tasks in multiple threads, the spare sub-threads for processing CPU-intensive tasks can be used to execute the CPU-intensive tasks; if there are no spare sub-threads for processing CPU-intensive tasks in multiple threads, if the newly detected CPU-intensive tasks are forcibly executed at this time, the time slices of other tasks being executed will be deleted, resulting in the inability to fully execute other tasks being executed. Therefore, the tasks to be executed can be controlled to queue and wait until there are spare sub-threads for processing CPU-intensive tasks in multiple threads to execute the tasks to be executed.

[0045] According to an exemplary embodiment of the present disclosure, when the lock function corresponding to the current running scene is triggered, the thread holding the lock function can be searched from multiple threads. Next, the priority of the thread holding the lock function can be raised to the highest priority. For example, when the current running scene is the playing video running scene, the sub-thread used to decode the video frame is the target thread, that is, the sub-thread decoding the video frame is in a high priority execution state at this time. If the lock function of the playing video running scene is triggered, the sub-thread used to decode the video frame will be paused (blocked) at this time, and the sub-thread decoding the video frame can continue to run after waiting for the thread holding the lock function of the playing video running scene to release the lock function. Therefore, the priority of the thread holding the lock function can be raised to the highest priority, so that the thread holding the lock function can be executed as quickly as possible, and the blocked target thread can be guaranteed to resume operation as soon as possible.

[0046] According to an exemplary embodiment of the present disclosure, a hook function is pre-added to the lock function corresponding to each of the multiple running scenarios. The hook function is used to indicate the thread holding the lock function among the multiple threads when the lock function is triggered. In this way, by pre-adding a hook function to the lock function corresponding to each of the multiple running scenarios, when the lock function corresponding to a certain running scenario is triggered, the hook function pre-added to the running scenario can be used to know which thread holds the lock function, and then the priority of the thread holding the lock function can be raised to the highest priority, so that the thread holding the lock function can be executed as quickly as possible. Even if the thread holding the lock function releases the lock function as quickly as possible, it can be ensured that the target thread blocked due to the triggering of the lock function resumes operation as soon as possible.

[0047] Figure 2is a flowchart showing a specific implementation of a thread scheduling method according to an exemplary embodiment of the present disclosure.

[0048] Reference Figure 2 The specific implementation scheme of the thread scheduling method disclosed in the present invention may include four parts, namely, a registration part, a thread scheduling part, a thread pool scheduling part and a trigger lock function part.

[0049] In step 201, the thread to be scheduled is registered to the specified scene, that is, the corresponding target threads are registered for different scenes.

[0050] In step 202, the task to be executed is registered to the specified scene, that is, the corresponding target tasks are registered for different scenes, wherein the target task is a task executed by the target thread in the corresponding scene.

[0051] In step 203, hook functions are added to lock functions in different scenarios.

[0052] In step 204, after the user starts the application, the running scene is triggered during the running of the application.

[0053] In step 205, it is determined whether there is a thread that needs to be scheduled in the current running scenario.

[0054] In step 206, when there is a thread that needs to be scheduled in the current running scenario, all threads of the application are traversed to find the target thread corresponding to the current running scenario.

[0055] In step 207, the priority of the target thread in the current running scenario is adjusted to the highest priority that can be scheduled.

[0056] In step 208, it is determined whether there is a non-target thread with high CPU usage in the current running scenario.

[0057] In step 209, when there is a non-target thread with high CPU usage in the current running scenario, the priority of the non-target thread with high CPU usage is lowered.

[0058] In step 2010, it is determined whether there is any task to be executed in the current running scenario.

[0059] In step 2011, when there is a task to be executed in the current running scenario, the task to be executed is submitted to the thread pool.

[0060] In step 2012, the task type of the task to be executed is determined.

[0061] In step 2013, when the task to be executed is a user interface (UI) task, it is dispatched to the main thread of the application for execution.

[0062] In step 2014, when the task to be executed is an input / output (IO) task, it is dispatched to the child thread with the highest priority for execution.

[0063] In step 2015, when the task to be executed is a central processing unit (CPU) intensive task, if there is an idle sub-thread for processing CPU intensive tasks in the current running scenario, the detected central processing unit (CPU) intensive task is dispatched to the idle sub-thread for processing CPU intensive tasks for execution.

[0064] In step 2016, the lock function corresponding to the current running scenario is triggered, causing the target thread in the current running scenario to be blocked.

[0065] In step 2017, the thread holding the lock function in the current running scenario is checked.

[0066] In step 2018, the priority of the thread holding the lock function is raised to the highest priority.

[0067] Figure 3 is a block diagram illustrating a thread scheduling device according to an exemplary embodiment of the present disclosure.

[0068] Reference Figure 3 The device 300 may include a starting module 301 , a thread type determination module 302 and an adjustment module 303 .

[0069] The start module 301 can start the target application in response to the user's operation instruction. For example, the user can click the icon of the target application on the terminal with a finger to start the target application.

[0070] During the operation of the target application, the thread type determination module 302 can determine the thread type of each of the multiple threads corresponding to the target application in the current operation scene. The thread type may include a target thread and a non-target thread corresponding to the current operation scene. The target application may include multiple operation scenes, and each operation scene may be pre-registered with a corresponding target thread and a target task executed by the target thread.

[0071] For example, the target application may include three running scenarios that run in sequence, namely a theme interface running scenario, a data request running scenario, and a video playback running scenario.

[0072] In the theme interface running scenario, it is necessary to display the theme interface of the target application to the user. At this time, the user interface (UI) task is the main task, that is, the task of rendering the theme interface is the main task. The user interface (UI) task can be registered for the theme interface running scenario as the target task under the running scenario, and the main thread used to process the user interface (UI) task can be registered as the target thread under the running scenario.

[0073] In the request data running scenario, data needs to be pulled. At this time, the input and output (IO) task is the main task, that is, the task of sending and receiving data is the main task. The input and output (IO) task can be registered as the target task under the running scenario for the request data running scenario, and the sub-thread used to process the input and output (IO) task can be registered as the target thread under the running scenario.

[0074] In the video playback operation scenario, the pulled video frames need to be decoded. At this time, the central processing unit (CPU) intensive task is the main task, that is, the task of decoding the video frames is the main task. The task of decoding the video frames can be registered for the video playback operation scenario as the target task under the operation scenario, and the sub-thread used to decode the video frames can be registered as the target thread under the operation scenario.

[0075] The adjustment module 303 may adjust the priority of each thread based on the thread type of each thread in the current running scenario.

[0076] According to an exemplary embodiment of the present disclosure, when there is a target thread corresponding to the current running scene in multiple threads, the priority of the target thread can be adjusted to the highest priority among the priorities of the multiple threads. For example, when the current running scene is the theme interface running scene, the priority of the main thread for processing the user interface (UI) task corresponding to the theme interface running scene can be adjusted to the highest priority; when the current running scene is the request data running scene, the priority of the sub-thread for processing the input and output (IO) task corresponding to the request data running scene can be adjusted to the highest priority; when the current running scene is the play video running scene, the priority of the sub-thread for decoding the video frame corresponding to the play video running scene can be adjusted to the highest priority. In this way, by adjusting the priority of the target thread corresponding to the current running scene to the highest priority among the priorities of multiple threads, it can be ensured that the target task in the current running scene can be executed with high priority, that is, it can be ensured that the core task in the current running scene can be executed with priority, avoiding the situation where the target thread where the core task in the current running scene is located is preempted by other threads with higher priority and executed too slowly, and the program running effect is better.

[0077] According to an exemplary embodiment of the present disclosure, it should be noted that if there are non-target threads with the highest priority, the target thread no longer has a priority advantage over these non-target threads with the highest priority, which will result in a reduction in the time slices allocated to the target thread. Therefore, if the thread type of each thread in the current running scenario is a non-target thread, and the priority of each thread is at the highest priority, the adjustment module 303 can reduce the priority of the non-target thread with the highest priority in the current running scenario, ensuring that the target thread becomes the only thread with the highest priority, so that the non-target thread with the highest priority can be prevented from competing with the target thread for the time slice, and the target task on the target thread can be executed as quickly as possible.

[0078] According to an exemplary embodiment of the present disclosure, it should be noted that the priority of a thread may include four levels, namely background, low, default and high, and these four levels are gradually increased, that is, background is the lowest priority and high is the highest priority. The adjustment module 303 may reduce the priority of a non-target thread with the highest priority and in the current running scenario by one level according to a predetermined priority order, that is, the priority of a non-target thread with the highest priority high may be reduced by one level, that is, to default.

[0079] According to an exemplary embodiment of the present disclosure, the thread scheduling device 300 may further include a detection module, a task type determination module and a selection module. The detection module may detect whether there is a task to be executed in the current running scenario. In the case where there is a task to be executed in the current running scenario, the task type determination module may determine the task type of the task to be executed. Then, the selection module may select a thread from multiple threads to execute the task to be executed according to the task type of the task to be executed.

[0080] According to an exemplary embodiment of the present disclosure, the task type may include at least one of a user interface (UI) task, an input / output (IO) task, and a central processing unit (CPU) intensive task. The user interface (UI) task is a task for rendering a user interface, that is, the user interface (UI) task is a task for rendering a theme interface of an application; the input / output (IO) task is a task for sending and receiving data, and the central processing unit (CPU) intensive task is a task that requires the CPU to continuously perform calculations, for example, the central processing unit (CPU) intensive task may be a task for decoding video frames.

[0081] According to an exemplary embodiment of the present disclosure, the selection module is further configured to select a main thread among multiple threads to execute the UI task when the task type of the task to be executed is a UI task.

[0082] According to an exemplary embodiment of the present disclosure, the selection module is further configured to select a sub-thread with the highest priority from multiple threads to execute the input and output (IO) task when the task type of the task to be executed is an input and output (IO) task. It should be noted that since the input and output (IO) task does not occupy the time slice of the thread, it is not necessary to control the number of concurrent threads.

[0083] According to an exemplary embodiment of the present disclosure, when the task type of the task to be executed is a central processing unit (CPU) intensive task, the execution status of the central processing unit (CPU) intensive task is related to the number of CPU cores. For example, the number of CPU cores may be 4, but there are only 2 active cores. If there are 3 or 4 central processing unit (CPU) intensive tasks to be processed, which exceeds the number of active cores of the CPU, 2, then each central processing unit (CPU) intensive task will grab the time slice, and an active core of a CPU cannot always calculate a central processing unit (CPU) intensive task, resulting in each central processing unit (CPU) intensive task being unable to be fully executed.

[0084] In order to solve the above problems, the thread scheduling method disclosed in the present invention, when the task type of the task to be executed is a central processing unit (CPU) intensive task, the selection module can also detect whether there are spare sub-threads for processing CPU intensive tasks in multiple threads. If there are spare sub-threads for processing CPU intensive tasks in multiple threads, the spare sub-threads for processing CPU intensive tasks can be used to execute the CPU intensive tasks; if there are no spare sub-threads for processing CPU intensive tasks in multiple threads, if the newly detected CPU intensive tasks are forcibly executed at this time, the time slices of other tasks being executed will be deleted, resulting in the inability to fully execute other tasks being executed. Therefore, the tasks to be executed can be controlled to queue and wait until there are spare sub-threads for processing CPU intensive tasks in multiple threads to execute the tasks to be executed.

[0085] According to an exemplary embodiment of the present disclosure, the thread scheduling device 300 may also include a search module and an adjustment module. When the lock function corresponding to the current running scene is triggered, the search module can search for the thread holding the lock function from multiple threads. Next, the adjustment module can increase the priority of the thread holding the lock function to the highest priority. For example, when the current running scene is the video playing running scene, the sub-thread used to decode the video frame is the target thread, that is, the sub-thread decoding the video frame is in a high priority execution state at this time. If the lock function of the video playing running scene is triggered, the sub-thread used to decode the video frame will be paused (blocked) at this time, and the sub-thread decoding the video frame can continue to run after the thread holding the lock function of the video playing running scene releases the lock function. Therefore, the priority of the thread holding the lock function can be increased to the highest priority, so that the thread holding the lock function can be executed as quickly as possible, and the blocked target thread can be guaranteed to resume operation as soon as possible.

[0086] According to an exemplary embodiment of the present disclosure, a hook function is pre-added to the lock function corresponding to each of the multiple running scenarios. The hook function is used to indicate the thread holding the lock function among the multiple threads when the lock function is triggered. In this way, by pre-adding a hook function to the lock function corresponding to each of the multiple running scenarios, when the lock function corresponding to a certain running scenario is triggered, the hook function pre-added to the running scenario can be used to know which thread holds the lock function, and then the priority of the thread holding the lock function can be raised to the highest priority, so that the thread holding the lock function can be executed as quickly as possible. Even if the thread holding the lock function releases the lock function as quickly as possible, it can be ensured that the target thread blocked due to the triggering of the lock function resumes operation as soon as possible.

[0087] Figure 4 is a block diagram illustrating an electronic device 400 according to an exemplary embodiment of the present disclosure.

[0088] Reference Figure 4 The electronic device 400 includes at least one memory 401 and at least one processor 402. The at least one memory 401 stores instructions. When the instructions are executed by the at least one processor 402, the thread scheduling method according to the exemplary embodiment of the present disclosure is executed.

[0089] As an example, the electronic device 400 may be a PC, a tablet device, a personal digital assistant, a smart phone, or other device capable of executing the above instructions. Here, the electronic device 400 is not necessarily a single electronic device, but may also be any device or circuit capable of executing the above instructions (or instruction sets) individually or in combination. The electronic device 400 may also be part of an integrated control system or a system manager, or may be configured as a portable electronic device interconnected with a local or remote (e.g., via wireless transmission) interface.

[0090] In the electronic device 400, the processor 402 may include a central processing unit (CPU), a graphics processing unit (GPU), a programmable logic device, a dedicated processor system, a microcontroller or a microprocessor. As an example and not limitation, the processor may also include an analog processor, a digital processor, a microprocessor, a multi-core processor, a processor array, a network processor, etc.

[0091] The processor 402 may execute instructions or codes stored in the memory 401, which may also store data. Instructions and data may also be sent and received over a network via a network interface device, which may employ any known transmission protocol.

[0092] The memory 401 may be integrated with the processor 402, for example, by placing RAM or flash memory within an integrated circuit microprocessor or the like. In addition, the memory 401 may include a separate device, such as an external disk drive, a storage array, or any other storage device that can be used by a database system. The memory 401 and the processor 402 may be operatively coupled, or may communicate with each other, such as through an I / O port, a network connection, etc., so that the processor 402 can read files stored in the memory.

[0093] In addition, the electronic device 400 may also include a video display (such as a liquid crystal display) and a user interaction interface (such as a keyboard, a mouse, a touch input device, etc.) All components of the electronic device 400 may be connected to each other via a bus and / or a network.

[0094] According to an exemplary embodiment of the present disclosure, a computer-readable storage medium may also be provided, and when the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the thread scheduling method according to the present disclosure. Examples of computer-readable storage media here include: read-only memory (ROM), random access programmable read-only memory (PROM), electrically erasable programmable read-only memory (EEPROM), random access memory (RAM), dynamic random access memory (DRAM), static random access memory (SRAM), flash memory, non-volatile memory, CD-ROM, CD-R, CD+R, CD-RW, CD+RW, DVD-ROM, DVD-R, DVD+R, DVD-RW, DVD+RW, DVD-RAM, BD-ROM, BD-R, BD-R LTH, BD-RE, Blu-ray or optical disk storage, hard disk drive (HDD), solid state drive (SSD), card storage (such as, multimedia card, secure digital (SD) card or extreme digital (XD) card), magnetic tape, floppy disk, magneto-optical data storage device, optical data storage device, hard disk, solid state disk and any other device, any other device is configured to store computer programs and any associated data, data files and data structures in a non-transitory manner and provide the computer programs and any associated data, data files and data structures to a processor or computer so that the processor or computer can execute the computer program. The computer program in the above-mentioned computer-readable storage medium can be run in an environment deployed in a computer device such as a client, a host, an agent device, a server, etc. In addition, in one example, the computer program and any associated data, data files and data structures are distributed on a networked computer system, so that the computer program and any associated data, data files and data structures are stored, accessed and executed in a distributed manner by one or more processors or computers.

[0095] According to an exemplary embodiment of the present disclosure, a computer program product may also be provided, including a computer program, and when the computer program is executed by a processor, the thread scheduling method according to the present disclosure is implemented.

[0096] According to the thread scheduling method, device, electronic device and storage medium disclosed in the present invention, during the operation of the target application, the priority of each thread can be adjusted based on the thread type of each thread in the current operation scenario, so as to avoid the situation where the thread where the more important task is located is preempted by other threads with higher priority and executed too slowly, and the program operation effect is better. Further, by adjusting the priority of the target thread corresponding to the current operation scenario to the highest priority among the priorities of multiple threads, it can be ensured that the target task in the current operation scenario can be executed with high priority, that is, it can be ensured that the core task in the current operation scenario can be executed with priority, so as to avoid the situation where the target thread where the core task is located in the current operation scenario is preempted by other threads with higher priority and executed too slowly, and the program operation effect is better. Further, the priority of the non-target thread with the highest priority can be reduced, so that the target thread becomes the only thread with the highest priority, so as to avoid the non-target thread with the highest priority from snatching the time slice with the target thread, and it can be ensured that the target task on the target thread is executed as quickly as possible. Further, the priority of the thread holding the lock function can be raised to the highest priority, so that the thread holding the lock function is executed as quickly as possible, and it can be ensured that the blocked target thread resumes operation as soon as possible. Furthermore, by pre-adding a hook function to the lock function corresponding to each running scenario in multiple running scenarios, when the lock function corresponding to a certain running scenario is triggered, the hook function pre-added by the running scenario can be used to know which thread holds the lock function, and then the priority of the thread holding the lock function can be raised to the highest priority, so that the thread holding the lock function can be executed as quickly as possible. Even if the thread holding the lock function releases the lock function as quickly as possible, it can be ensured that the target thread that is blocked due to the triggering of the lock function resumes running as soon as possible.

[0097] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.

[0098] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims

1. A thread scheduling method, characterized in that: include: In response to a user's operation instruction, starting a target application; During the running process of the target application, determining the thread type of each thread in the multiple threads corresponding to the target application in the current running scenario; Based on the thread type of each thread in the current running scenario, the priority of each thread is adjusted, wherein the thread type includes a target thread and a non-target thread corresponding to the current running scenario, the target application includes a plurality of running scenarios, and each running scenario is pre-registered with a corresponding target thread and a target task executed by the target thread; Wherein, the thread scheduling method further includes: When the lock function corresponding to the current running scenario is triggered, searching the multiple threads for a thread holding the lock function; The priority of the thread holding the lock function is raised to the highest priority.

2. The thread scheduling method according to claim 1, characterized in that: The adjusting the priority of each thread based on the thread type of each thread in the current running scenario includes: If the thread type of each thread in the current running scenario is a non-target thread and the priority of each thread is at the highest priority, the priority of the non-target thread in the current running scenario with the highest priority is lowered.

3. The thread scheduling method according to claim 2, characterized in that: The step of lowering the priority of the non-target thread that has the highest priority and is in the current running scenario includes: According to a predetermined priority order, the priority of the non-target thread in the current running scenario which has the highest priority is reduced by one level.

4. The thread scheduling method according to claim 1, characterized in that: After the step of adjusting the priority of each thread based on the thread type of each thread in the current running scenario, the thread scheduling method further includes: Detect whether there is a task to be executed in the current running scenario; If there is a task to be executed in the current running scenario, determining the task type of the task to be executed; According to the task type of the task to be executed, a thread is selected from the multiple threads to execute the task to be executed.

5. The thread scheduling method according to claim 4, characterized in that: The selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed includes: In the case that the task type of the task to be executed is a user interface UI task, a main thread among the multiple threads is selected to execute the UI task.

6. The thread scheduling method according to claim 4, characterized in that: The selecting, according to the task type of the task to be executed, a thread from the multiple threads to execute the task to be executed comprises: When the task type of the task to be executed is an input / output (IO) task, a sub-thread with the highest priority is selected from the multiple threads to execute the IO task.

7. The thread scheduling method according to claim 4, characterized in that: The selecting a thread from the multiple threads to execute the task to be executed according to the task type of the task to be executed includes: In the case where the task type of the task to be executed is a central processing unit (CPU) intensive task, detecting whether there is a spare sub-thread for processing the CPU intensive task in the multiple threads; If there is a spare sub-thread for processing the CPU intensive task among the multiple threads, the CPU intensive task is executed by using the spare sub-thread for processing the CPU intensive task; Otherwise, the to-be-executed task is controlled to queue and wait until there is a spare sub-thread for processing CPU-intensive tasks among the multiple threads to execute the to-be-executed task.

8. A thread scheduling device, characterized in that: include: A starting module, configured to start a target application in response to an operation instruction of a user; A thread type determination module is configured to determine, during the running process of the target application, the thread type of each thread among the multiple threads corresponding to the target application in the current running scenario; an adjustment module configured to adjust the priority of each thread based on a thread type of each thread in a current running scenario, wherein the thread type includes a target thread and a non-target thread corresponding to the current running scenario, the target application includes a plurality of running scenarios, and each running scenario is pre-registered with a corresponding target thread and a target task executed by the target thread; Wherein, the thread scheduling device also includes: A search module, configured to search for a thread holding the lock function from the multiple threads when the lock function corresponding to the current running scenario is triggered; The raising module is configured to raise the priority of the thread holding the lock function to the highest priority.

9. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the thread scheduling method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the thread scheduling method according to any one of claims 1 to 7.

11. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the thread scheduling method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Task processing method and device and computer system

    CN111104210A