Timing task scheduling method and device, equipment and medium

CN117112160BActive Publication Date: 2026-10-27ZHENGZHOU YUNHAI INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310952920.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-31
Publication Date
2026-10-27
Estimated Expiration
2043-07-31

AI Technical Summary

Benefits of technology

[0041]本申请有益效果为:本申请应用于当前级别工作线程,获取当前全局时间以及下一级别工作线程的当前局部时间,并确定所述当前全局时间和所述当前局部时间的当前时间间隔;如果所述当前时间间隔不大于预设阈值,则判断所述当前全局时间和所述当前局部时间是否相等;若相等,则获取当前更新后的所述当前全局时间以及当前所述下一级别工作线程更新后的所述当前局部时间,并重新跳转至所述确定所述当前全局时间和所述当前局部时间的当前时间间隔的步骤;若不相等,则对所述当前局部时间进行更新,以便所述下一级别工作线程基于所述当前局部时间执行对应的定时任务,并重新跳转至所述判断所述当前全局时间和所述当前局部时间是否相等的步骤。由此可见,本申请是当前级别工作线程去检测下一级别工作线程的互相检测机制,而不是自检机制,可以避免因自身发生超时而无法检测,即可以可靠的进行超时检测;如果当前全局时间和下一级别工作线程的当前局部时间一致,则说明下一级别工作线程正常工作,也无需对下一级别工作线程进行调整,如果当前全局时间和下一级别工作线程的当前局部时间不一致时,则对下一级别工作线程的当前局部时间进行更新,以使得下一级别工作线程能够基于当前局部时间执行对应的定时任务,保证了定时任务被及时调度。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117112160B_ABST
    Figure CN117112160B_ABST
Patent Text Reader

Abstract

The application discloses a timing task scheduling method and device, equipment and medium, a multi-task technical field, applied to the current level work thread, comprising: acquiring the current global time and the current local time of the next level work thread, and determining the current time interval of the current global time and the current local time; if the current time interval is not greater than a preset threshold, it is judged whether the current global time and the current local time are equal; if equal, the current updated current global time and the current updated current local time of the next level work thread are acquired, and the step of determining the current time interval of the current global time and the current local time is re-jumped; if not equal, the current local time is updated, so that the next level work thread executes the corresponding timing task based on the current local time, and the step of judging whether the current global time and the current local time are equal is re-jumped. The reliability of the scheduled timing task is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of multitasking technology, and in particular to timed task scheduling methods, apparatus, equipment and media. Background Technology

[0002] Scheduled task execution is a crucial part of storage systems. Scheduled tasks provide a convenient way to accomplish specific tasks. The scheduling of scheduled tasks includes modules for task storage, task triggering, task processing, and time settings. Given the accuracy and stability requirements of storage systems, it's essential to ensure task security and timely processing. Therefore, it's necessary to monitor the worker threads executing scheduled tasks and handle any anomalies promptly to guarantee successful scheduled task execution.

[0003] In summary, improving the reliability of scheduled tasks is a problem that needs to be solved in this field. Summary of the Invention

[0004] In view of this, the purpose of this invention is to provide a method, apparatus, device, and medium for scheduling timed tasks, thereby improving the reliability of scheduled tasks. The specific solution is as follows:

[0005] Firstly, this application discloses a timed task scheduling method, applied to the current-level worker thread, including:

[0006] Obtain the current global time and the current local time of the next-level worker thread, and determine the current time interval between the current global time and the current local time;

[0007] If the current time interval is not greater than a preset threshold, then determine whether the current global time and the current local time are equal;

[0008] If they are equal, then obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time;

[0009] If they are not equal, the current local time is updated so that the next-level worker thread can execute the corresponding timed task based on the current local time, and then jump back to the step of determining whether the current global time and the current local time are equal.

[0010] Optionally, after determining the current time interval between the current global time and the current local time, the method further includes:

[0011] If the current time interval is greater than a preset threshold, an alarm command is sent to a preset display platform so that the user can stop the execution of all the working threads based on the alarm command displayed on the preset display platform.

[0012] Optionally, updating the current local time includes:

[0013] The current local time is increased by a preset unit time step to update the current local time.

[0014] Optionally, the next-level worker thread executes a corresponding scheduled task based on the current local time, including:

[0015] The next-level worker thread determines whether the local first ready queue is empty. If not, it updates the current heartbeat count of the timed task in the current first ready queue based on the current local time to obtain the current updated heartbeat count of the timed task.

[0016] If a scheduled task with a current updated heartbeat count of zero exists in the current first ready queue, then the scheduled task is executed; otherwise, the current updated heartbeat count of the scheduled task is updated again.

[0017] Optionally, if a scheduled task with a current updated heartbeat count of zero exists in the current first ready queue, then the scheduled task is executed; otherwise, the current updated heartbeat count of the scheduled task is updated again, including:

[0018] When the data structure of the current first ready queue is a linked list structure, determine whether the current updated heartbeat count of the first timed task in the current first ready queue is zero;

[0019] If not, then the current updated heartbeat count of all timed tasks in the current first ready queue is updated again;

