Thread scheduling method and apparatus, electronic device, and storage medium

CN122111579APending Publication Date: 2026-05-29GOERTEK INC

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
GOERTEK INC
Filing Date
2024-11-27
Publication Date
2026-05-29

Smart Images

  • Figure CN122111579A_ABST
    Figure CN122111579A_ABST
Patent Text Reader

Abstract

The application discloses a thread scheduling method and device, electronic equipment and storage medium, relates to the technical field of thread processing, and comprises the following steps: when a first thread of a target application needs to acquire a lock resource in a held state, and the thread type of the first thread is a key thread, inserting the first thread into a first waiting queue corresponding to the lock resource; if a second thread holding the lock resource is a non-key thread, inheriting a key attribute of the first thread to the second thread, updating the thread type of the second thread from the non-key thread to the key thread according to the key attribute inheritance result; calling the lock resource according to the second thread whose thread type is the key thread to execute a thread task corresponding to the second thread, and releasing the lock resource after the execution of the thread task, so that the key thread in the first waiting queue processes the task according to the acquisition of the lock resource. The application can reduce the waiting time of the important key thread and improve the response speed of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of thread processing technology, and in particular to thread scheduling methods, apparatus, electronic devices and storage media. Background Technology

[0002] With the increasing computing power and application complexity of smart devices, the Android system needs to manage numerous threads simultaneously to support the efficient operation of various applications and services. However, in the Android system, when multiple threads attempt to access the same resource or perform mutual exclusion operations, a locking mechanism is typically used to ensure data consistency and integrity. In this situation, critical threads may need to wait for other less important, non-critical threads to release locks, causing critical threads to remain in a waiting state for extended periods, resulting in response delays and impacting user experience. Therefore, reducing the waiting time of critical threads and improving system response speed has become an urgent problem to solve.

[0003] The above content is only used to help understand the technical solution of this application and does not represent an admission that the above content is prior art. Summary of the Invention

[0004] The main objective of this application is to provide a thread scheduling method, apparatus, electronic device, and storage medium, which aims to solve the technical problem of how to reduce the waiting time of important and critical threads and improve the system response speed.

[0005] To achieve the above objectives, this application proposes a thread scheduling method, which includes the following steps:

[0006] When the first thread of the target application is waiting to acquire a lock resource that is in a held state, check whether the thread type of the first thread is a critical thread;

[0007] If the first thread is a critical thread, then the first thread is inserted into the first waiting queue corresponding to the lock resource, wherein the priority of critical threads in the first waiting queue is higher than the priority of non-critical threads.

[0008] If the thread type of the second thread holding the lock resource is a non-critical thread, then the critical attributes of the first thread are inherited by the second thread, and the thread type of the second thread is updated from non-critical thread to critical thread according to the result of the critical attribute inheritance.

[0009] The second thread, whose thread type is critical thread, calls the lock resource to execute the thread task corresponding to the second thread, and releases the lock resource after the thread task is completed, so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

[0010] Optionally, the step of inheriting the key attributes of the first thread to the second thread, and updating the thread type of the second thread from a non-critical thread to a critical thread based on the key attribute inheritance result, includes:

[0011] Determine the key tag corresponding to the first thread, and map the key tag as a key attribute to obtain the mapped key attribute.

[0012] Assign a preset tag validity period to the mapping key attribute, and pass the mapping key attribute and the tag validity period to the second thread;

[0013] Based on the mapping key attributes and the tag validity period passed to the second thread, the thread type of the second thread is updated from non-critical thread to critical thread.

[0014] Optionally, the step of inserting the first thread into the first waiting queue corresponding to the lock resource includes:

[0015] Detect whether the first thread is a real-time thread;

[0016] If the first thread is a real-time thread, then the first thread is inserted at the head of the real-time thread queue in the first waiting queue.

[0017] Optionally, after the step of detecting whether the first thread is a real-time thread, the following steps are included:

[0018] If the first thread is a non-real-time thread, then check whether the first thread is a normal thread;

[0019] If the first thread is a normal thread, then the first thread is inserted at the head of the normal thread queue in the first waiting queue, wherein the priority of each real-time thread in the real-time thread queue is higher than the priority of each normal thread in the normal thread queue.

[0020] Optionally, the step of detecting whether the thread type of the first thread is a critical thread includes:

[0021] Detect whether the first thread is the UI thread or the audio processing thread;

[0022] If the first thread is a UI thread or an audio processing thread, then a preset key label is assigned to the first thread, and the thread type of the first thread is set as a key thread based on the key label.

[0023] Optionally, after the step of detecting whether the thread type of the first thread is a critical thread, the following steps are included:

