Task scheduling method and device and storage medium
By using a scheduling method that combines task group skip lists and time wheels in the smart home system, the performance bottleneck and task loss problems under concurrent delayed tasks are solved, achieving efficient and reliable task scheduling and state management.
Patent Information
- Application Number
- CN202511368847.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-23
- Publication Date
- 2026-01-09
AI Technical Summary
Existing smart home systems suffer from performance bottlenecks when faced with a large number of concurrent delayed tasks, resulting in high memory consumption, low scheduling efficiency, and the potential loss of delayed tasks when the cloud platform is abnormally interrupted, affecting user experience and system reliability.
Based on the user's input pattern requirements, task groups are identified and stored in different skip lists and time wheels. The time wheels are used to schedule task groups in a unified manner, avoiding the creation of separate threads. The skip lists are combined with task status management to achieve separation and coordinated control of task scheduling and status management.
It improves the efficiency of task scheduling and system responsiveness, avoids task loss, realizes flexible and fine-grained task scheduling, and saves thread resources.
Smart Images

Figure CN121300932A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of task processing technology, and more specifically, to a task scheduling method, device, and storage medium. Background Technology
[0002] With the continuous development of IoT technology, smart home systems are gradually becoming more widespread and deeply integrated into people's daily lives. Users can remotely control home devices through smart terminals and achieve interconnected operations between devices by setting scenes or automated tasks. Among these, delayed execution, as a common task control logic, is widely used in various smart home scenarios such as lighting control, security arming, and environmental adjustment. How to efficiently and stably implement delayed task scheduling has become an important issue in improving the user experience and system performance of smart home systems.
[0003] In current smart home cloud platforms, delayed tasks are typically implemented using thread sleep. When a user configures a delayed task, the system creates an independent thread and calls a delay function (such as sleep or a timer) to wait for a specified time before executing subsequent operations. Multiple delayed tasks are managed through a thread pool to reduce excessive consumption of system resources.
[0004] However, traditional thread delay mechanisms suffer from significant performance bottlenecks when faced with a large number of concurrent delayed tasks. On the one hand, each delayed task requires independent thread resources, leading to a surge in the number of system threads and resulting in problems such as high memory consumption and low scheduling efficiency. On the other hand, if the cloud platform experiences a service restart or abnormal interruption, the executing thread delay tasks will be interrupted and cannot be recovered, resulting in task loss and affecting the integrity and reliability of user scenario execution. Summary of the Invention
[0005] The purpose of this application is to provide a task scheduling method, device, and storage medium to address the shortcomings of the prior art and solve the problem of significant performance bottlenecks in the prior art.
[0006] To achieve the above objectives, the technical solutions adopted in the embodiments of this application are as follows:
[0007] In a first aspect, one embodiment of this application provides a task scheduling method, the method comprising:
[0008] Based on the user's input pattern requirements, multiple task groups and the expected execution time of each task group are determined. Each task group includes at least one task to be executed, and the first task to be executed in at least one task group is a delayed task.
[0009] Traverse each of the task groups. For the current task group, determine the waiting time of the current task group based on the expected execution time of the current task group. Store the current task group in a target storage object based on the waiting time of the current task group. The target storage object includes any one of the following: a first skip list, a second skip list, a first skip list and a time wheel, wherein the waiting time of each task group in the first skip list is less than the waiting time of each task group in the second skip list.
[0010] Run the time wheel, and when the time wheel is running, execute the task groups in the time wheel.
[0011] In one possible implementation, determining multiple task groups and the expected execution time of each task group based on the user-input pattern requirements includes:
[0012] Based on the mode requirements, a plurality of tasks to be executed are determined in sequence. The plurality of tasks to be executed are arranged in sequence according to their execution time, and the execution time of the tasks is determined based on the mode requirements.
[0013] Based on the task type of each of the tasks to be executed, at least one delayed task is determined from the plurality of tasks to be executed;
[0014] The plurality of task groups are determined according to the arrangement of each of the delayed tasks;
[0015] The expected execution time of each task group is determined based on the execution time of at least one task in each task group.
[0016] In one possible implementation, determining the plurality of task groups based on the arrangement of the delayed tasks includes:
[0017] The current delayed task and the target tasks to be executed between the current delayed task and the next delayed task of the current delayed task, as well as the current delayed task, are regarded as a task group, wherein the current delayed task and each of the target tasks to be executed are arranged in the task group according to the execution time of the tasks.
[0018] In one possible implementation, storing the current task group to a target storage object based on the waiting time of the current task group includes:
[0019] If the waiting time of the current task group is less than the first threshold, and there is no task group in the first skip list, then the target storage object is determined to be the first skip list and the time wheel, and the current task group is stored in the first skip list and the time wheel.
[0020] If the waiting time of the current task group is less than the first threshold, and there is a task group in the first skip list, then the target storage object is determined to be the first skip list, and the task group that already exists in the first skip list is stored in the time wheel, and the current task group is stored in the first skip list;
[0021] If the waiting time of the current task group is greater than the first threshold, then the target storage object is determined to be the second skip list, and the current task group is stored in the second skip list.
[0022] One possible implementation also includes:
[0023] If there is no task group to be executed in the first skip list, then the first task group to be transferred is determined according to the waiting time of each task group in the second skip list;
[0024] The first task group to be transferred is stored in the first skip list and the time wheel, and the first task group to be transferred is deleted from the second skip list.
[0025] In one possible implementation, determining the first task group to be transferred based on the waiting time of each task group in the second skip list includes:
[0026] Traverse each task group in the second skip list. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then determine the current task group as the first task group to be transferred.
[0027] One possible implementation also includes:
[0028] After the time wheel finishes executing the task group in the time wheel, the completed task group is deleted from the first skip list.
[0029] One possible implementation also includes:
[0030] When the timer program is running, it determines the second task group to be transferred in the second skip list, stores the second task group to be transferred in the first skip list and the time wheel, and deletes the second task group to be transferred from the second skip list.
[0031] Secondly, another embodiment of this application provides a task scheduling apparatus, the apparatus comprising:
[0032] The first determining module is used to determine multiple task groups and the expected execution time of each task group according to the pattern requirements input by the user. Each task group includes at least one task to be executed, and the first task to be executed in at least one task group is a delayed task.
[0033] The second determining module is used to traverse each of the task groups, and for the current task group that has been traversed, to determine the waiting time of the current task group based on the expected execution time of the current task group, and to store the current task group in a target storage object based on the waiting time of the current task group. The target storage object includes any one of the following: a first skip list, a second skip list, a first skip list and a time wheel, wherein the waiting time of each task group in the first skip list is less than the waiting time of each task group in the second skip list.
[0034] An execution module is used to run the time wheel, which executes the task groups within the time wheel when it is running.
[0035] In one possible implementation, the first determining module is specifically used for:
[0036] Based on the mode requirements, a plurality of tasks to be executed are determined in sequence. The plurality of tasks to be executed are arranged in sequence according to their execution time, and the execution time of the tasks is determined based on the mode requirements.
[0037] Based on the task type of each of the tasks to be executed, at least one delayed task is determined from the plurality of tasks to be executed;
[0038] The plurality of task groups are determined according to the arrangement of each of the delayed tasks;
[0039] The expected execution time of each task group is determined based on the execution time of at least one task in each task group.
[0040] In one possible implementation, the first determining module is specifically used for:
[0041] The current delayed task and the target tasks to be executed between the current delayed task and the next delayed task of the current delayed task, as well as the current delayed task, are regarded as a task group, wherein the current delayed task and each of the target tasks to be executed are arranged in the task group according to the execution time of the tasks.
[0042] In one possible implementation, the second determining module is specifically used for:
[0043] If the waiting time of the current task group is less than the first threshold, and there is no task group in the first skip list, then the target storage object is determined to be the first skip list and the time wheel, and the current task group is stored in the first skip list and the time wheel.
[0044] If the waiting time of the current task group is less than the first threshold, and there is a task group in the first skip list, then the target storage object is determined to be the first skip list, and the task group that already exists in the first skip list is stored in the time wheel, and the current task group is stored in the first skip list;
[0045] If the waiting time of the current task group is greater than the first threshold, then the target storage object is determined to be the second skip list, and the current task group is stored in the second skip list.
[0046] In one possible implementation, it also includes: a transfer module, used for:
[0047] If there is no task group to be executed in the first skip list, then the first task group to be transferred is determined according to the waiting time of each task group in the second skip list;
[0048] The first task group to be transferred is stored in the first skip list and the time wheel, and the first task group to be transferred is deleted from the second skip list.
[0049] In one possible implementation, the transfer module is specifically used for:
[0050] Traverse each task group in the second skip list. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then determine the current task group as the first task group to be transferred.
[0051] One possible implementation also includes: a deletion module, used for:
[0052] After the time wheel finishes executing the task group in the time wheel, the completed task group is deleted from the first skip list.
[0053] In one possible implementation, the transfer module is also used for:
[0054] When the timer program is running, it determines the second task group to be transferred in the second skip list, stores the second task group to be transferred in the first skip list and the time wheel, and deletes the second task group to be transferred from the second skip list.
[0055] Thirdly, another embodiment of this application provides an electronic device, including: a processor, a storage medium, and a bus, wherein the storage medium stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor communicates with the storage medium via the bus, and the processor executes the machine-readable instructions to perform the steps of any of the methods described in the first aspect above.
[0056] Fourthly, another embodiment of this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, performs the steps of any of the methods described in the first aspect above.
[0057] The beneficial effects of this application are as follows: Based on user-inputted pattern requirements, multiple task groups and their expected execution times are determined. Each task group is then traversed, and for the current task group, its waiting time is determined based on its expected execution time. The current task group is then stored in a target storage object based on its waiting time. A time wheel is then run, executing the task groups within it. This allows for the division of task groups and the combination of skip lists and the time wheel for task group processing. This not only separates task scheduling from state management but also coordinates their control, balancing scheduling performance and reliability. Furthermore, by using the time wheel for unified scheduling at fixed time units during the scheduling process, flexible and fine-grained task scheduling is achieved while avoiding the creation of separate threads for each task group, saving thread resources and improving scheduling efficiency and system response efficiency. Attached Figure Description
[0058] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0059] Figure 1 A schematic diagram of a smart home system provided in an embodiment of this application;
[0060] Figure 2 A flowchart illustrating a task scheduling method provided in an embodiment of this application;
[0061] Figure 3 A flowchart illustrating the process of determining multiple task groups and the expected execution time of each task group in the task scheduling method provided in this application embodiment;
[0062] Figure 4 A flowchart illustrating the process of storing the current task group to a target storage object based on the waiting time of the current task group in the task scheduling method provided in this application embodiment;
[0063] Figure 5 This is another flowchart illustrating the task scheduling method provided in the embodiments of this application;
[0064] Figure 6 A schematic diagram of a task scheduling device provided in an embodiment of this application;
[0065] Figure 7 This is a schematic diagram of the electronic device structure provided in an embodiment of this application. Detailed Implementation
[0066] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the accompanying drawings in this application are for illustrative and descriptive purposes only and are not intended to limit the scope of protection of this application. Furthermore, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate operations implemented according to some embodiments of this application. It should be understood that the operations in the flowcharts may not be implemented in sequence, and steps without logical contextual relationships may be reversed or implemented simultaneously. In addition, those skilled in the art, guided by the content of this application, may add one or more other operations to the flowcharts, or remove one or more operations from the flowcharts.
[0067] Furthermore, the described embodiments are merely some, not all, of the embodiments of this application. The components of the embodiments of this application described and illustrated herein can typically be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0068] It should be noted that the term "comprising" will be used in the embodiments of this application to indicate the presence of the features declared thereafter, but does not exclude the addition of other features.
[0069] In current smart home cloud platforms, delayed tasks are typically implemented using thread sleep. When a user configures a delayed task, the system creates an independent thread and calls a delay function (such as sleep or a timer) to wait for a specified time before executing subsequent operations. Multiple delayed tasks are managed through a thread pool to reduce excessive consumption of system resources.
[0070] However, traditional thread delay mechanisms suffer from significant performance bottlenecks when faced with a large number of concurrent delayed tasks. On the one hand, each delayed task requires independent thread resources, leading to a surge in the number of system threads and resulting in problems such as high memory consumption and low scheduling efficiency. On the other hand, if the cloud platform experiences a service restart or abnormal interruption, the executing thread delay tasks will be interrupted and cannot be recovered, resulting in task loss and affecting the integrity and reliability of user scenario execution.
[0071] Based on the aforementioned problems, this application proposes a task scheduling method. It determines multiple task groups and their expected execution times based on user-inputted pattern requirements, iterates through each task group, and for the current task group, determines its waiting time based on its expected execution time. The current task group is then stored in a target storage object based on its waiting time, and a time wheel is run. During runtime, the task groups in the time wheel are executed. This method allows for the division of task groups, enabling flexible task scheduling and improving system response efficiency. Furthermore, it allows for unified scheduling of each task group through the time wheel, avoiding the creation of separate threads for each group and saving thread resources. Additionally, storing task groups using a skip list prevents task loss and ensures task persistence.
[0072] It is understood that the task scheduling method provided in this application embodiment can be applied to any scenario that requires task scheduling, such as smart home scenarios, industrial automation control scenarios, etc.
[0073] For example, taking a smart home scenario as an example, Figure 1 This is a schematic diagram of a smart home system provided in an embodiment of this application, with reference to... Figure 1 As shown, a smart home system includes: a control system, a mobile terminal, and home devices.
[0074] Specifically, household appliances can include controlled devices such as lights, curtains, and air conditioners.
[0075] Users can control home devices via mobile terminals. When a user sets the operation of home devices via a mobile terminal, the control system responds to the user's settings and executes the task scheduling method provided in this application embodiment to control the operation of the home devices.
[0076] For example, a user can pre-set the operation of home appliances in wake-up mode on a mobile terminal, such as turning on the night light at 6:00 AM, turning on the main lights and curtains five seconds later, and turning off the air conditioner ten seconds later. The control system responds to the user's settings and executes the task scheduling method provided in this application embodiment, turning on the night light at 6:00 AM, turning on the main lights five seconds later, and opening the curtains and turning off the air conditioner ten seconds later.
[0077] The task scheduling method provided in this application will be described in detail below with reference to several embodiments.
[0078] Figure 2 This is a flowchart illustrating a task scheduling method provided in an embodiment of this application, with reference to... Figure 2 As shown, the executing entity of this method can be any electronic device with processing capabilities, such as the aforementioned control system. The method includes:
[0079] S201. Based on the user's input pattern requirements, determine multiple task groups and the expected execution time of each task group.
[0080] Optionally, at least one task to be executed can be determined based on the pattern requirements input by the user, thereby processing each task to be executed, determining multiple task groups, and determining the expected execution time of each task group.
[0081] Optionally, after obtaining each task group and its expected execution time, the task group, its expected execution time, the specific information of each task in each task group, and the identifier of each task group in the total task group can be stored in the preset task group storage space in the cache.
[0082] The mode requirement indicates the operating mode or status of the device corresponding to each task. Each task group includes at least one task to be executed, and the first task to be executed in at least one task group is a delayed task. The expected execution time refers to the expected time for the device corresponding to each task to execute the corresponding task.
[0083] For example, continuing with a smart home scenario, the mode requirement indicates the operating status or mode of home devices in wake-up mode. Under this mode requirement, the tasks to be performed can be determined to include: turning on the night light, turning on the main light, opening the curtains, and turning off the air conditioner. Therefore, the first task group includes: turning on the night light; the second task group includes: turning on the main light and opening the curtains; and the third task group includes: turning off the air conditioner. The expected execution time for each task group can then be determined.
[0084] S202. Traverse each task group. For the current task group that has been traversed, determine the waiting time of the current task group based on the expected execution time of the current task group, and store the current task group in the target storage object based on the waiting time of the current task group.
[0085] Optionally, each task group can be traversed, and for the current task group, the waiting time can be determined based on the expected execution time of the current task and the current time. The waiting time of the current task group refers to the time still required for the current task group to be executed, which can be obtained by subtracting the current time from the expected execution time of the first task in the current task group.
[0086] Optionally, after obtaining the waiting time of the current task group, the current task group is stored in the target storage object according to the waiting time of the current task group.
[0087] For example, the current task group can be stored in the target storage object based on its waiting time and the state of the target storage object. Alternatively, the identifier of the current task group within the overall task group can also be stored in the target storage object.
[0088] For example, the waiting time of the current task group can be combined with a preset waiting time threshold to determine whether the waiting time is less than the preset waiting time threshold. If the waiting time of the current task group is less than the preset waiting time threshold, the current task group is stored in the first skip list and the time wheel. If the waiting time of the current task group is greater than or equal to the preset waiting time threshold, the current task group is stored in the second skip list.
[0089] The target storage object includes any one of the following: a first skip list, a second skip list, a first skip list and a time wheel, wherein the waiting time of each task group in the first skip list is less than the waiting time of each task group in the second skip list.
[0090] Specifically, the first skip list can be set in the preset short-delay task storage space in the cache, and the second skip list can be set in the preset long-delay task storage space in the cache. Both the first skip list and the second skip list are ordered skip list structures. When storing each task group, the first skip list and the second skip list are sorted according to the remaining execution time of the task.
[0091] The first and second skip lists can be manipulated using Lua scripts when storing task groups, thus ensuring the atomicity of task insertion, deletion, and update and avoiding concurrency issues.
[0092] Specifically, the time wheel is driven by a preset thread. The time wheel has a circular structure and includes multiple slots. Each slot represents a time unit and can store several tasks to be executed. The time wheel is used to handle short-delay tasks.
[0093] It is understandable that the first and second skip lists are used for the persistence and management of task states, while the time wheel is used to handle short-latency tasks and ensure scheduling efficiency. By combining the first skip list with the time wheel, task scheduling and state management can be separated but coordinated, balancing performance and reliability.
[0094] S203. Run the time wheel. When the time wheel is running, the task groups in the time wheel are executed.
[0095] Optionally, the time wheel is run, and when the time wheel is running, the task groups in the time wheel are executed.
[0096] Specifically, when the time wheel is running, it periodically checks the task group in the current slot. If there is a task group in the current slot, it triggers the execution process of the task group and removes the task group from the time wheel after the task group has been executed.
[0097] When the time wheel is running, it can look up the specific information of each task from the preset task group storage space in the cache and trigger its execution.
[0098] By using a ring-shaped time wheel as a unified scheduling mechanism for short-delay task groups, and through the periodic advancement of threads, the corresponding slot task groups are checked and executed in each time unit, thereby achieving an efficient, low-resource-consumption, and recoverable delayed task scheduling mechanism.
[0099] It's worth noting that during the execution of S201-S203 above, the main thread executes S201-S202, splitting the task groups and writing them to the target storage object, while the time wheel thread executes S203, running the time wheel. Both the main thread and the time wheel thread are independent scheduling threads. This means that the process of the main thread splitting the task groups and writing them to the target storage object is independent of the process of the time wheel thread actively executing tasks.
[0100] In this embodiment, multiple task groups and their expected execution times are determined based on user-inputted pattern requirements. Each task group is then iterated through. For the current task group, its waiting time is determined based on its expected execution time. The current task group is then stored in a target storage object based on its waiting time. A time wheel is then run, executing the task groups within it. This allows for task group division, and the combination of a skip list and the time wheel for task group processing not only separates task scheduling from state management but also coordinates their control, balancing performance and reliability. Furthermore, during scheduling, the time wheel uses a fixed time unit for unified scheduling, enabling flexible and fine-grained task scheduling while avoiding the creation of separate threads for each task group, saving thread resources and improving scheduling and system response efficiency.
[0101] In one possible implementation, Figure 3This is a flowchart illustrating the process of determining multiple task groups and the expected execution time of each task group in the task scheduling method provided in this application embodiment, with reference to... Figure 3 As shown, in step S201 above, based on the user's input pattern requirements, multiple task groups and the expected execution time of each task group are determined, including:
[0102] S301. Based on the pattern requirements, determine the multiple tasks to be executed in sequence.
[0103] Optionally, based on the pattern requirements, multiple tasks under the pattern requirements can be found, and the tasks can be arranged according to their execution time to obtain multiple tasks to be executed in sequence.
[0104] Among them, multiple tasks to be executed are arranged in order of their execution time, which is determined based on the pattern requirements.
[0105] For example, continuing with the smart home scenario, the mode requirement is used to indicate the operating status or operating mode of home devices in the wake-up mode. Under this mode requirement, multiple tasks to be performed in sequence can be identified, including: turning on the night light, turning on the main light, opening the curtains, and turning off the air conditioner.
[0106] S302. Based on the task type of each task to be executed, determine at least one delayed task among the multiple tasks to be executed.
[0107] Optionally, the task type of each task to be executed can be determined. If the task type of the task to be executed is a delayed task, then the task to be executed can be determined to be a delayed task.
[0108] The task types include delayed tasks and non-delayed tasks. Delayed tasks are those that require a certain amount of time to execute, meaning there is a time difference between their triggering and execution times. Non-delayed tasks are those that are executed immediately, meaning there is no time difference between their triggering and execution times.
[0109] S303. Determine multiple task groups based on the arrangement of each delayed task.
[0110] Optionally, the multiple tasks to be executed can be divided into multiple task groups according to the position of each delayed task in the multiple tasks to be executed in sequence.
[0111] For example, continuing with the smart home scenario, the mode requirement is used to indicate the operating status or mode of home devices in wake-up mode. Under this mode requirement, the first task group includes: turning on the night light; the second task group includes: turning on the main lights and curtains; and the third task group includes: turning off the air conditioner. The expected execution time for each task group is then determined.
[0112] S304. Determine the expected execution time of each task group based on the execution time of at least one task in each task group.
[0113] Optionally, for each task group, the expected execution time of each task group is determined based on the expected execution time of at least one task in each task group.
[0114] For example, continuing with the smart home scenario, the second task group includes: when turning on the lights and curtains, the expected execution time of the second task group can be the expected execution time of the task of turning on the lights, or it can be the expected execution time of the two tasks of turning on the lights and opening the curtains.
[0115] In one possible implementation, S303 above determines multiple task groups based on the arrangement of the delayed tasks, including:
[0116] The target task to be executed between the current delayed task and the next delayed task of the current delayed task, as well as the current delayed task, are grouped into a task group.
[0117] Optionally, the target task to be executed between the current delayed task and the next delayed task of the current delayed task, as well as the current delayed task, can be grouped together as a task group.
[0118] For example, taking the first delayed task as an example, the tasks before the first delayed task can be regarded as the first task group, the tasks between the first delayed task and the second delayed task can be regarded as the target tasks to be executed, the target tasks to be executed and the first delayed task can be regarded as the second task group, the tasks between the second delayed task and the third delayed task can be regarded as the target tasks to be executed, the target tasks to be executed and the second delayed task can be regarded as the third task group, and so on.
[0119] Among them, the currently delayed tasks and the tasks to be executed for each target are arranged in the task group according to the execution time of the tasks.
[0120] By arranging the delayed tasks, we can divide them into task groups, which allows for a reasonable allocation of scheduling mechanisms, avoids resource waste, and enables the prioritization of time-sensitive tasks during scheduling, thus improving the user experience.
[0121] In one possible implementation, Figure 4 This is a flowchart illustrating the process of storing the current task group to a target storage object based on the waiting time of the current task group in the task scheduling method provided in this application embodiment. (Refer to...) Figure 4 As shown, in step S202 above, storing the current task group to the target storage object based on the current task group's waiting time includes:
[0122] S401. If the waiting time of the current task group is less than the first threshold and there is no task group in the first skip list, then the target storage object is determined to be the first skip list and the time wheel, and the current task group is stored in the first skip list and the time wheel.
[0123] It is understandable that when storing the current task group to the target storage object based on the waiting time of the current task group, the current task group can be stored in different target storage objects depending on the different waiting times of the current task group and the different states of the target storage object.
[0124] Optionally, if the waiting time of the current task group is less than the first threshold and there is no task group in the first skip list, then the task in the current task group can be determined to be a short-delay task. In this case, the target storage object can be determined to be the first skip list and the time wheel, and the current task group can be stored in the first skip list and the time wheel.
[0125] Storing the current task group in the first skip list means inserting the current task group into the corresponding position in the first skip list according to the waiting time. Storing the current task group in the time wheel means inserting the current task group into the corresponding slot in the time wheel according to the waiting time.
[0126] If the waiting time of the current task group is less than the first threshold, and the task group does not exist in the first skip list, the target storage objects are determined to be the first skip list and the time wheel. The current task group is then stored in both the first skip list and the time wheel. This allows tasks in the time wheel to be stored through the first skip list, ensuring execution can resume even if the time wheel restarts, preventing task loss after a service restart and guaranteeing task persistence. Furthermore, the first skip list and the time wheel enable rapid response and execution, thereby improving scheduling performance.
[0127] S402. If the waiting time of the current task group is less than the first threshold, and there is a task group in the first skip list, then the target storage object is determined to be the first skip list, and the task group that already exists in the first skip list is stored in the time wheel, and the current task group is stored in the first skip list.
[0128] Optionally, if the waiting time of the current task group is less than the first threshold, and the task group exists in the first skip list, then the task in the current task group can be determined to be a short-delay task, and the time wheel does not yet meet the addition conditions of the current task group. In this case, the target storage object can be determined to be the first skip list, and the task group that already exists in the first skip list can be stored in the time wheel, and the current task group can be stored in the first skip list. This allows the task in the first skip list to be added to the time wheel after the time wheel meets the task conditions, so that the time wheel can run the corresponding task.
[0129] Storing the current task group into the first skip list means inserting the current task group into the corresponding position in the first skip list according to the waiting time.
[0130] When the waiting time of the current task group is less than the first threshold and the task group exists in the first skip list, the target storage object is determined to be the first skip list. The task groups that already exist in the first skip list are stored in the time wheel, and the current task group is stored in the first skip list. This can avoid the accumulation of too many "about to be executed" tasks in the first skip list, which would cause delays. It can also prevent the task groups in the first skip list from "getting stuck" or "accumulating". At the same time, it can also realize the dynamic division of responsibilities and collaborative scheduling between the first skip list and the time wheel, improving the dynamic adaptability and flexibility in the scheduling process.
[0131] S403. If the waiting time of the current task group is greater than the first threshold, then the target storage object is determined to be the second skip list, and the current task group is stored in the second skip list.
[0132] Optionally, if the waiting time of the current task group is greater than the first threshold, the task in the current task group can be determined to be a long-delay task. Then the target storage object can be determined to be the second skip list, and the current task group can be stored in the second skip list.
[0133] Storing the current task group into the second skip list means inserting the current task group into the corresponding position in the second skip list according to the waiting time.
[0134] If the waiting time of the current task group exceeds the first threshold, the target storage object is determined as the second skip list, and the current task group is stored in the second skip list. The second skip list can store long-delayed tasks, avoiding long-delayed tasks from occupying time wheel resources for a long time. At the same time, it can realize the phased scheduling of tasks from waiting to preparation to execution, and can also improve the throughput during the scheduling process.
[0135] It is understood that during the execution of the task scheduling method provided in the embodiments of this application, the second skip list can be dynamically migrated to the first skip list according to the progress of each task group for task scheduling. The following is an exemplary description.
[0136] In one possible implementation, Figure 5 This is another flowchart illustrating the task scheduling method provided in the embodiments of this application, referred to... Figure 5 As shown, it also includes:
[0137] S501. If there is no task group to be executed in the first jump table, then determine the first task group to be transferred based on the waiting time of each task group in the second jump table.
[0138] Optionally, if there is no task group to be executed in the first skip list, the first task group to be transferred can be determined based on the waiting time of each task group in the second skip list.
[0139] For example, a lookup can be performed on the first skip list. If the first skip list is empty, it can be determined that there is no task group to be executed in the first skip list.
[0140] For example, the task groups in the second skip list can be sorted according to the waiting time of each task group to determine the first task group to be transferred.
[0141] For example, the target task group to be transferred can also be found from the second jump table according to the first threshold, and all task groups whose positions are before the target task group to be transferred can be regarded as the first task group to be transferred.
[0142] S502. Store the first task group to be transferred into the first skip list and the time wheel, and delete the first task group to be transferred from the second skip list.
[0143] Optionally, the first task group to be transferred is inserted into the corresponding position in the first skip list and the time wheel according to the waiting time of the first task group to be transferred, and then deleted from the second skip list. S501-S502 described above can be implemented through the main thread.
[0144] By identifying the first task group to be transferred in the second jump table and dynamically migrating it from the second jump table to the first jump table, the responsiveness, resource utilization, and task execution success rate during task scheduling can be significantly improved.
[0145] In one possible implementation, the process in S501 above, which determines the first task group to be transferred based on the waiting time of each task group in the second skip table, includes:
[0146] Traverse each task group in the second skip list. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then determine the current task group as the first task group to be transferred.
[0147] Optionally, each task group in the second skip list can be traversed. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then the current task group is used as a first task group to be transferred.
[0148] In one possible implementation, the method further includes:
[0149] After the time wheel finishes executing the task group in the time wheel, it will remove the completed task group from the first skip list.
[0150] Optionally, after the time wheel finishes executing the task group in the time wheel, the completed task group can be deleted from the first skip list through the time wheel thread. This ensures that the task status is consistent with the system status, avoids repeated execution, and also releases memory space, reduces the number of skip list nodes, and improves the efficiency of skip list query and management.
[0151] In one possible implementation, the method further includes:
[0152] When the timer runs, it determines the second task group to be transferred in the second skip list, stores the second task group to be transferred in the first skip list and the time wheel, and deletes the second task group to be transferred from the second skip list.
[0153] Optionally, a timer program can be run. When the timer program runs, it determines the second group of tasks to be transferred in the second skip list, stores the second group of tasks to be transferred in the first skip list and the time wheel, and deletes the second group of tasks to be transferred from the second skip list. The timer program can be implemented using a timed scanning thread.
[0154] For example, when the timer is running, the second task group to be transferred can be determined based on the waiting time of each task group in the second skip table.
[0155] For example, a candidate task group to be transferred can be found from the second skip list according to a first threshold, and all task groups whose positions are before the candidate task group to be transferred can be used as the second task group to be transferred.
[0156] For example, the second task group to be transferred is stored in the first skip list and the time wheel according to the waiting time of the second task group to be transferred, and the second task group to be transferred is deleted from the second skip list, thereby significantly improving the responsiveness, resource utilization and task execution success rate in the task scheduling process.
[0157] In one possible implementation, the method further includes:
[0158] When the time wheel restarts, it scans and updates the first and second skip lists, runs the time wheel, and executes the task groups in the time wheel while it is running.
[0159] Optionally, when the time wheel restarts, the loading thread can scan each task group in the first skip list and the second skip list, and determine the waiting time of each task group in the first skip list and the waiting time of each task group in the second skip list based on the expected execution time of each task group in the first skip list and the expected execution time of each task group in the second skip list. Then, based on the waiting time of each task group and the first threshold, the task groups with waiting times less than the first threshold are stored in the first skip list and the time wheel, and the task groups with waiting times greater than or equal to the first threshold are stored in the second skip list, so as to update the first skip list and the second skip list.
[0160] Meanwhile, the time wheel is run by a time wheel thread. When the time wheel is running, the task groups in the time wheel are executed, which can rebuild the tasks in the time wheel, avoid task loss, and ensure high availability in the task scheduling process.
[0161] Based on the same inventive concept, this application also provides a task scheduling device corresponding to the task scheduling method. Since the principle of the device in this application is similar to the task scheduling method described above in this application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0162] Reference Figure 6 As shown, Figure 6 This is a schematic diagram of a task scheduling device provided in an embodiment of the present application. The device includes: a first determining module 601, a second determining module 602, and an execution module 603.
[0163] The first determining module 601 is used to determine multiple task groups and the expected execution time of each task group according to the pattern requirements input by the user. Each task group includes at least one task to be executed, and the first task to be executed in at least one task group is a delayed task.
[0164] The second determining module 602 is used to traverse each task group, and for the current task group that has been traversed, to determine the waiting time of the current task group based on the expected execution time of the current task group, and to store the current task group in the target storage object based on the waiting time of the current task group. The target storage object includes any one of the following: a first skip list, a second skip list, a first skip list and a time wheel, wherein the waiting time of each task group in the first skip list is less than the waiting time of each task group in the second skip list.
[0165] Execution module 603 is used to run the time wheel. When the time wheel is running, it executes the task groups in the time wheel.
[0166] In one possible implementation, the first determining module 601 is specifically used for:
[0167] Based on the pattern requirements, a number of tasks to be executed are determined in sequence. These tasks are arranged in order of their execution time, which is determined based on the pattern requirements.
[0168] Based on the task type of each task to be executed, determine at least one delayed task among multiple tasks to be executed;
[0169] Based on the arrangement of each delayed task, determine multiple task groups;
[0170] The expected execution time for each task group is determined based on the execution time of at least one task in each task group.
[0171] In one possible implementation, the first determining module 601 is specifically used for:
[0172] The current delayed task and the next delayed task of the current delayed task are grouped together as a task group. The current delayed task and each target task to be executed are arranged in the task group according to the execution time of the task.
[0173] In one possible implementation, the second determining module 602 is specifically used for:
[0174] If the waiting time of the current task group is less than the first threshold, and there is no task group in the first skip list, then the target storage object is determined to be the first skip list and the time wheel, and the current task group is stored in the first skip list and the time wheel.
[0175] If the waiting time of the current task group is less than the first threshold, and there is a task group in the first skip list, then the target storage object is determined to be the first skip list, and the task group that already exists in the first skip list is stored in the time wheel, and the current task group is stored in the first skip list.
[0176] If the waiting time of the current task group is greater than the first threshold, the target storage object is determined to be the second skip list, and the current task group is stored in the second skip list.
[0177] In one possible implementation, it also includes: a transfer module, used for:
[0178] If there is no task group to be executed in the first jump table, the first task group to be transferred is determined based on the waiting time of each task group in the second jump table.
[0179] Store the first task group to be transferred into the first skip list and the time wheel, and delete the first task group to be transferred from the second skip list.
[0180] In one possible implementation, the transfer module is specifically used for:
[0181] Traverse each task group in the second skip list. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then determine the current task group as the first task group to be transferred.
[0182] One possible implementation also includes: a deletion module, used for:
[0183] After the time wheel finishes executing the task group in the time wheel, it will remove the completed task group from the first skip list.
[0184] In one possible implementation, the transfer module is also used for:
[0185] When the timer runs, it determines the second task group to be transferred in the second skip list, stores the second task group to be transferred in the first skip list and the time wheel, and deletes the second task group to be transferred from the second skip list.
[0186] The processing flow of each module in the device and the interaction flow between each module can be referred to the relevant descriptions in the above method embodiments, and will not be detailed here.
[0187] This application also provides an electronic device, such as... Figure 7 As shown, Figure 7 The schematic diagram of the electronic device structure provided in the embodiments of this application includes: a processor 701, a memory 702, and optionally, a bus 703. The memory 702 stores machine-readable instructions executable by the processor 701 (e.g., ...). Figure 6 The device includes the first determining module 601, the second determining module 602, and the execution module 603 (and the corresponding execution instructions, etc.). When the electronic device is running, the processor 701 and the memory 702 communicate through the bus 703. When the machine-readable instructions are executed by the processor 701, the steps of the above-mentioned task scheduling method are performed.
[0188] This application also provides a computer-readable storage medium storing a computer program, which, when run by a processor, executes the steps of the task scheduling method described above.
[0189] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems and devices described above can be referred to the corresponding processes in the method embodiments, and will not be repeated here. In the several embodiments provided in this application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed mutual coupling or direct coupling or communication connection can be through some communication interfaces; the indirect coupling or communication connection of devices or modules can be electrical, mechanical, or other forms.
[0190] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. If the functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0191] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any changes 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.
Claims
1. A task scheduling method, characterized in that, include: Based on the user's input pattern requirements, multiple task groups and the expected execution time of each task group are determined. Each task group includes at least one task to be executed, and the first task to be executed in at least one task group is a delayed task. Traverse each of the task groups. For the current task group, determine the waiting time of the current task group based on the expected execution time of the current task group. Store the current task group in a target storage object based on the waiting time of the current task group. The target storage object includes any one of the following: a first skip list, a second skip list, a first skip list and a time wheel, wherein the waiting time of each task group in the first skip list is less than the waiting time of each task group in the second skip list. Run the time wheel, and when the time wheel is running, execute the task groups in the time wheel.
2. The task scheduling method according to claim 1, characterized in that, The step of determining multiple task groups and the expected execution time of each task group based on the user's input pattern requirements includes: Based on the mode requirements, a plurality of tasks to be executed are determined in sequence. The plurality of tasks to be executed are arranged in sequence according to their execution time, and the execution time of the tasks is determined based on the mode requirements. Based on the task type of each of the tasks to be executed, at least one delayed task is determined from the plurality of tasks to be executed; The plurality of task groups are determined according to the arrangement of each of the delayed tasks; The expected execution time of each task group is determined based on the execution time of at least one task in each task group.
3. The task scheduling method according to claim 2, characterized in that, The step of determining the plurality of task groups based on the arrangement of the delayed tasks includes: The current delayed task and the target tasks to be executed between the current delayed task and the next delayed task of the current delayed task, as well as the current delayed task, are regarded as a task group, wherein the current delayed task and each of the target tasks to be executed are arranged in the task group according to the execution time of the tasks.
4. The task scheduling method according to claim 1, characterized in that, The step of storing the current task group to the target storage object according to the waiting time of the current task group includes: If the waiting time of the current task group is less than the first threshold, and there is no task group in the first skip list, then the target storage object is determined to be the first skip list and the time wheel, and the current task group is stored in the first skip list and the time wheel. If the waiting time of the current task group is less than the first threshold, and there is a task group in the first skip list, then the target storage object is determined to be the first skip list, and the task group that already exists in the first skip list is stored in the time wheel, and the current task group is stored in the first skip list; If the waiting time of the current task group is greater than the first threshold, then the target storage object is determined to be the second skip list, and the current task group is stored in the second skip list.
5. The task scheduling method according to claim 4, characterized in that, Also includes: If there is no task group to be executed in the first skip list, then the first task group to be transferred is determined according to the waiting time of each task group in the second skip list; The first task group to be transferred is stored in the first skip list and the time wheel, and the first task group to be transferred is deleted from the second skip list.
6. The task scheduling method according to claim 5, characterized in that, The step of determining the first task group to be transferred based on the waiting time of each task group in the second skip list includes: Traverse each task group in the second skip list. For the current task group that is traversed, if the waiting time of the current task group is less than the first threshold, then determine the current task group as the first task group to be transferred.
7. The task scheduling method according to claim 1, characterized in that, Also includes: After the time wheel finishes executing the task group in the time wheel, the completed task group is deleted from the first skip list.
8. The task scheduling method according to claim 1, characterized in that, Also includes: When the timer program is running, it determines the second task group to be transferred in the second skip list, stores the second task group to be transferred in the first skip list and the time wheel, and deletes the second task group to be transferred from the second skip list.
9. An electronic device, characterized in that, include: A processor and a memory, the memory storing machine-readable instructions executable by the processor, which, when the electronic device is running, are executed by the processor to perform the steps of the task scheduling method as described in any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the steps of the task scheduling method as described in any one of claims 1 to 8.