[0020] If so, the first timed task in the current first ready queue is executed, the current first ready queue is updated to obtain the next first ready queue, and the next first ready queue is used as the current first ready queue. Then, the process jumps back to the step of determining whether the current updated heartbeat count of the first timed task in the current first ready queue is zero.

[0021] Optionally, executing the first scheduled task in the current first ready queue includes:

[0022] The first scheduled task in the current first ready queue is identified as the target scheduled task, and the target scheduled task is removed from the current first ready queue;

[0023] The callback function for executing the target scheduled task is executed.

[0024] Optionally, the timed task scheduling method further includes:

[0025] If there is a scheduled task to be inserted, then the scheduled task to be inserted is inserted into the current second ready queue of the current level worker thread to obtain the next ready queue.

[0026] Optionally, inserting the scheduled task to be inserted into the current second ready queue of the current level worker thread includes:

[0027] Add a write lock to the current second ready queue of the current level worker thread, obtain the unit heartbeat time corresponding to the frequency of the central processing unit, and determine the multiple relationship between the timing duration of the timed task to be inserted and the unit heartbeat time, so as to determine the initial heartbeat number of the timed task to be inserted based on the multiple relationship.

[0028] Determine the data structure of the current second ready queue;

[0029] If the data structure of the current second ready queue is an array or a queue, then the timed task to be inserted is inserted at the tail of the current second ready queue;

[0030] If the data structure of the current second ready queue is a linked list, the insertion position and updated heartbeat count of the timed task to be inserted are determined based on the initial heartbeat count and the heartbeat count of the timed tasks already stored in the current second ready queue, so as to insert the timed task to be inserted into the current second ready queue.

[0031] Adjust the task status of the scheduled task to be inserted to the pending execution status, and release the write lock on the current second ready queue.

[0032] Secondly, this application discloses a timed task scheduling device, applied to the current-level worker thread, including:

[0033] An interval determination module is used to obtain the current global time and the current local time of the next-level worker thread, and to determine the current time interval between the current global time and the current local time.

[0034] The time determination module is used to determine whether the current global time and the current local time are equal if the current time interval is not greater than a preset threshold.

[0035] The first jump module is used to, if they are equal, obtain the currently updated global time and the currently updated local time of the next level worker thread, and then jump back to the step of determining the current time interval between the current global time and the current local time;

[0036] The second jump module is used to update the current local time if they are not equal, so that the next level worker thread can execute the corresponding timed task based on the current local time and jump back to the step of determining whether the current global time and the current local time are equal.

[0037] Thirdly, this application discloses an electronic device, including:

[0038] Memory, used to store computer programs;

[0039] A processor is used to execute the computer program to implement the steps of the aforementioned disclosed timed task scheduling method.

[0040] Fourthly, this application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the steps of the aforementioned disclosed timed task scheduling method.

[0041] The beneficial effects of this application are as follows: This application is applied to the current level worker thread, obtains the current global time and the current local time of the next level worker thread, and determines the current time interval between the current global time and the current local time; if the current time interval is not greater than a preset threshold, it determines whether the current global time and the current local time are equal; if they are equal, it obtains the currently updated current global time and the currently updated current local time of the next level worker thread, and jumps back to the step of determining the current time interval between the current global time and the current local time; if they are not equal, it updates the current local time so that the next level worker thread executes the corresponding timed task based on the current local time, and jumps back to the step of determining whether the current global time and the current local time are equal. Therefore, this application is a mutual detection mechanism where the current-level worker thread detects the next-level worker thread, rather than a self-checking mechanism. This avoids the inability to detect due to timeouts, ensuring reliable timeout detection. If the current global time is consistent with the current local time of the next-level worker thread, it means the next-level worker thread is working normally, and no adjustment is needed. If the current global time is inconsistent with the current local time of the next-level worker thread, the current local time of the next-level worker thread is updated so that the next-level worker thread can execute the corresponding scheduled task based on the current local time, ensuring that the scheduled task is scheduled in a timely manner. Attached Figure Description

[0042] 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, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0043] Figure 1 This is a flowchart of a timed task scheduling method disclosed in this application;

[0044] Figure 2 This is a specific schematic diagram of a working thread disclosed in this application;

[0045] Figure 3 This is a schematic diagram of a specific timed task structure disclosed in this application;

[0046] Figure 4 This is a schematic diagram of a specific timed task insertion disclosed in this application;

[0047] Figure 5 This is a schematic diagram illustrating a specific task insertion method based on heartbeat count disclosed in this application;

[0048] Figure 6 This is a schematic diagram illustrating a specific insertion position determination disclosed in this application;

[0049] Figure 7 This is a flowchart of a specific timed task scheduling method disclosed in this application;

[0050] Figure 8 Here is a flowchart of another specific timed task scheduling method disclosed in this application;

[0051] Figure 9 This is a schematic diagram of the structure of a timed task scheduling device disclosed in this application;

[0052] Figure 10 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0053] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.

[0054] Scheduled task execution is a crucial part of storage systems. Scheduled tasks provide a convenient way to accomplish specific tasks. The scheduling of scheduled tasks includes modules for task storage, task triggering, task processing, and time settings. Given the accuracy and stability requirements of storage systems, it's essential to ensure task security and timely processing. Therefore, it's necessary to monitor the worker threads executing scheduled tasks and handle any anomalies promptly to guarantee successful scheduled task execution.