[0024] If the first thread is a non-critical thread, then the first thread is inserted at the end of the first waiting queue and the first thread is blocked until the first thread in the first waiting queue is selected to acquire the lock resource, at which point the first thread is restored.

[0025] Optionally, after the step of inserting the first thread into the first waiting queue corresponding to the lock resource, the method includes:

[0026] The first thread is blocked until it is selected in the first waiting queue to acquire the lock resource, at which point the first thread is resumed.

[0027] Furthermore, to achieve the above objectives, this application also proposes a thread scheduling device, comprising:

[0028] The detection module is used to detect whether the thread type of the first thread in the target application is a critical thread when the first thread is waiting to acquire a lock resource that is in a held state.

[0029] An insertion module is used to insert the first thread into a first waiting queue corresponding to the lock resource if the thread type of the first thread is a critical thread, wherein the priority of critical threads in the first waiting queue is higher than the priority of non-critical threads.

[0030] An inheritance module is used to inherit the critical attributes of the first thread to the second thread if the thread type of the second thread holding the lock resource is a non-critical thread, and update the thread type of the second thread from non-critical thread to critical thread according to the key attribute inheritance result.

[0031] The release module is used to call the lock resource to execute the thread task corresponding to the second thread according to the thread type of the critical thread, and release the lock resource after the thread task is executed, so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

[0032] In addition, to achieve the above objectives, this application also proposes an electronic device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the thread scheduling method described above.

[0033] In addition, to achieve the above objectives, this application also proposes a storage medium, which is a computer-readable storage medium, on which a computer program is stored, and which, when executed by a processor, implements the steps of the thread scheduling method described above.

[0034] In this application, when the first thread of the target application is waiting to acquire a lock resource that is currently held, and the thread type of the first thread is a critical thread, the first thread is inserted into the first waiting queue corresponding to the lock resource. When the second thread holding the lock resource is a non-critical thread, the critical attributes of the first thread are inherited by the second thread, the thread type of the second thread is updated from non-critical thread to critical thread, and the second thread calls the lock resource to execute its corresponding thread task. After execution, the lock resource is released, allowing the critical thread in the first waiting queue to acquire the lock resource and perform its corresponding task processing. This avoids the situation where the second thread holding the lock resource is a non-critical thread, which would be at a disadvantage in resource contention such as scheduling, I / O, and memory, resulting in a longer lock resource holding time and thus a longer waiting time for the critical thread. By inheriting the critical attributes of the first thread to the second thread, the second thread possesses the characteristics of a critical thread, giving it an advantage in resource contention such as scheduling, I / O, and memory. This allows the second thread to quickly complete its thread task, release the lock resource, and thus allow the first thread to quickly acquire the lock resource, thereby reducing the waiting time of the critical thread and improving the system's response speed. Attached Figure Description

[0035] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0036] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0037] Figure 1 This is a flowchart illustrating the first embodiment of the thread scheduling method of this application;

[0038] Figure 2 This is a flowchart illustrating the second embodiment of the thread scheduling method of this application.

[0039] Figure 3 This is a flowchart illustrating the third embodiment of the thread scheduling method of this application;

[0040] Figure 4 This is a flowchart illustrating the fourth embodiment of the thread scheduling method of this application;

[0041] Figure 5 This is a complete flowchart of the thread scheduling method in this application;

[0042] Figure 6 This is a schematic diagram of the module architecture of the thread scheduling device in this application;

[0043] Figure 7 This is a schematic diagram of the device structure of the hardware operating environment involved in the thread scheduling method in the embodiments of this application.

[0044] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0045] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0046] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0047] In the Android system, when a large number of threads are concurrently competing for a lock, threads that need to wait are added to a waiting queue, which can be a futex waiting queue. The futex waiting queue sorts real-time threads according to their thread priority (0-99) and all ordinary threads according to priority (100), following a first-in, first-out (FIFO) principle. However, this approach can lead to excessively long waiting times for user experience-related threads, such as critical threads like the UI thread, resulting in significant latency. Therefore, in this embodiment, user experience-related threads can be designated as critical threads, and scheduled before ordinary threads and after real-time threads. Optionally, when a critical thread is a real-time thread, it can also be scheduled directly before real-time threads, giving critical threads higher priority in competition for resources such as scheduling, I / O, and memory, resulting in faster execution.

[0048] Optionally, when a critical thread is waiting for a lock held by a regular thread, the regular thread may run very slowly in the critical section due to the lack of corresponding resource allocation, causing priority inversion and preventing the critical thread from leveraging its advantages. Therefore, in this embodiment, a futex can be used to pass the critical attributes of the critical thread to the regular thread holding the lock, allowing the regular thread to inherit the critical attributes for a period of time. The regular thread with the critical attributes has higher priority in scheduling, I / O, and memory resource competition, resulting in faster thread execution speed. This reduces the original waiting time of the critical thread, minimizing its waiting latency.

[0049] This embodiment sets higher priority for important critical threads in the competition for resources such as scheduling, I / O, and memory by setting them, thereby reducing the waiting time of critical threads and improving the overall system response speed and user experience.

[0050] Optionally, a thread is a single sequential flow of control in the execution of an application. It is the smallest unit of execution flow in an application and the basic unit of processor scheduling and dispatching. Threads share the application's memory space (i.e., the memory space of the process they belong to). A standard thread consists of a thread ID, a program counter (PC), registers, and a stack.

[0051] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device or thread scheduling device capable of performing the above functions. The following description uses an electronic device as an example to illustrate this embodiment and the subsequent embodiments. Furthermore, the electronic device is equipped with an Android system, enabling the invocation of various threads.

[0052] Based on this, embodiments of this application provide a thread scheduling method, referring to... Figure 1 , Figure 1 This is a flowchart illustrating the first embodiment of the thread scheduling method of this application.

[0053] In this embodiment, the thread scheduling method includes steps S10 to S40.

[0054] Step S10: When the first thread of the target application is waiting to acquire a lock resource that is in a held state, check whether the thread type of the first thread is a critical thread.

[0055] Optionally, the target application can be any type of application that can run on an electronic device, such as a music application or game application installed on a mobile phone, or a music application installed on a head-mounted display device.

[0056] Optionally, the first thread can be a thread triggered by the target application at the current moment. Optionally, the first thread can be the main thread of the target application, or it can be a child thread of the target thread. For example, it can be the UI thread, the rendering thread, the audio processing thread, etc.

[0057] Optionally, lock resources can be important tools for controlling access to shared resources in multithreaded environments, and lock resources can include various different types of locks. Optionally, a lock is a synchronization object used to protect shared resources from simultaneous access by multiple threads. When one thread acquires the lock, other threads attempting to acquire the same lock will be blocked until the lock is released. Optionally, locks ensure that only one thread can access shared resources at a time, preventing data races and coordinating the execution order of multiple threads.

[0058] Optionally, the lock type can include a mutex lock, which is used to protect shared resources and ensure that only one thread can access the shared resource at a time. It can also include a read-write lock, which allows multiple read threads to access the shared resource simultaneously, but only one write thread is allowed to access it. And so on.

[0059] Optionally, critical threads can be threads related to user experience, which can be set in advance and configured according to different application environments. For example, the UI thread and audio processing thread can be set as critical threads.

[0060] Optionally, when the first thread of the target application prepares to acquire a lock resource (such as a read-write lock or a mutex), it can first check whether the lock is held. If so, it determines that the first thread needs to acquire the lock resource that is held. If the lock is idle, the first thread can directly acquire the lock resource, that is, acquire a read-write lock or a mutex, and according to the acquired lock resource, the first thread can retrieve data from the shared resource corresponding to the lock resource to execute the thread task corresponding to the first thread. Optionally, after the lock is acquired by any thread, the lock is in a held state; when the lock is not acquired by any thread, the lock is in an idle state.

[0061] Optionally, when the lock to be acquired by the first thread in the lock resource is in a held state, it is necessary to check whether the thread type of the first thread is a critical thread, and perform different operations according to different detection results.

[0062] Step S20: If the thread type of the first thread is a critical thread, then insert the first thread into the first waiting queue corresponding to the lock resource.

[0063] It should be noted that critical threads in the first waiting queue have a higher priority than non-critical threads.

[0064] Optionally, the first waiting queue can be a Futex waiting queue. The sorting rules in the first waiting queue can be set, modified, and adjusted according to user needs. Optionally, the sorting rules in the first waiting queue can be based on the priority of critical threads being higher than that of non-critical threads, the priority of real-time threads being higher than that of non-real-time threads, or the first-in-first-out (FIFO) principle, etc. That is, in the first waiting queue, the priority of real-time threads can be 0-99, and the priority of ordinary threads can be 100. Real-time threads are sorted according to their corresponding priorities, and ordinary threads can be sorted according to the FIFO principle.

[0065] Optionally, critical threads may include ordinary threads and real-time threads that are relevant to user experience. Non-critical threads may include ordinary threads and real-time threads that are not relevant to user experience.

[0066] Optionally, if the first thread is determined to be a critical thread, it can be inserted before all ordinary threads in the first waiting queue and after the real-time threads.

[0067] Optionally, when the first thread is a critical thread, and that critical thread is a normal thread related to user experience, the first thread can be inserted before all normal threads in the first waiting queue and after the real-time threads. That is, the first thread has a higher priority than any other normal thread in the first waiting queue, but lower than any real-time thread in the first waiting queue. Optionally, when the first thread is a critical thread, and that critical thread is a real-time thread related to user experience, the first thread can be inserted among all real-time threads.