[0055] Therefore, this application provides a timed task scheduling scheme to improve the reliability of timed task scheduling.

[0056] See Figure 1 As shown in the figure, this application discloses a timed task scheduling method, applied to the current level worker thread, including:

[0057] Step S11: Obtain the current global time and the current local time of the next level worker thread, and determine the current time interval between the current global time and the current local time.

[0058] It's important to note that in this embodiment, the current-level worker thread checks for timeouts in the next-level worker thread, rather than checking its own thread. This is because if a next-level worker thread times out, it cannot perform a self-check. However, this embodiment uses a worker thread mutual checking mechanism, ensuring that even if a next-level worker thread times out, the current-level worker thread can still check it, guaranteeing normal detection. Here, "current level" and "next level" are relative. For example, in a pre-defined scheduling system with three worker threads: worker thread 0, worker thread 1, and worker thread 2, when worker thread 0 is the current-level worker thread, worker thread 1 is the next-level worker thread; when worker thread 1 is the current-level worker thread, worker thread 2 is the next-level worker thread; and when worker thread 2 is the current-level worker thread, worker thread 0 is the next-level worker thread. This forms a closed-loop detection level, ensuring that each worker thread only needs to check the other worker thread, guaranteeing that each worker thread can be checked by another worker thread without unduly increasing the workload of the worker threads.

[0059] For example Figure 2 The diagram illustrates a specific worker thread. The system contains N Central Processing Units (CPUs), and therefore should have N worker threads. Global time can be understood as the real-time time at which the system updates, i.e., the implementation time maintained by all worker threads. The global time of a worker thread is the real-time time at which that thread updates itself. The local time of all worker threads should be roughly consistent with the global time. This means that the current time interval between the local and global times needs to be obtained. Based on the relationship between the current time interval and a preset threshold, it needs to be determined whether a timeout has occurred. If the current time interval is greater than the preset threshold, it indicates that the next level of worker thread has timed out, requiring an alarm to be reported and all worker threads to be terminated.

[0060] Step S12: If the current time interval is not greater than a preset threshold, then determine whether the current global time and the current local time are equal.

[0061] Understandably, if the current time interval is not greater than the preset threshold, it means that the current local time of the next level worker thread is roughly consistent with the current global time. However, it is necessary to determine whether the current local time of the next level worker thread is completely consistent with the current global time or whether there is a certain deviation.

[0062] Step S13: If they are equal, obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time.

[0063] In this embodiment, if the current global time and the current local time are equal, it means that the next level worker thread does not currently have a local time update problem. Therefore, the timed task of the next level worker thread will be executed in a timely manner. It is only necessary to re-obtain the current updated global time and the current updated local time of the next level worker thread. In other words, if the current global time and the current local time are equal, it is necessary to re-obtain the updated global time and local time and continuously check whether the next level worker thread has a local update problem.

[0064] Step S14: If they are not equal, update the current local time so that the next level worker thread can execute the corresponding timed task based on the current local time, and jump back to the step of determining whether the current global time and the current local time are equal.

[0065] Understandably, if the current time interval between the current global time and the current local time is less than a preset threshold, and the current global time and the current local time are not equal, it indicates that there is a slight deviation between the current local time and the current global time of the next-level worker thread. However, this deviation is not significant. Therefore, the current local time of the next-level worker thread can be actively updated so that the next-level worker thread executes the corresponding scheduled task based on the current local time. Each time the current local time of the next-level worker thread is updated, only the smallest granularity time step is added. This allows the worker thread to execute scheduled tasks with high precision. However, it is impossible to determine whether the updated current local time is consistent with the current global time. Therefore, it is necessary to re-evaluate whether the updated current local time is consistent with the current global time. In other words, if they are consistent, it means that the next-level worker thread can complete the scheduled task normally without further adjustment. If they are inconsistent, it means that the current local time of the next-level worker thread needs to be updated again until the updated local time is consistent with the current global time.

[0066] In this embodiment, if there is a scheduled task to be inserted, it is inserted into the current second ready queue of the current-level worker thread to obtain the next ready queue. The task creation module in the scheduling system creates scheduled tasks to be inserted according to specific circumstances, so that subsequent scheduled tasks can be inserted into the corresponding worker threads. The structure of the scheduled task to be inserted includes task status, callback function, timeout duration, whether the task is scheduled cyclically, and task position identifier. The task status indicates whether it has been inserted into the corresponding ready queue. The callback function is used to execute the scheduled task. The timeout duration indicates the execution time of the scheduled task, and the unit of the timeout duration is milliseconds. Assuming that the task is expected to run after P milliseconds, the timeout duration is set to P (P is a positive integer). Whether the task is scheduled cyclically indicates whether the scheduled task needs to be executed every certain period of time. The task position identifier indicates the position in the current ready queue. It can also be used to chain all tasks in the ready queue together to determine the order of task execution.