[0068] Step S30: If the thread type of the second thread holding the lock resource is a non-critical thread, then the critical attributes of the first thread are inherited to the second thread, and the thread type of the second thread is updated from non-critical thread to critical thread according to the result of the critical attribute inheritance.

[0069] Optionally, the second thread can be the thread that is currently invoking the lock and owns the lock resource.

[0070] Optionally, the system can detect the second thread holding the lock resource and determine whether its thread type is a critical thread. If the second thread is a critical thread, it can wait for the second thread to acquire the shared resource data based on the lock resource to execute the corresponding thread task. After the thread task is completed, the lock resource is released. Then, in the first waiting queue, threads in the first waiting queue are called in descending order of priority. For example, if the highest priority thread in the first waiting queue is the first thread with the critical thread type, then the first thread can be called to acquire the lock resource and acquire the shared resource data based on the lock resource to execute the thread task related to the first thread.

[0071] Optionally, if the second thread is a non-critical thread and a regular thread unrelated to user experience, it can be determined that the second thread is at a disadvantage in competing for resources such as scheduling, I / O, and memory. This results in a longer time required for the first thread to acquire lock resources, meaning a very large response latency. Therefore, the critical attributes of the first thread can be inherited by the second thread, updating the second thread's thread type from non-critical to critical. In this case, the second thread can enjoy the treatment of a critical thread, meaning it has higher priority in competing for resources such as scheduling, I / O, and memory.

[0072] Step S40: The second thread, whose thread type is critical thread, calls the lock resource to execute the thread task corresponding to the second thread, and releases the lock resource after the thread task is completed so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

[0073] Optionally, after the thread type of the second thread changes to a critical thread, the second thread can invoke the lock resource to obtain relevant data from the shared resource to execute the thread task corresponding to the second thread. After the thread task is completed, the lock resource can be released, updating the lock resource's state from held to idle. At this time, threads in the first waiting queue can be invoked in descending order of priority to acquire the idle lock resource. For example, if the thread with the highest priority in the first waiting queue is the first thread with the critical thread type, then the first thread can be invoked to acquire the lock resource, and the data from the shared resource obtained from the lock resource can be used to execute the thread task related to the first thread.

[0074] Optionally, after the second thread releases the lock resource, the thread type of the second thread will change from critical thread to non-critical thread. That is, the critical attributes inherited by the first thread from the second thread will become invalid, and the second thread will restore its original thread type.

[0075] In this embodiment, when the first thread of the target application is waiting to acquire a lock resource that is currently held, and the thread type of the first thread is a critical thread, the first thread is inserted into the first waiting queue corresponding to the lock resource. When the second thread holding the lock resource is a non-critical thread, the critical attributes of the first thread are inherited by the second thread, the thread type of the second thread is updated from non-critical to critical, and the second thread calls the lock resource to execute its corresponding thread task. After execution, the lock resource is released, allowing the critical thread in the first waiting queue to acquire the lock resource and perform its corresponding task processing. This avoids the situation where the second thread holding the lock resource is a non-critical thread, which would put it at a disadvantage in resource contention such as scheduling, I / O, and memory, resulting in a longer lock resource holding time and thus a longer waiting time for the critical thread. By inheriting the critical attributes of the first thread to the second thread, the second thread can possess the characteristics of a critical thread, giving it an advantage in resource contention such as scheduling, I / O, and memory. This allows the second thread to quickly complete its thread task, release the lock resource, and thus allow the first thread to quickly acquire the lock resource, reducing the waiting time of the critical thread and improving the system's response speed.

[0076] Based on the first embodiment of this application, a second embodiment of this application is proposed. In this second embodiment, content that is the same as or similar to the above embodiment can be referred to the above description, and will not be repeated hereafter. Based on this, refer to... Figure 2 In step S30, the key attributes of the first thread are inherited to the second thread, and the thread type of the second thread is updated from non-key thread to key thread based on the key attribute inheritance result, including steps a10-a30.

[0077] Step a10: Determine the key tag corresponding to the first thread, and map the key tag as a key attribute to obtain the mapped key attribute;

[0078] Optionally, the key tag is an identifier assigned by the system in the electronic device to the key thread, which can be represented in the form of numbers, arrays, text, etc.

[0079] Optionally, the condition for performing critical attribute inheritance can be that one thread is a critical thread and the other thread is a non-critical thread.

[0080] Optionally, a key label can be pre-assigned to the first thread, and the key label can be mapped as a key attribute to obtain at least one label that is the same as the key label, and this label can be used as the mapping key attribute.

[0081] Step a20: Assign a preset tag validity period to the mapping key attribute, and pass the mapping key attribute and tag validity period to the second thread;

[0082] Optionally, a tag validity period can be assigned to the mapping key attribute so that the second thread that inherits the mapping key attribute can only use the mapping key attribute during the tag validity period, and cannot use the mapping key attribute after the tag validity period has expired.

[0083] Optionally, the tag's validity period can be a pre-set fixed time range, such as ten minutes. Alternatively, it can be a rule-based time, such as using the time when the second thread inherits the key attribute from the first thread as the start time of the tag's validity period and the time when the second thread releases the lock resource as the end time. Other rules can also be used to set the tag's validity period; there are no restrictions here.

[0084] Optionally, the mapping key attributes and label validity periods can be passed to a second thread via a first waiting queue, such as a futex queue.

[0085] Step a30: Based on the mapping key attributes and label validity period passed to the second thread, update the thread type of the second thread from non-critical thread to critical thread.

[0086] Optionally, after the second thread receives the transmitted mapping key attribute and tag validity period, it can use the mapping key attribute as its own thread attribute, update the thread type of the second thread from non-critical thread to critical thread, and automatically delete the mapping key attribute in the second thread after the tag validity period is reached, and update the thread type of the second thread from critical thread to non-critical thread.

[0087] In this embodiment, by determining the key tag corresponding to the first thread, the key tag is mapped as a key attribute to obtain the mapped key attribute. A valid time for the corresponding tag is assigned, and the mapped key attribute and tag valid time are passed to the second thread to update the thread type of the second thread from non-critical thread to critical thread. This allows the second thread to possess the characteristics of a critical thread, giving it an advantage in resource contention such as scheduling, I / O, and memory. The second thread can quickly complete its thread tasks, release lock resources, and thus allow the first thread to quickly acquire lock resources. This reduces the waiting time of the critical thread and improves the system's response speed.

[0088] Based on the first or second embodiment of this application, a third embodiment of this application is proposed. In this third embodiment, content that is the same as or similar to the above embodiments can be referred to the above description, and will not be repeated hereafter. Based on this, refer to... Figure 3 In step S20, the step of inserting the first thread into the first waiting queue corresponding to the lock resource includes steps b10-b20.

[0089] Step b10: Check if the first thread is a real-time thread;

[0090] Step b20: If the first thread is a real-time thread, then insert the first thread at the head of the real-time thread queue in the first waiting queue.

[0091] Optionally, a real-time thread can be an RT thread, i.e., a thread created by the RT-Thread kernel. Optionally, a real-time thread has real-time requirements, ensuring timely response to tasks.

[0092] Optionally, if the first thread is a critical thread, it is possible to continue to detect whether the first thread is a real-time thread;

[0093] If the first thread is a real-time thread, then the real-time thread queue and the normal thread queue in the first waiting queue can be determined. The first thread is inserted at the head of the real-time thread queue. This ensures that after the second thread releases the lock resource, it can immediately call the first thread in the first waiting queue to acquire the lock resource, and then retrieve the data from the shared resource based on the acquired lock resource to process the thread task.

[0094] Optionally, the priority of the real-time thread queue in the first waiting queue is higher than that of the ordinary thread queue. The priority of each real-time thread in the real-time thread queue can be 0-99, while the priority of all threads in the ordinary thread queue is 100. The real-time threads in the real-time thread queue can be sorted according to their respective priority levels. The ordinary threads in the ordinary thread queue can be sorted in a first-in, first-out (FIFO) order, that is, according to the order in which they were written to the first waiting queue.

[0095] In this embodiment, when the first thread is a real-time thread, it is inserted at the head of the real-time thread queue in the first waiting queue. This allows the first thread in the first waiting queue to acquire the lock resource immediately after the second thread releases the lock resource, thereby reducing the waiting time of the first thread (i.e., the critical thread) and improving the system's response speed.

[0096] Further, in one feasible embodiment, after step b10, the step of detecting whether the first thread is a real-time thread, the method includes:

[0097] Step b30: If the first thread is a non-real-time thread, then check whether the first thread is a normal thread;

[0098] Step b40: If the first thread is a normal thread, then insert the first thread at the head of the normal thread queue in the first waiting queue.

[0099] It should be noted that the priority of each real-time thread in the real-time thread queue is higher than the priority of each ordinary thread in the ordinary thread queue.

[0100] Alternatively, a regular thread can be a thread created by the application and has no real-time requirements.

[0101] Optionally, after detecting that the first thread is a non-real-time thread (i.e., not a real-time thread), the detection of the first thread continues to check whether the first thread is a normal thread.

[0102] Optionally, if the first thread is a normal thread, it can be inserted into the normal thread queue in the first waiting queue. Since the first thread is a critical thread, it can be inserted at the beginning of the normal thread queue. That is, in the first waiting queue, the real-time thread queue is sorted first, then the first thread is sorted, and finally the normal thread queue is sorted.