[0067] The following example uses a linked list structure for the ready queue data structure. Figure 3 The diagram shows a specific timed task structure. Whether the task is scheduled cyclically is a Boolean variable. If you want the timed task to be cyclic, set it to true; otherwise, set it to false. If the timed task is set to cyclic, it will be executed once at fixed intervals. The linked list pointer (i.e., the task position identifier) ​​is used to mount the task in the ready queue.

[0068] It is understandable that all worker threads insert tasks in the same way, and in order to maximize performance, the number of worker threads needs to be determined by the number of cores. That is, if there are n CPU cores, then n worker threads need to be created. Each worker thread has a ready queue to store timed tasks. Unlike the existing technology where a worker thread can only store one timed task, in this embodiment, a ready queue can store multiple timed tasks, which can reduce the number of worker threads and allow more resources to be used to process other tasks.

[0069] In this embodiment, inserting the scheduled task to be inserted into the current second ready queue of the current-level worker thread includes: adding a write lock to the current second ready queue of the current-level worker thread; obtaining the unit heartbeat time corresponding to the frequency of the central processing unit; and determining the multiple relationship between the timing duration of the scheduled task to be inserted and the unit heartbeat time, so as to determine the initial heartbeat count of the scheduled task to be inserted based on the multiple relationship; determining the data structure of the current second ready queue; if the data structure of the current second ready queue is an array or a queue, then inserting the scheduled task to be inserted into the tail of the current second ready queue; if the data structure of the current second ready queue is a linked list, then determining the insertion position and updated heartbeat count of the scheduled task to be inserted based on the initial heartbeat count and the heartbeat count of the scheduled tasks already stored in the current second ready queue, so as to insert the scheduled task to be inserted into the current second ready queue; adjusting the task status of the scheduled task to be inserted to the pending execution state, and releasing the write lock on the current second ready queue. For example... Figure 4 The diagram illustrates a specific method for inserting a scheduled task. The specific process of inserting the scheduled task to be inserted into the current second ready queue of the current level worker thread is as follows:

[0070] 1) Add a write lock to the current second ready queue of the current level worker thread. In this way, the current second ready queue can only execute the insertion of the timed task to be inserted, preventing multiple timed tasks to be inserted into the current second ready queue at the same time.

[0071] 2) Convert the timing duration of the scheduled task from milliseconds to the number of CPU heartbeats. This involves obtaining the unit heartbeat time corresponding to the CPU frequency and determining the multiple relationship between the timing duration of the scheduled task to be inserted and the unit heartbeat time. This multiple relationship is used to determine the initial number of heartbeats for the scheduled task to be inserted. For example, if the CPU frequency is 2GHz, the time for one heartbeat is 10 milliseconds, meaning the unit heartbeat time is 10 milliseconds. It's important to note that the multiple relationship must be an integer. That is, if the timing duration of the scheduled task to be inserted is 70 milliseconds, the multiple relationship is 7; if the timing duration is 73 milliseconds, the multiple relationship is also 7. Therefore, the initial number of heartbeats for the scheduled task to be inserted is determined to be 7. Here, a timing duration of 70 milliseconds means that the scheduled task should execute 70 milliseconds after insertion, and an initial number of heartbeats of 7 means that the scheduled task should execute 7 heartbeats after the local time of insertion.

[0072] 3) Determine the data structure of the current second ready queue; it is understood that if the data structure of the ready queue is different, the corresponding timed task insertion method will also be different.

[0073] 4.1) If the data structure of the current second ready queue is an array or a queue, then the timed task to be inserted is inserted at the end of the current second ready queue; if the data structure of the current second ready queue is an array or a queue, then the timed task to be inserted does not need to be inserted in the order of task scheduling time.

[0074] 4.2) If the current second ready queue's data structure is a linked list, then the insertion position and updated heartbeat count of the scheduled task to be inserted are determined based on the initial heartbeat count and the heartbeat counts of the scheduled tasks already stored in the current second ready queue, so that the scheduled task to be inserted can be inserted into the current second ready queue; for example... Figure 5 The diagram illustrates a specific task insertion based on heartbeat counts. For example, the second ready queue currently stores scheduled tasks A, B, and C. The current local time is 2. If scheduled task A has a heartbeat count of 1, it means scheduled task A needs to be executed at local time 3. If scheduled task B has a heartbeat count of 3, it means scheduled task B needs to be executed at local time 6. If scheduled task C has a heartbeat count of 1, it means scheduled task A needs to be executed at local time 7. In other words, the execution time of the currently positioned scheduled task is the sum of its own heartbeat count and the heartbeat counts of all preceding scheduled tasks. Therefore, when there is a... When a task to be inserted needs to be added to the current second ready queue, the insertion position and updated heartbeat count of the task are determined based on the stored heartbeat counts of the scheduled tasks. For example, if the initial heartbeat count of scheduled task D is 2, meaning it needs to be executed at local time 4, then the insertion position of scheduled task D is between scheduled tasks A and B. That is, scheduled task D is inserted between scheduled tasks A and B, resulting in scheduled task D. Because of the insertion of scheduled task D, the heartbeat counts of the corresponding scheduled tasks need to be updated. The updated heartbeat counts for scheduled tasks D and B are 1 and 2 respectively. For example, using... Figure 6 The process of determining the insertion position is illustrated using a specific schematic diagram as an example:

[0075] B101: Determine the current timed task according to the order of the timed tasks already stored in the current second ready queue;

[0076] B102: Read the heartbeat count Yn of the current timed task and compare it with the initial heartbeat count X of the timed task to be inserted.

[0077] B103: If Yn is equal to X, then the insertion position of the scheduled task to be inserted is determined to be after the current scheduled task, and the heartbeat count after the scheduled task to be inserted is 0, then jump to B108.

[0078] B104: If Yn is greater than X, it proves that the execution time of the current timed task is later than the execution time of the timed task to be inserted, while the execution time of the previous timed task is earlier than the execution time of the timed task to be inserted. Therefore, the insertion position of the timed task to be inserted can be determined as between the previous timed task and the current timed task. Jump to B108.

[0079] B105: If Yn is less than X, it means that the execution time of the current scheduled task is earlier than the execution time of the scheduled task to be inserted. Calculate the difference R between Yn and X, i.e., R = X - Yn, and determine R as the new heartbeat count. It can be understood that the execution time of each scheduled task is determined by an additive calculation method, so when updating the heartbeat count of the scheduled task to be inserted, it is calculated by a progressively decreasing calculation method.

[0080] B106: If there are other scheduled tasks following the current scheduled task, determine the next scheduled task according to the order of the scheduled tasks already stored in the current second ready queue, and set the next scheduled task as the new current scheduled task, then jump to B102.

[0081] B107: If there is no scheduled task following the current scheduled task, it means that the current scheduled task in the second ready queue is the latest scheduled task to be executed. Since the execution time of the current scheduled task is earlier than the execution time of the scheduled task to be inserted, the position following the current scheduled task can be determined as the insertion position of the scheduled task to be inserted. Jump to B108.

[0082] B108: End the process of determining the insertion position of the scheduled task to be inserted.

[0083] 5) Adjust the status of the task to be inserted into the scheduled task to the queued state and release the write lock on the current second ready queue.

[0084] The beneficial effects of this application are as follows: This application is applied to the current level worker thread, obtains the current global time and the current local time of the next level worker thread, and determines the current time interval between the current global time and the current local time; if the current time interval is not greater than a preset threshold, it determines whether the current global time and the current local time are equal; if they are equal, it obtains the currently updated current global time and the currently updated current local time of the next level worker thread, and jumps back to the step of determining the current time interval between the current global time and the current local time; if they are not equal, it updates the current local time so that the next level worker thread executes the corresponding timed task based on the current local time, and jumps back to the step of determining whether the current global time and the current local time are equal. Therefore, this application is a mutual detection mechanism where the current-level worker thread detects the next-level worker thread, rather than a self-checking mechanism. This avoids the inability to detect due to timeouts, ensuring reliable timeout detection. If the current global time is consistent with the current local time of the next-level worker thread, it means the next-level worker thread is working normally, and no adjustment is needed. If the current global time is inconsistent with the current local time of the next-level worker thread, the current local time of the next-level worker thread is updated so that the next-level worker thread can execute the corresponding scheduled task based on the current local time, ensuring that the scheduled task is scheduled in a timely manner.

[0085] See Figure 7 As shown in the figure, this application discloses a specific timed task scheduling method, applied to the current level worker thread, including:

[0086] Step S21: Obtain the current global time and the current local time of the next level worker thread, and determine the current time interval between the current global time and the current local time.

[0087] Step S22: If the current time interval is greater than a preset threshold, an alarm command is sent to a preset display platform so that the user can stop the execution of all the working threads based on the alarm command displayed on the preset display platform.

[0088] Understandably, if the current time interval is greater than a preset threshold, it means that the next level of worker thread has timed out. For example, if the preset threshold is 80 heartbeats, or 800 milliseconds, if the current time interval is greater than the preset threshold, it can be determined that the next level of worker thread is stuck and the user needs to perform corresponding operations. Therefore, an alarm command (panic) is sent to the preset display platform so that the user can stop the execution of all worker threads based on the alarm command displayed on the preset display platform.

[0089] Step S23: If the current time interval is not greater than the preset threshold, then determine whether the current global time and the current local time are equal.

[0090] Step S24: If they are equal, obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time.

[0091] Step S25: If they are not equal, update the current local time so that the next level worker thread can execute the corresponding timed task based on the current local time, and jump back to the step of determining whether the current global time and the current local time are equal.

[0092] In this embodiment, the next-level worker thread executes the corresponding timed task based on the current local time, including: the next-level worker thread determines whether the local first ready queue is empty; if not, it updates the current heartbeat count of the timed task in the current first ready queue based on the current local time to obtain the current updated heartbeat count of the timed task; if there is a timed task in the current first ready queue with a current updated heartbeat count of zero, it executes the timed task; if not, it updates the current updated heartbeat count of the timed task again. First, determine if there are any scheduled tasks in the current first ready queue. If not, it means there are no scheduled tasks waiting to be executed in the current first ready queue. If there are, it means there are scheduled tasks waiting to be executed in the current first ready queue. Then, update the current heartbeat count of the scheduled tasks in the current first ready queue based on the current local time. In other words, it is necessary to determine whether there are scheduled tasks that need to be executed now in the current first ready queue based on the updated heartbeat count and the current local time, i.e., whether there are scheduled tasks with a heartbeat count of zero after the current update. If there are, then execute the scheduled task.