[0103] Optionally, after the second thread releases the lock resource, and all real-time threads in the real-time thread queue of the first sorting queue have acquired and released the lock resource, the first thread in the first sorting queue is then called to acquire the lock resource again. The acquired lock resource is then used to retrieve data from the shared resource for thread task processing. After the first thread releases the lock resource, ordinary threads in the ordinary thread queue acquire the lock resource sequentially in a first-in-first-out order to process the corresponding thread tasks.

[0104] In this embodiment, by inserting the first thread into the first queue of ordinary threads in the first waiting queue when the first thread is an ordinary thread, it is possible to promptly call the first thread to acquire the lock resource after the second thread releases the lock resource and all real-time threads in the second thread queue have acquired and released the lock resource. This avoids the phenomenon of waiting for all threads in the first waiting queue to acquire and release the lock resource before the first thread can acquire the lock resource, thereby reducing the waiting time of the first thread (i.e., the critical thread) and improving the system's response speed.

[0105] Based on the first, second, or third embodiment of this application, a fourth embodiment of this application is proposed. In this fourth embodiment, content that is the same as or similar to the above embodiments can be referred to the above description, and will not be repeated hereafter. Based on this, refer to... Figure 4 In step S10, the step of detecting whether the thread type of the first thread is a critical thread includes steps c10-c20.

[0106] c10, detects whether the first thread is the UI thread or the audio processing thread;

[0107] c20 If the first thread is a UI thread or an audio processing thread, then assign a preset key label to the first thread and set the thread type of the first thread as a key thread based on the key label.

[0108] Optionally, you can pre-define which threads are critical threads, that is, which threads are related to user experience. For example, critical threads include the UI thread and the audio processing thread.

[0109] Optionally, it is possible to directly detect whether the first thread is a UI thread or an audio processing thread. If the first thread is a UI thread or an audio processing thread, a pre-set key label can be assigned to it. The key label can refer to step a10 in the above embodiment.

[0110] Optionally, once the first thread has a key tag, its thread type can be automatically set to a key thread based on that key tag. That is, if the first thread is a UI thread or an audio processing thread, its thread type can be directly determined as a key thread.

[0111] Optionally, in a scenario, the first thread can be detected to determine whether it is a normal thread or a real-time thread related to user experience. If so, a key label can be assigned to the first thread, and its thread type can be determined as a key thread.

[0112] In this embodiment, by assigning a key label to the first thread when it is a UI thread or an audio processing thread, and setting the thread type of the first thread as a key thread, the validity of the determined key thread is ensured.

[0113] Furthermore, in one feasible embodiment, step d10 is included after step S10, which detects whether the thread type of the first thread is a critical thread.

[0114] Step d10: If the first thread is a non-critical thread, insert the first thread into the last position of the first waiting queue and block the first thread until the first thread in the first waiting queue is selected to acquire the lock resource, then resume the thread operation.

[0115] Optionally, when the thread type of the first thread is detected to be a non-critical thread, it is determined whether the thread type of the first thread is a normal thread that is irrelevant to user experience or a real-time thread that is irrelevant to user experience.

[0116] If the first thread is a real-time thread unrelated to user experience, its priority is determined according to a preset priority configuration rule. The first thread is then inserted into the real-time thread queue within the first waiting queue based on its configured priority, resulting in an updated real-time thread queue. The real-time threads in the updated queue are then sorted according to their priority. The priority configuration rule can be based on existing rules or on rules set during extraction.

[0117] Optionally, if the first thread is a normal thread unrelated to user experience, it is inserted at the end of the first waiting queue, that is, the last thread in the normal thread queue of the first waiting queue. The first thread is blocked to prevent it from continuing to work. When the first thread in the first waiting queue is called, it is determined that the first thread is selected to acquire the lock resource. At this point, thread resumption can be performed on the first thread. After the first thread resumes, it acquires the lock resource and uses the lock resource to obtain the shared resource data to execute the thread tasks related to the first thread.

[0118] In this embodiment, when the first thread is a non-critical thread, it is inserted at the end of the first waiting queue to block the first thread until it is selected to acquire the lock resource. Then, the first thread is resumed, thus avoiding the phenomenon of the first thread continuing to execute without acquiring the lock resource, which would otherwise lead to resource waste.

[0119] Furthermore, in one feasible embodiment, step S20, after the step of inserting the first thread into the first waiting queue corresponding to the lock resource, includes step e10.

[0120] Step e10: Block the first thread until it is selected in the first waiting queue to acquire the lock resource, then resume the first thread.