[0093] In this embodiment, if there is a scheduled task in the current first ready queue whose current updated heartbeat count is zero, then the scheduled task is executed; if not, the current updated heartbeat count of the scheduled task is updated again, including: when the data structure of the current first ready queue is a linked list structure, determining whether the current updated heartbeat count of the first scheduled task in the current first ready queue is zero; if not, updating the current updated heartbeat count of all scheduled tasks in the current first ready queue again; if yes, executing the first scheduled task in the current first ready queue, updating the current first ready queue to obtain the next first ready queue, and using the next first ready queue as the current first ready queue, then jumping back to the step of determining whether the current updated heartbeat count of the first scheduled task in the current first ready queue is zero. It's understandable that, since scheduled tasks are inserted in the order of their execution time, checking if the current updated heartbeat count of the first scheduled task in the current first ready queue is zero determines whether there is a scheduled task that needs to be executed in the current first ready queue. In other words, if the current updated heartbeat count of the first scheduled task is zero, then the current first scheduled task needs to be executed. After the current first scheduled task is executed, the current first ready queue is updated to obtain the next first ready queue, and the next first ready queue is used as the current first ready queue. Then, the process jumps back to the step of checking if the current updated heartbeat count of the first scheduled task in the current first ready queue is zero, until all scheduled tasks that need to be executed have been executed.

[0094] In this embodiment, executing the first scheduled task in the current first ready queue includes: determining the first scheduled task in the current first ready queue as the target scheduled task, and removing the target scheduled task from the current first ready queue; and executing the callback function of the target scheduled task. When executing the target scheduled task A, it is necessary to remove the target scheduled task from the current first ready queue, that is, the next first ready queue will not contain the target scheduled task A, because the callback function of the scheduled task is included when it is created, so executing the callback function completes the scheduling of the target scheduled task.

[0095] Therefore, this application obtains the current time interval between the current global time and the current local time, and determines how to complete the detection of worker threads and the scheduling of timed tasks based on the current time interval, so as to ensure the stability of the scheduling system.

[0096] See Figure 8 As shown in the embodiments of this application, another specific timed task scheduling method is disclosed, applied to the current level worker thread, including:

[0097] Step S31: Obtain the current global time and the current local time of the next level worker thread, and determine the current time interval between the current global time and the current local time.

[0098] Step S32: If the current time interval is not greater than a preset threshold, then determine whether the current global time and the current local time are equal.

[0099] Step S33: If they are equal, obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time.

[0100] Step S34: If they are not equal, the current local time is increased by a preset unit time step to update the current local time, so that the next level worker thread can execute the corresponding timed task based on the current local time, and jump back to the step of judging whether the current global time and the current local time are equal.

[0101] In this embodiment, to achieve high-precision completion of scheduled tasks, if the current global time and the current local time are not equal, and the current time interval is not greater than a preset threshold, then each time the current local time is updated, only a preset unit time step is updated. The preset unit time step can be one heartbeat. For example, if the current local time is 4 and the current global time is 7, updating the current local time to 5 will allow scheduled tasks that need to be executed when the local time is 5 to be executed in the ready queue of the next level worker thread. If the update is not based on the preset unit time step, for example, if the new current local time is 6, then the scheduled tasks that need to be executed when the local time is 5 cannot be executed, and high-precision scheduling of scheduled tasks cannot be achieved.

[0102] Therefore, it can be seen that each time the current local time is updated, the preset unit time step is increased, that is, the update is performed with the smallest update granularity, which ensures that every timed task in the next level worker thread can be executed and there will be no timed tasks that are ignored. In terms of precision, it meets the task precision requirements of millisecond level storage.

[0103] See Figure 9 As shown in the figure, this application discloses a timed task scheduling device, applied to the current level worker thread, including:

[0104] The interval determination module 11 is used to obtain the current global time and the current local time of the next level worker thread, and to determine the current time interval between the current global time and the current local time.

[0105] The time determination module 12 is used to determine whether the current global time and the current local time are equal if the current time interval is not greater than a preset threshold.

[0106] The first jump module 13 is used to obtain the updated current global time and the updated current local time of the next level worker thread if they are equal, and then jump back to the step of determining the current time interval between the current global time and the current local time.

[0107] The second jump module 14 is used to update the current local time if they are not equal, so that the next level worker thread can execute the corresponding timed task based on the current local time and jump back to the step of judging whether the current global time and the current local time are equal.

[0108] The beneficial effects of this application are as follows: This application is applied to the current level worker thread, obtains the current global time and the current local time of the next level worker thread, and determines the current time interval between the current global time and the current local time; if the current time interval is not greater than a preset threshold, it determines whether the current global time and the current local time are equal; if they are equal, it obtains the currently updated current global time and the currently updated current local time of the next level worker thread, and jumps back to the step of determining the current time interval between the current global time and the current local time; if they are not equal, it updates the current local time so that the next level worker thread executes the corresponding timed task based on the current local time, and jumps back to the step of determining whether the current global time and the current local time are equal. Therefore, this application is a mutual detection mechanism where the current-level worker thread detects the next-level worker thread, rather than a self-checking mechanism. This avoids the inability to detect due to timeouts, ensuring reliable timeout detection. If the current global time is consistent with the current local time of the next-level worker thread, it means the next-level worker thread is working normally, and no adjustment is needed. If the current global time is inconsistent with the current local time of the next-level worker thread, the current local time of the next-level worker thread is updated so that the next-level worker thread can execute the corresponding scheduled task based on the current local time, ensuring that the scheduled task is scheduled in a timely manner.

[0109] Furthermore, embodiments of this application also provide an electronic device. Figure 10This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.

[0110] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Specifically, it may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the following steps:

[0111] Obtain the current global time and the current local time of the next-level worker thread, and determine the current time interval between the current global time and the current local time;

[0112] If the current time interval is not greater than a preset threshold, then determine whether the current global time and the current local time are equal;

[0113] If they are equal, then obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time;

[0114] If they are not equal, the current local time is updated so that the next-level worker thread can execute the corresponding timed task based on the current local time, and then jump back to the step of determining whether the current global time and the current local time are equal.

[0115] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0116] If the current time interval is greater than a preset threshold, an alarm command is sent to a preset display platform so that the user can stop the execution of all the working threads based on the alarm command displayed on the preset display platform.

[0117] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0118] The current local time is increased by a preset unit time step to update the current local time.

[0119] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0120] The next-level worker thread determines whether the local first ready queue is empty. If not, it updates the current heartbeat count of the timed task in the current first ready queue based on the current local time to obtain the current updated heartbeat count of the timed task.

[0121] If a scheduled task with a current updated heartbeat count of zero exists in the current first ready queue, then the scheduled task is executed; otherwise, the current updated heartbeat count of the scheduled task is updated again.

[0122] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0123] When the data structure of the current first ready queue is a linked list structure, determine whether the current updated heartbeat count of the first timed task in the current first ready queue is zero;

[0124] If not, then the current updated heartbeat count of all timed tasks in the current first ready queue is updated again;

[0125] If so, the first timed task in the current first ready queue is executed, the current first ready queue is updated to obtain the next first ready queue, and the next first ready queue is used as the current first ready queue. Then, the process jumps back to the step of determining whether the current updated heartbeat count of the first timed task in the current first ready queue is zero.

[0126] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0127] The first scheduled task in the current first ready queue is identified as the target scheduled task, and the target scheduled task is removed from the current first ready queue;

[0128] The callback function for executing the target scheduled task is executed.

[0129] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:

[0130] If there is a scheduled task to be inserted, then the scheduled task to be inserted is inserted into the current second ready queue of the current level worker thread to obtain the next ready queue.

[0131] In some specific embodiments, the processor, by executing a computer program stored in the memory, may further include the following steps:

[0132] Add a write lock to the current second ready queue of the current level worker thread, obtain the unit heartbeat time corresponding to the frequency of the central processing unit, and determine the multiple relationship between the timing duration of the timed task to be inserted and the unit heartbeat time, so as to determine the initial heartbeat number of the timed task to be inserted based on the multiple relationship.

[0133] Determine the data structure of the current second ready queue;

[0134] If the data structure of the current second ready queue is an array or a queue, then the timed task to be inserted is inserted at the tail of the current second ready queue;

[0135] If the data structure of the current second ready queue is a linked list, the insertion position and updated heartbeat count of the timed task to be inserted are determined based on the initial heartbeat count and the heartbeat count of the timed tasks already stored in the current second ready queue, so as to insert the timed task to be inserted into the current second ready queue.

[0136] Adjust the task status of the scheduled task to be inserted to the pending execution status, and release the write lock on the current second ready queue.

[0137] In this embodiment, the power supply 23 is used to provide operating voltage for various hardware devices on the electronic device; the communication interface 24 can create a data transmission channel between the electronic device and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0138] The processor 21 may include one or more processing cores, such as a quad-core processor or an octa-core processor. The processor 21 may be implemented using at least one hardware form selected from DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 21 may also include a main processor and a coprocessor. The main processor, also known as a CPU (Central Processing Unit), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 21 may integrate a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, the processor 21 may also include an AI (Artificial Intelligence) processor, which is used to handle computational operations related to machine learning.

[0139] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or optical disk, etc. The resources stored on it include operating system 221, computer program 222 and data 223, etc., and the storage method can be temporary storage or permanent storage.

[0140] The operating system 221 manages and controls the various hardware devices and computer programs 222 on the electronic device to enable the processor 21 to perform calculations and processing on the massive amounts of data 223 in the memory 22. The operating system can be Windows, Unix, Linux, etc. The computer program 222, in addition to including a computer program capable of performing the timed task scheduling method disclosed in any of the foregoing embodiments, may further include computer programs capable of performing other specific tasks. The data 223 may include data received by the electronic device from external devices, as well as data collected by its own input / output interface 25.

[0141] Furthermore, embodiments of this application also disclose a computer-readable storage medium storing a computer program. When the computer program is loaded and executed by a processor, it implements the method steps executed during the timed task scheduling process disclosed in any of the foregoing embodiments.