[0121] Optionally, after inserting the first thread at the head of the ordinary thread queue in the first waiting queue, or at the head of the real-time thread queue, the first thread can be blocked to prevent it from continuing to work. When the first thread in the first waiting queue is called, it is determined that the first thread is selected to acquire the lock resource. At this time, the first thread can be resumed. After the first thread resumes, it can acquire the lock resource and execute the thread task related to the first thread based on the data of the shared resource obtained from the lock resource.

[0122] In this embodiment, after inserting the first thread into the first waiting queue, the first thread is blocked until it is selected to acquire the lock resource. Then, the first thread is resumed, thus avoiding the phenomenon of the first thread continuing to execute without acquiring the lock resource, which would otherwise lead to resource waste.

[0123] In addition, to aid in understanding the overall process of thread scheduling in this embodiment, examples are provided below.

[0124] For example, such as Figure 5As shown, when the first thread of the target application (hereinafter referred to as thread) calls the lock resource, it checks whether the lock resource is held (i.e., in a held state). If not, the calling thread acquires the lock resource, and the first thread continues to execute its corresponding thread task. If so, it determines whether the thread is a critical thread. If not, the thread is placed in the ordinary thread queue of the waiting queue, i.e., the first thread is placed in the ordinary thread queue of the first waiting queue. Thread blocking is then applied to the first thread. If the thread is a critical thread, it determines whether the critical thread is a RT thread or an ordinary thread. If it is a critical thread, the thread is inserted into the RT queue with a priority of 0-99. If it is an ordinary thread, the thread is inserted after the RT queue and before the ordinary threads, i.e., the first thread is inserted at the head of the ordinary thread queue. The lock holder is marked as a critical thread, meaning that the second thread holding the lock resource inherits the critical attribute from the first thread, updating the thread type of the second thread from non-critical to critical. The first thread is blocked, and after the lock holder finishes its work and releases the lock, a key thread is selected from the waiting queue to acquire the lock and then the thread continues to execute its corresponding thread task.

[0125] Furthermore, embodiments of this application provide a thread scheduling device, referring to... Figure 6 The thread scheduling device includes:

[0126] The detection module A10 is used to detect whether the thread type of the first thread in the target application is a critical thread when the first thread is waiting to acquire a lock resource that is in a held state.

[0127] The insertion module A20 is used to insert the first thread into the first waiting queue corresponding to the lock resource if the thread type of the first thread is a critical thread, wherein the priority of critical threads in the first waiting queue is higher than the priority of non-critical threads.

[0128] Inheritance module A30 is used to inherit the critical attributes of the first thread to the second thread if the thread type of the second thread holding the lock resource is a non-critical thread, and update the thread type of the second thread from non-critical thread to critical thread according to the key attribute inheritance result.

[0129] The release module A40 is used to execute the thread task corresponding to the second thread based on the thread type of the critical thread, and release the lock resource after the thread task is completed so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

[0130] The thread scheduling device provided in this application, employing the thread scheduling method in the above embodiments, can solve the technical problem of how to reduce the waiting time of important critical threads and improve the system's response speed. Compared with the prior art, the beneficial effects of the thread scheduling device provided in this application are the same as those of the thread scheduling method provided in the above embodiments, and other technical features in the thread scheduling device are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0131] This application provides an electronic device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform the thread scheduling method in Embodiment 1 above.

[0132] The following is for reference. Figure 7 The figure illustrates a structural diagram of an electronic device suitable for implementing embodiments of this application. The electronic devices in the embodiments of this application may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. The devices shown in the figure are merely examples and should not be construed as limiting the functionality and scope of use of the embodiments of this application.

[0133] The electronic device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.) that can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM) 1004. The RAM 1004 also stores various programs and data required for device operation. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices 1008 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 1003 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1009. Communication device 1009 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. While electronic devices with various systems are shown in the figures, it should be understood that implementation or possession of all the systems shown is not required. More or fewer systems may be implemented alternatively.

[0134] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0135] The electronic device provided in this application, employing the thread scheduling method described in the above embodiments, can solve the technical problem of how to reduce the waiting time of important critical threads and improve the system's response speed. Compared with the prior art, the beneficial effects of the electronic device provided in this application are the same as those of the thread scheduling method provided in the above embodiments, and other technical features of this electronic device are the same as those disclosed in the previous embodiment method, and will not be repeated here.

[0136] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.

[0137] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0138] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the thread scheduling method in the above embodiments.

[0139] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, system, or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.

[0140] The aforementioned computer-readable storage medium may be included in an electronic device or may exist independently without being assembled into an electronic device.

[0141] The aforementioned computer-readable storage medium carries one or more programs, which, when executed by an electronic device, enable the electronic device to perform the steps in the aforementioned thread scheduling method.

[0142] Computer program code for performing the operations of this application can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, and C++, and conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0143] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0144] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.