[0142] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0143] The above provides a detailed description of a timed task scheduling method, apparatus, device, and medium provided by the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.

Claims

1. A method for scheduling timed tasks, characterized in that, Applies to worker threads at the current level, including: Obtain the current global time and the current local time of the next-level worker thread, and determine the current time interval between the current global time and the current local time; If the current time interval is not greater than a preset threshold, then determine whether the current global time and the current local time are equal; If they are equal, then obtain the updated current global time and the updated current local time of the next level worker thread, and jump back to the step of determining the current time interval between the current global time and the current local time; If they are not equal, the current local time is updated so that the next level worker thread can execute the corresponding timed task based on the current local time, and then jump back to the step of determining whether the current global time and the current local time are equal. The next-level worker thread executes the corresponding scheduled task based on the current local time, including: The next-level worker thread determines whether the local first ready queue is empty. If not, it updates the current heartbeat count of the timed tasks in the current first ready queue based on the current local time to obtain the current updated heartbeat count of the timed tasks. If there is a timed task in the current first ready queue with a current updated heartbeat count of zero, the timed task is executed. If not, the current updated heartbeat count of the timed task is updated again.

2. The timed task scheduling method according to claim 1, characterized in that, After determining the current time interval between the current global time and the current local time, the method further includes: If the current time interval is greater than a preset threshold, an alarm command is sent to a preset display platform so that the user can stop the execution of all the working threads based on the alarm command displayed on the preset display platform.

3. The timed task scheduling method according to claim 1, characterized in that, The update of the current local time includes: The current local time is increased by a preset unit time step to update the current local time.

4. The timed task scheduling method according to claim 1, characterized in that, If there is a scheduled task in the current first ready queue whose heartbeat count is zero after the current update, then the scheduled task is executed. If it does not exist, the current updated heartbeat count of the scheduled task will be updated again, including: When the data structure of the current first ready queue is a linked list structure, determine whether the current updated heartbeat count of the first timed task in the current first ready queue is zero; If not, then the current updated heartbeat count of all timed tasks in the current first ready queue is updated again; If so, the first timed task in the current first ready queue is executed, the current first ready queue is updated to obtain the next first ready queue, and the next first ready queue is used as the current first ready queue. Then, the process jumps back to the step of determining whether the current updated heartbeat count of the first timed task in the current first ready queue is zero.

5. The timed task scheduling method according to claim 4, characterized in that, Executing the first scheduled task in the current first ready queue includes: The first scheduled task in the current first ready queue is identified as the target scheduled task, and the target scheduled task is removed from the current first ready queue; The callback function for executing the target scheduled task is executed.

6. The timed task scheduling method according to any one of claims 1 to 5, characterized in that, Also includes: If there is a scheduled task to be inserted, then the scheduled task to be inserted is inserted into the current second ready queue of the current level worker thread to obtain the next ready queue.

7. The timed task scheduling method according to claim 6, characterized in that, The step of inserting the scheduled task to be inserted into the current second ready queue of the current level worker thread includes: Add a write lock to the current second ready queue of the current level worker thread, obtain the unit heartbeat time corresponding to the frequency of the central processing unit, and determine the multiple relationship between the timing duration of the timed task to be inserted and the unit heartbeat time, so as to determine the initial heartbeat number of the timed task to be inserted based on the multiple relationship. Determine the data structure of the current second ready queue; If the data structure of the current second ready queue is an array or a queue, then the timed task to be inserted is inserted at the tail of the current second ready queue; If the data structure of the current second ready queue is a linked list, the insertion position and updated heartbeat count of the timed task to be inserted are determined based on the initial heartbeat count and the heartbeat count of the timed tasks already stored in the current second ready queue, so as to insert the timed task to be inserted into the current second ready queue. Adjust the task status of the scheduled task to be inserted to the pending execution status, and release the write lock on the current second ready queue.

8. A timed task scheduling device, characterized in that, Applies to worker threads at the current level, including: An interval determination module is used to obtain the current global time and the current local time of the next-level worker thread, and to determine the current time interval between the current global time and the current local time. The time determination module is used to determine whether the current global time and the current local time are equal if the current time interval is not greater than a preset threshold. The first jump module is used to, if they are equal, obtain the currently updated global time and the currently updated local time of the next level worker thread, and then jump back to the step of determining the current time interval between the current global time and the current local time; The second jump module is used to update the current local time if they are not equal, so that the next level worker thread can execute the corresponding timed task based on the current local time and jump back to the step of judging whether the current global time and the current local time are equal. The second jump module is specifically used for: The next-level worker thread determines whether the local first ready queue is empty. If not, it updates the current heartbeat count of the timed tasks in the current first ready queue based on the current local time to obtain the current updated heartbeat count of the timed tasks. If there is a timed task in the current first ready queue with a current updated heartbeat count of zero, the timed task is executed. If not, the current updated heartbeat count of the timed task is updated again.

9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the steps of the timed task scheduling method as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, Used to store computer programs; wherein, when the computer programs are executed by a processor, they implement the steps of the timed task scheduling method as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • Thread monitoring method and system, equipment and medium

    CN112631858A

  • System and method for scheduling tasks for execution

    US20070050771A1