[0145] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the above-described thread scheduling method. This solves the technical problem of reducing the waiting time of critical threads and improving system response speed. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the thread scheduling method provided in the above embodiments, and will not be repeated here.

[0146] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the thread scheduling method described above.

[0147] The computer program product provided in this application can solve the technical problem of how to reduce the waiting time of important critical threads and improve the system response speed. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the thread scheduling method provided in the above embodiments, and will not be repeated here.

[0148] The above are only some embodiments of this application and do not limit the patent scope of this application. All equivalent structural transformations made under the technical concept of this application and using the contents of the specification and drawings of this application, or direct / indirect applications in other related technical fields, are included in the patent protection scope of this application.

Claims

1. A thread scheduling method, characterized in that, The thread scheduling method includes the following steps: When the first thread of the target application is waiting to acquire a lock resource that is in a held state, check whether the thread type of the first thread is a critical thread; If the first thread is a critical thread, then the first thread is inserted into the first waiting queue corresponding to the lock resource, wherein the priority of critical threads in the first waiting queue is higher than the priority of non-critical threads. If the thread type of the second thread holding the lock resource is a non-critical thread, then the critical attributes of the first thread are inherited by the second thread, and the thread type of the second thread is updated from non-critical thread to critical thread according to the result of the critical attribute inheritance. The second thread, whose thread type is critical thread, calls the lock resource to execute the thread task corresponding to the second thread, and releases the lock resource after the thread task is completed, so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

2. The thread scheduling method as described in claim 1, characterized in that, The step of inheriting the key attributes of the first thread to the second thread, and updating the thread type of the second thread from non-critical thread to critical thread based on the key attribute inheritance result, includes: Determine the key tag corresponding to the first thread, and map the key tag as a key attribute to obtain the mapped key attribute. Assign a preset tag validity period to the mapping key attribute, and pass the mapping key attribute and the tag validity period to the second thread; Based on the mapping key attributes and the tag validity period passed to the second thread, the thread type of the second thread is updated from non-critical thread to critical thread.

3. The thread scheduling method as described in claim 1, characterized in that, The step of inserting the first thread into the first waiting queue corresponding to the lock resource includes: Detect whether the first thread is a real-time thread; If the first thread is a real-time thread, then the first thread is inserted at the head of the real-time thread queue in the first waiting queue.

4. The thread scheduling method as described in claim 3, characterized in that, After the step of detecting whether the first thread is a real-time thread, the following is included: If the first thread is a non-real-time thread, then check whether the first thread is a normal thread; If the first thread is a normal thread, then the first thread is inserted at the head of the normal thread queue in the first waiting queue, wherein the priority of each real-time thread in the real-time thread queue is higher than the priority of each normal thread in the normal thread queue.

5. The thread scheduling method as described in claim 1, characterized in that, The step of detecting whether the thread type of the first thread is a critical thread includes: Detect whether the first thread is the UI thread or the audio processing thread; If the first thread is a UI thread or an audio processing thread, then a preset key label is assigned to the first thread, and the thread type of the first thread is set as a key thread based on the key label.

6. The thread scheduling method according to any one of claims 1-5, characterized in that, After the step of detecting whether the thread type of the first thread is a critical thread, the following steps are included: If the first thread is a non-critical thread, then the first thread is inserted at the end of the first waiting queue and the first thread is blocked until the first thread in the first waiting queue is selected to acquire the lock resource, at which point the first thread is restored.

7. The thread scheduling method according to any one of claims 1-5, characterized in that, Following the step of inserting the first thread into the first waiting queue corresponding to the lock resource, the steps include: The first thread is blocked until it is selected in the first waiting queue to acquire the lock resource, at which point the first thread is resumed.

8. A thread scheduling device, characterized in that, The thread scheduling device includes: The detection module is used to detect whether the thread type of the first thread in the target application is a critical thread when the first thread is waiting to acquire a lock resource that is in a held state. An insertion module is used to insert the first thread into a first waiting queue corresponding to the lock resource if the thread type of the first thread is a critical thread, wherein the priority of critical threads in the first waiting queue is higher than the priority of non-critical threads. An inheritance module is used to inherit the critical attributes of the first thread to the second thread if the thread type of the second thread holding the lock resource is a non-critical thread, and update the thread type of the second thread from non-critical thread to critical thread according to the key attribute inheritance result. The release module is used to call the lock resource to execute the thread task corresponding to the second thread according to the thread type of the critical thread, and release the lock resource after the thread task is executed, so that the critical thread in the first waiting queue can acquire the lock resource to process the task.

9. An electronic device, characterized in that, The electronic device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the thread scheduling method as described in any one of claims 1 to 7.

10. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, it implements the steps of the thread scheduling method as described in any one of claims 1 to 7.