CN115509700A - Multi-type task management method and device - Google Patents
Multi-type task management method and device Download PDFInfo
- Publication number
- CN115509700A CN115509700A CN202110694107.0A CN202110694107A CN115509700A CN 115509700 A CN115509700 A CN 115509700A CN 202110694107 A CN202110694107 A CN 202110694107A CN 115509700 A CN115509700 A CN 115509700A
- Authority
- CN
- China
- Prior art keywords
- task
- time
- queue
- executing
- tasks
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000007726 management method Methods 0.000 title claims abstract description 49
- 238000000034 method Methods 0.000 claims abstract description 36
- 230000000737 periodic effect Effects 0.000 claims description 122
- 230000006870 function Effects 0.000 claims description 71
- 238000012545 processing Methods 0.000 claims description 22
- 230000008569 process Effects 0.000 claims description 16
- 238000004590 computer program Methods 0.000 claims description 11
- 238000001514 detection method Methods 0.000 claims description 10
- 230000002159 abnormal effect Effects 0.000 claims description 7
- 238000007639 printing Methods 0.000 claims description 4
- 230000005856 abnormality Effects 0.000 description 10
- 238000010586 diagram Methods 0.000 description 6
- 238000004891 communication Methods 0.000 description 5
- 238000003672 processing method Methods 0.000 description 5
- 238000005457 optimization Methods 0.000 description 4
- 230000009467 reduction Effects 0.000 description 3
- 230000008878 coupling Effects 0.000 description 2
- 238000010168 coupling process Methods 0.000 description 2
- 238000005859 coupling reaction Methods 0.000 description 2
- 230000003287 optical effect Effects 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 239000000284 extract Substances 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000012544 monitoring process Methods 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
- 230000001960 triggered effect Effects 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4843—Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
- G06F9/4881—Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
Abstract
The invention provides a multi-type task management method and a device, wherein the method comprises the following steps: determining a target task type; executing a target task corresponding to the target task type based on a task scheduling manager; wherein the task scheduling manager provides: the device comprises a first list, a first queue, a second queue, an interface, a first function and a second function. The invention executes the target task corresponding to the target task type by determining the target task type and based on the interface, the corresponding list, the queue and the function provided by the task scheduling manager, thereby realizing the unified management of the multi-type tasks, improving the usability and ensuring the more stable and reliable execution of the multi-type tasks.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a multi-type task management method and device.
Background
An Automatic Train Supervision (ATS) system is a monitoring system applied to a subway line, and includes various types of tasks, such as a timing task, a periodic task, or a one-time task.
At present, a processing method for a timing task is to set a time interval from a current time of a system to a target time for executing the timing task by using a timer, and the timer executes the timing task after the timer counts the time interval, which affects the accuracy of executing the timing task; or, a timer is used to set a small time interval, and periodically compares the time interval with the current time of the system, and the timing task is executed when the time counted by the timer reaches the target time of the timing task, which wastes a lot of extra Central Processing Unit (CPU) time.
The processing method for the periodic tasks is characterized in that a multi-thread timer is used for setting periodic time intervals, the periodic tasks are executed under the condition that the timer times to reach the periodic time intervals, and the mode causes that the front and back periodic tasks with dependency cannot be normally executed; or, a single exclusive thread is used for executing the periodic task, the current thread is blocked after the periodic task is executed each time, the periodic task is continuously executed after a periodic time interval, and the mode causes the resource supply and short-supply of the system thread; or, a periodic task for updating a UI is implemented by using a User Interface (UI) timer through a message queue, which may result in that the periodic task cannot be accurately executed.
The processing method for the disposable task is to start a new thread to execute the disposable task, or to utilize a thread pool thread to execute the disposable task, which causes a large amount of repeated codes to appear when the performance of the disposable task is detected or the journal is recorded, and the coupling degree is high, which is not beneficial to maintaining the program.
Therefore, optimization and unified management of the execution methods of the multi-type tasks cannot be achieved in the related art.
Disclosure of Invention
The invention provides a multi-type task management method and device, which are used for solving the problem that the optimization and unified management of the execution method of the multi-type task cannot be realized in the related technology.
In a first aspect, the present invention provides a multi-type task management method, including:
determining a target task type;
executing a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking the periodic task out of the first queue, and the second function is used for taking the one-time task out of the second queue.
Optionally, the executing the target task corresponding to the target task type based on the task scheduling manager specifically includes:
under the condition that the target task is a timing task, storing the timing task to the first list through the interface, and executing the timing task;
under the condition that the target task is a periodic task, the periodic task is stored to the first queue through the interface, the first function is called to take out the periodic task, and the periodic task is executed;
and under the condition that the target task is a one-time task, storing the one-time task to the second queue through the interface, calling the second function to take out the one-time task, and executing the one-time task.
Optionally, the executing the timing task specifically includes:
starting a timer, and executing an Elapsed event of the timer when the timer times to reach a time interval of the timer;
wherein the Elapsed event executing the timer comprises:
stopping the timer, and calculating the remaining time for executing the timed task based on the target execution time and the current time of the timed task;
judging whether the residual time is less than a preset time interval or not, if the residual time is less than the preset time interval and greater than zero, asynchronously waiting for the residual time and then executing the timed task, and after the execution of the timed task is finished, exiting the execution of an Elapsed event of the timer; or,
and if the remaining time is greater than the preset time interval, updating the time interval of the timer based on a reduced proportion, starting the timer, and continuing to execute the Elapsed event of the timer when the timer times to reach the time interval of the updated timer.
Optionally, the executing the periodic task specifically includes:
allocating an ith thread to the periodic task based on a background thread pool;
executing the periodic task by utilizing the ith thread in the ith period time, wherein i is a natural number which is more than or equal to 1;
and if the periodic task is not cancelled, asynchronously waiting for a preset execution period, and starting the task execution of the next period after the waiting is finished.
Optionally, the executing the one-time task specifically includes:
calculating the parallelism of the one-time task based on the CPU logic core number of the central processing unit;
judging whether the number of the remaining tasks in the second queue is larger than the parallelism degree, and if the number of the remaining tasks in the second queue is larger than the parallelism degree, taking out a plurality of tasks of which the number is equal to the parallelism degree from the second queue; otherwise, taking out all the tasks left in the second queue;
and executing a plurality of taken-out tasks in parallel by using a plurality of threads in the background thread pool, wherein one thread correspondingly executes one task.
Optionally, the performing the one-time task further comprises at least one of:
in the process of executing a plurality of taken-out tasks in parallel by using a plurality of pool threads in the background threads, performing waiting operation and abnormality detection operation on the execution of each task, and if the execution time exceeds a first preset threshold or an abnormality occurs, performing log recording;
after the execution of all tasks is finished, acquiring the total time of executing and taking out a plurality of tasks in parallel, and if the total time is greater than or equal to a second preset threshold, performing log recording;
and printing the residual task number of the second queue and the use condition of the background thread pool.
In a second aspect, the present invention further provides a multi-type task management apparatus, including:
the setting unit is used for determining the type of the target task;
and the execution unit is used for executing the target task corresponding to the target task type based on the task scheduling manager.
Optionally, the execution unit specifically includes:
the first execution subunit is configured to, when the target task is a timing task, store the timing task to the first list through the interface, and execute the timing task;
the second execution subunit is configured to, when the target task is a periodic task, store the periodic task to the first queue through the interface, call the first function to take out the periodic task, and execute the periodic task;
and the third execution subunit is configured to, when the target task is a one-time task, store the one-time task to the second queue through the interface, call the second function to take out the one-time task, and execute the one-time task.
In a third aspect, the present invention further provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the multi-type task management method according to the first aspect when executing the program.
In a fourth aspect the present invention also provides a non-transitory computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of the multi-type task management method according to the first aspect.
According to the multi-type task management method and device, the target task corresponding to the target task type is executed by determining the target task type and based on the interface, the corresponding list, the corresponding queue and the function provided by the task scheduling manager, so that unified management of the multi-type task is realized, the usability is improved, and the execution of the multi-type task is more stable and reliable.
Drawings
In order to more clearly illustrate the technical solutions of the present invention or the prior art, the drawings needed for the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and those skilled in the art can also obtain other drawings according to the drawings without creative efforts.
FIG. 1 is a flow chart diagram of a multi-type task management method provided by the present invention;
FIG. 2 is a flow chart illustrating a method for processing a timed task according to the present invention;
FIG. 3 is a flow chart illustrating a method for processing periodic tasks according to the present invention;
FIG. 4 is a flow chart diagram of a method for processing a one-time task according to the present invention;
FIG. 5 is a schematic structural diagram of a multi-type task management device provided by the present invention;
fig. 6 is a schematic structural diagram of an electronic device provided in the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings, and it is obvious that the described embodiments are some, but not all embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In order to solve the problem that optimization and unified management cannot be performed on execution methods of multiple types of tasks in the related art, embodiments of the present application provide a multiple types of task management method, which can be applied to an ATS system, where the ATS system includes a task scheduling manager, and the task scheduling manager is used to implement unified management and optimization of the ATS system on the multiple types of tasks.
Fig. 1 is a flowchart illustrating a multi-type task management method according to an embodiment of the present invention. As shown in fig. 1, the multi-type task management method provided by the present invention includes:
and step 100, determining the type of the target task.
Optionally, the target task type includes at least one of a timed task, a periodic task, and a one-time task.
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking out the periodic task from the first queue, and the second function is used for taking out the one-time task from the second queue.
In the embodiment of the invention, in order to realize unified management on the multi-type tasks, improve the usability, monitor the system thread resources required by executing the multi-type tasks and ensure that the execution of the multi-type tasks is more stable and reliable, a task scheduling manager is designed in an ATS system.
And the task scheduling manager is used for instructing the ATS system to perform scheduling management on the multi-type tasks.
The task scheduling manager provides a unified interface for the execution of the multi-type tasks, and the unified interface is used for adding the multi-type tasks to a corresponding list or queue in the task scheduling manager to realize the storage of the multi-type tasks.
The task scheduling manager defines a first queue and a buffer queue, and the buffer queue comprises a first queue and a second queue.
The first list is used for storing a plurality of timing tasks, the first queue is used for storing a plurality of periodic tasks, and the second queue is used for storing a plurality of one-time tasks.
Optionally, the buffer queue is structured as a sequential queue or a circular queue.
It should be noted that, the task scheduling manager implements unified management on multiple types of tasks through the first list and the cache queue, for example, if the timed task a is cancelled, the task scheduling manager may directly delete the timed task a from the first list, and then cancel execution of the timed task a.
Further, the task scheduling manager defines a first function for fetching periodic tasks from the first queue and a second function for fetching one-time tasks from the second queue.
In one embodiment, the ATS system schedules and manages a target task corresponding to a target task type based on a task scheduling manager, and executes the target task.
According to the multi-type task management method provided by the embodiment of the invention, the ATS system determines the type of the target task, and executes the target task corresponding to the type of the target task based on the interface, the corresponding list, the corresponding queue and the function provided by the task scheduling manager, so that the unified management of the multi-type task is realized, the usability is improved, and the execution of the multi-type task is more stable and reliable.
Based on the content of the foregoing embodiment, the executing, by the task scheduling manager, the target task corresponding to the target task type specifically includes:
under the condition that the target task is a timing task, storing the timing task to the first list through the interface, and executing the timing task;
under the condition that the target task is a periodic task, storing the periodic task to the first queue through the interface, calling the first function to take out the periodic task, and executing the periodic task;
and under the condition that the target task is a one-time task, storing the one-time task to the second queue through the interface, calling the second function to take out the one-time task, and executing the one-time task.
The timed task means a task executed when the target time point is reached. For example, a task of reading an operation plan four points each morning belongs to a timing task.
The periodic task means a task repeatedly executed based on a preset period. For example, a task of acquiring system operation log information every 1 hour belongs to a periodic task.
A one-time task means a plurality of tasks that are executed in parallel once in a short time. For example, traffic data of a plurality of network devices are acquired respectively.
Optionally, the task is fetched from the first queue and the second queue in a first-in-first-out or first-in-last-out manner.
It should be noted that, the first function independently starts an exclusive thread with a higher priority, periodically fetches a periodic task from the first queue, the second function independently starts an exclusive thread with a higher priority, periodically fetches a one-time task from the second queue, and independently starts an exclusive thread with a higher priority for the first function and the second function, so that the situation that the first function or the second function cannot run when an exception occurs during task scheduling processing can be effectively avoided.
In one embodiment, when determining that the target task is a timed task, the task scheduling manager stores the acquired timed task to a timed task list through an interface, and immediately starts and executes the timed task.
In one embodiment, when the task scheduling manager determines that the target task is a periodic task, the task scheduling manager stores the acquired periodic task into a first queue through an interface, calls a first function to take out the periodic task from the first queue, and then executes the periodic task.
In one embodiment, under the condition that the target task is determined to be the one-time task, the task scheduling manager stores the acquired one-time task to the second queue through the interface, calls the second function to take out the one-time task from the second queue, and then executes the one-time task.
According to the multi-type task management method provided by the embodiment of the invention, the task scheduling manager stores the target tasks to the corresponding lists or queues through the interfaces based on the target task types to execute the target tasks, so that the unified management of the multi-type tasks is realized, the usability is improved, and the execution of the multi-type tasks is more stable and reliable.
Based on the content of the foregoing embodiment, the executing the timing task specifically includes:
starting a timer, and executing an Elapsed event of the timer when the timer times to reach a time interval of the timer;
wherein the Elapsed event executing the timer comprises:
stopping the timer, and calculating the remaining time for executing the timed task based on the target execution time and the current time of the timed task;
judging whether the residual time is smaller than a preset time interval or not, if so, asynchronously waiting for the residual time and then executing the timed task, and after the timed task is executed, exiting the execution of the Elapsed event of the timer; or,
and if the remaining time is greater than the preset time interval, updating the time interval of the timer based on a reduced proportion, starting the timer, and continuing to execute the Elapsed event of the timer when the timer times to reach the time interval of the updated timer.
The timed task comprises a target execution time t d Residual time t, preset time interval delta t, reduction ratio sigma, actual execution task and timer.
The timing task may be one or more timing tasks.
The target execution time represents a target time point at which the timed task is executed.
The remaining time represents the interval from the current time of the system to the target execution time. The current time of the system is denoted t 0 Then time t = t remains d -t 0 。
The reduction ratio is used to update the time interval of the timer, wherein the reduction ratio sigma satisfies 0 < sigma ≦ 0.5.
The timer comprises a time interval, and in the case that the timer times the time interval of the timer, the Elapsed event processing function is triggered.
The Elapsed event processing function is used for optimizing the execution process of the timing task.
In one embodiment, if the time interval of the timer is Δ T, the time interval of the updated timer is Δ T × σ.
In one embodiment, after the timing task is executed, the next target execution time of the timing task is recalculated, the remaining time for executing the timing task is calculated based on the current time of the system and the next target execution time, the initial time interval of the timer is set according to the remaining time, the timer is started, and the Elapsed event processing function of the timer is executed when the timer reaches the initial time interval of the timer.
For example, the timing task is a four-point reading operation plan every morning, after the four-point execution of the timing task is finished today, the current time of the system is four points in the morning today, the next target execution time of the timing task is four points in the tomorrow morning, when the next target execution time executes the timing task, the remaining time for executing the timing task is 24 hours, the initial time interval of the timer is set to be 12 hours according to the remaining time, the timer is started, and when the timer reaches 12 hours, the Elapsed event processing function of the timer is executed.
In one embodiment, a background thread pool thread is called to start a timer, and when the timer reaches Δ T, an Elapsed event handling function of the timer is executed, where the function is executed by:
and 200, stopping the timer, and calculating the residual time t of the timing task based on the target execution time of the timing task and the current time of the system.
Step 201, judging whether the remaining time is less than a preset time interval, and if t is less than or equal to 0, executing step 202; if t is more than 0 and less than or equal to Δ t, go to step 203, if t is more than or equal to Δ t 1 At, step 204 is performed.
Step 202, executing the timing task, and then executing step 205.
And step 203, after asynchronously waiting for the remaining time t, executing the timing task, and then executing step 205.
Step 204, updating Δ T to Δ T σ, starting the timer, and repeating step 200 when the timer reaches Δ T σ.
Step 205, recalculating the next target execution time of the timed task, updating the time interval of the timer, starting the timer, and executing step 200 according to the next target execution time of the timer when the timer reaches the time interval of the updated timer.
According to the multi-type task management method provided by the embodiment of the invention, under the condition that the timing of the timer corresponding to the timing task reaches the time interval of the timer, the Elapsed event processing function of the timer is executed, the function stops the timer, the residual time of the timing task is compared with the preset time interval, the time interval of the timer is updated according to the comparison result, and the residual time of the timing task and the preset time interval are repeatedly judged in a circulating manner until the timing task is executed.
The processing flow of the timing task is specifically described with reference to fig. 2. Fig. 2 is a flowchart illustrating a timing task processing method according to an embodiment of the present invention. As shown in fig. 2, the method for processing the timing task includes the following steps:
s1, triggering a processing function after a timer times to reach a time interval;
s2, stopping the timer;
s3, calculating the remaining time;
s4, if the residual time is less than or equal to 0, executing a step S8;
s5, if the residual time is more than 0 and less than or equal to a preset time interval delta t, executing a step S7;
s6, if the residual time is greater than the preset time interval delta t, executing the step S10;
s7, delaying the remaining time;
s8, executing a timing task;
s9, calculating the next target execution time of the timing task, and updating the remaining time for executing the timing task next time;
s10, updating a time interval of the timer, wherein the time interval = remaining time × (reduced scale);
and S11, starting a timer to time, and repeatedly executing the step S1.
Based on the content of the foregoing embodiment, the executing the periodic task specifically includes:
allocating an ith thread to the periodic task based on a background thread pool;
executing the periodic task by utilizing the ith thread in the ith period time, wherein i is a natural number which is more than or equal to 1;
and if the periodic task is not cancelled, asynchronously waiting for a preset execution period, and starting the task execution of the next period after the waiting is finished.
The periodic tasks include a preset execution period T and actual execution tasks.
It should be noted that the task execution process in the next cycle is: allocating an (i + 1) th thread to a periodic task based on a background thread pool, executing the periodic task by using the (i + 1) th thread within the (i + 1) th period time, if the periodic task does not cancel the asynchronous waiting for the preset execution period T, restarting the task execution within the (i + 2) th period after the waiting is finished, and circulating the previous steps until the periodic task is cancelled, and finishing the execution of the periodic task.
It should be noted that the ith thread and the (i + 1) th thread may be the same or different.
In one embodiment, if the periodic task is cancelled, the execution of the periodic task is finished.
In one embodiment, after the first function extracts the periodic task from the first queue, the specific steps of executing the periodic task include:
step 300, distributing the ith thread to a periodic task based on a background thread pool;
step 301, in the ith cycle time, the periodic task is executed by the ith thread.
Step 302, judging whether the periodic task is cancelled, and executing step 303 under the condition that the periodic task is not cancelled; in the case of periodic task cancellation, step 404 is performed.
Step 303, asynchronously waiting for a preset execution period T, and after the waiting is finished, repeatedly executing step 300 within the (i + 1) th cycle time.
And step 304, finishing executing the periodic task.
According to the multi-type task management method provided by the embodiment of the invention, in each period time, under the condition that the periodic task is not cancelled, the background thread pool is used for reallocating the threads for the periodic task for executing the periodic task, the method optimizes the processing process of the periodic task, saves the use of processor resources, ensures that the execution time of the front periodic task and the execution time of the rear periodic task are not overlapped, and improves the accuracy of executing the periodic task.
The processing flow of the periodic task is specifically described with reference to fig. 3. Fig. 3 is a flowchart illustrating a periodic task processing method according to an embodiment of the present invention.
As shown in fig. 3, the task scheduling manager stores periodic tasks including task 0, task 1, and task n into the first queue, and calls a first function to fetch the periodic tasks from the first queue.
Further, a thread is distributed to the periodic tasks based on the background thread pool, the thread is used for executing the periodic tasks, after the execution is finished, whether the periodic tasks are cancelled or not is judged, and if the periodic tasks are cancelled, the execution of the periodic tasks is finished; and if the periodic task is not cancelled, after asynchronously waiting for a preset execution period, reallocating a thread to the periodic task based on the background thread pool, wherein the thread is used for executing the periodic task, and after the execution is finished, repeating the step of judging whether the periodic task is cancelled or not.
Based on the content of the above embodiment, the executing the one-time task specifically includes:
calculating the parallelism of the one-time task based on the logic core number of a Central Processing Unit (CPU);
judging whether the number of the remaining tasks in the second queue is larger than the parallelism degree, and if the number of the remaining tasks in the second queue is larger than the parallelism degree, taking out a plurality of tasks of which the number is equal to the parallelism degree from the second queue; otherwise, taking out all the tasks left in the second queue;
and executing a plurality of taken tasks in parallel by using a plurality of threads in the background thread pool, wherein one thread executes one task correspondingly.
Parallelism is used to indicate the maximum number of tasks to execute in parallel.
In one embodiment, the parallelism of a one-time task is calculated based on the number of logical cores of the CPU using equation (1):
wherein N is P Representing the degree of parallelism of a one-time task, N C Represents the number of CPU logic cores, and N P ≥4。
For example, the number of CPU logic cores is 16, and the parallelism of the one-time task is 11.
Note that the plurality of tasks fetched from the second queue represents one-time tasks.
In one embodiment, prior to fetching the one-time task, the parallelism of the one-time task is calculated using equation (1) based on the number of CPU logical cores.
Further, the task call manager calls a second function to take out a plurality of tasks with the number equal to the parallelism from the second queue under the condition that the number of the remaining tasks in the second queue is larger than the parallelism; in the event that the remaining tasks in the second queue are proficient less than or equal to the degree of parallelism, a second function is invoked to fetch all tasks from the second queue.
Further, for a plurality of tasks, a plurality of threads are distributed to the plurality of tasks based on the background thread pool, the plurality of threads are utilized to execute the plurality of tasks in parallel, and each thread correspondingly executes one task.
The multi-type task management method provided by the embodiment of the invention calculates the parallelism of the one-time task, compares the number of the remaining tasks in the second queue with the parallelism, determines the number of the plurality of tasks, and executes the plurality of tasks in parallel by using the background thread pool, thereby realizing the execution of the one-time task with the parallelism, preventing the CPU from using a saturation-induced halt event of the CPU, and optimizing the processing process of the one-time task.
Based on the content of the above embodiment, the performing the one-time task further includes at least one of:
step 400, in the process of executing a plurality of taken-out tasks in parallel by using a plurality of pool threads in the background threads, executing a waiting operation and an abnormality detection operation on each task, and if the execution time exceeds a first preset threshold value or an abnormality occurs, performing log recording.
The waiting operation is used for indicating that the execution time of each task is respectively counted in the process of executing a plurality of tasks.
And the abnormality detection operation is used for indicating that abnormality detection is carried out on the execution process of each task in the process of executing a plurality of tasks.
In one embodiment, in the process of executing a plurality of tasks in parallel by using a plurality of threads, a background thread pool thread executes a waiting operation on each task, and if the time for executing the waiting operation exceeds a first preset threshold, a first log is recorded.
In one embodiment, in the process of executing a plurality of tasks in parallel by using a plurality of threads, a background thread pool thread executes an abnormality detection operation on each task, and if the task is abnormal, a second log is recorded.
The first log is used for indicating that when the waiting operation is executed for each task, the time for the task to execute the waiting operation exceeds a first preset threshold value.
The second log is used for indicating that each task is abnormal when the abnormal detection operation is executed on the task.
Step 401, after the execution of all the tasks is finished, obtaining the total time for executing the plurality of tasks taken out in parallel, and if the total time is greater than or equal to a second preset threshold, performing log recording.
Alternatively, the second preset threshold may be 10 seconds.
In one embodiment, after the plurality of tasks are executed in parallel by the plurality of threads, the total time for executing the plurality of tasks in parallel is calculated, and the total time and the second preset threshold value are judged.
Further, in the case that the total time is greater than or equal to a second preset threshold, a third log is recorded.
And the third log is used for indicating that the total time for executing the plurality of tasks in parallel by using the plurality of threads is greater than or equal to a second preset threshold value.
And 402, printing the residual task number of the second queue and the use condition of the background thread pool.
In one embodiment, the task scheduling manager prints the remaining number of tasks in the second queue and the thread usage in the current background thread pool of the system when the multiple threads are used in the background thread pool to execute multiple tasks in parallel.
According to the multi-type task management method provided by the embodiment of the invention, in the process of executing a plurality of tasks in parallel by using the background thread pool, the waiting operation and the abnormal detection operation are executed on the plurality of tasks by using the background thread pool thread, the log is recorded, after the execution of all the tasks is finished, the total time for executing all the tasks in parallel is obtained, the log is recorded, and the number of the remaining tasks in the second queue and the use condition of the current background thread pool of the system are printed at the same time, so that the execution condition of the plurality of tasks is timely monitored in a mode of transversely cutting a focus, the code coupling degree is reduced, the abnormal condition is conveniently diagnosed, and the maintainability of a program is improved.
The processing flow of the disposable task is specifically described with reference to fig. 4. Fig. 4 is a flowchart illustrating a method for processing a disposable task according to an embodiment of the present invention.
As shown in fig. 4, the task scheduling manager stores the one-time tasks including task 0, task 1, and task n into the second queue, and the task scheduling manager takes the one-time tasks out of the second queue using a second function.
Further, a number of one-time tasks equal to the parallelism, which is p, are taken out from the second queue, and the one-time tasks include task i, task i +1,. And task i + p.
And further, distributing a plurality of threads to the disposable tasks by using a background thread pool, wherein each thread corresponds to each task one by one, the plurality of threads execute the disposable tasks in parallel, and in the process of executing the disposable tasks, the plurality of threads count the parallel execution of the disposable tasks, perform abnormality detection operation on the disposable tasks, and record logs.
The following describes the multi-type task management device provided by the present invention, and the multi-type task management device described below and the multi-type task management method described above may be referred to in correspondence with each other.
FIG. 5 is a diagram of a multi-type task management device according to an embodiment of the present invention. As shown in fig. 5, the multi-type task management device includes: a determination unit 500 and an execution unit 510, wherein,
a determining unit 500, configured to determine a target task type;
an executing unit 510, configured to execute a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking out the periodic task from the first queue, and the second function is used for taking out the one-time task from the second queue.
According to the multi-type task management method and device, the target task corresponding to the target task type is executed by determining the target task type and based on the interface, the corresponding list, the corresponding queue and the function provided by the task scheduling manager, so that unified management of the multi-type task is realized, the usability is improved, and the execution of the multi-type task is more stable and reliable.
Optionally, the execution unit 510 specifically includes:
the first execution subunit is configured to, when the target task is a timing task, store the timing task to the first list through the interface, and execute the timing task;
the second execution subunit is configured to, when the target task is a periodic task, store the periodic task to the first queue through the interface, call the first function to take out the periodic task, and execute the periodic task;
and the third execution subunit is configured to, when the target task is a one-time task, store the one-time task to the second queue through the interface, call the second function to take out the one-time task, and execute the one-time task.
Optionally, the first execution subunit is specifically configured to:
starting a timer, and executing an Elapsed event of the timer when the timer times to reach a time interval of the timer;
wherein the Elapsed event executing the timer comprises:
stopping the timer, and calculating the remaining time for executing the timed task based on the target execution time and the current time of the timed task;
judging whether the residual time is smaller than a preset time interval or not, if so, asynchronously waiting for the residual time and then executing the timed task, and after the timed task is executed, exiting the execution of the Elapsed event of the timer; or,
if the remaining time is larger than the preset time interval, updating the time interval of the timer based on a reduced proportion, starting the timer, and continuously executing an Elapsed event of the timer under the condition that the time of the timer reaches the time interval of the updated timer.
Optionally, the second execution subunit is specifically configured to:
allocating an ith thread to the periodic task based on a background thread pool;
executing the periodic task by utilizing the ith thread in the ith period time, wherein i is a natural number which is more than or equal to 1;
and if the periodic task is not cancelled, asynchronously waiting for a preset execution period, and starting the task execution of the next period after the waiting is finished.
Optionally, the third execution subunit is specifically configured to:
calculating the parallelism of the one-time task based on the logic core number of a Central Processing Unit (CPU);
judging whether the number of the remaining tasks in the second queue is larger than the parallelism degree, and if the number of the remaining tasks in the second queue is larger than the parallelism degree, taking out a plurality of tasks of which the number is equal to the parallelism degree from the second queue; otherwise, taking out all the tasks left in the second queue;
and executing a plurality of taken tasks in parallel by using a plurality of threads in the background thread pool, wherein one thread executes one task correspondingly.
Optionally, the third execution subunit is specifically configured to at least one of:
in the process of executing a plurality of taken-out tasks in parallel by using a plurality of pool threads in the background threads, performing waiting operation and abnormality detection operation on the execution of each task, and if the execution time exceeds a first preset threshold or an abnormality occurs, performing log recording;
after the execution of all tasks is finished, acquiring the total time of the plurality of tasks which are taken out in parallel, and if the total time is greater than or equal to a second preset threshold, performing log recording;
and printing the residual task number of the second queue and the use condition of the background thread pool.
The multi-type task management device provided by the invention can realize the processes realized by the method embodiments of fig. 1 to fig. 4, and achieve the same technical effects, and is not repeated herein to avoid repetition.
Fig. 6 is a schematic structural diagram of an electronic device provided in the present invention, and as shown in fig. 6, the electronic device may include: a processor (processor) 610, a communication interface (communication interface) 620, a memory (memory) 630 and a communication bus 640, wherein the processor 610, the communication interface 620 and the memory 630 are communicated with each other via the communication bus 640. The processor 610 may call logical instructions in the memory 630 to perform a multi-type task management method comprising:
determining a target task type;
executing a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking out the periodic task from the first queue, and the second function is used for taking out the one-time task from the second queue.
In addition, the logic instructions in the memory 630 may be implemented in software functional units and stored in a computer readable storage medium when the logic instructions are sold or used as independent products. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-only memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk.
In another aspect, the present invention also provides a computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions which, when executed by a computer, enable the computer to perform a multi-type task management method provided by the above methods, the method comprising:
determining a target task type;
executing a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking the periodic task out of the first queue, and the second function is used for taking the one-time task out of the second queue.
In yet another aspect, the present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored, the computer program being implemented by a processor to perform the multi-type task management method provided by the above embodiments, the method including:
determining a target task type;
executing a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking out the periodic task from the first queue, and the second function is used for taking out the one-time task from the second queue.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment may be implemented by software plus a necessary general hardware platform, and may also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, and not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.
Claims (10)
1. A multi-type task management method, comprising:
determining a target task type;
executing a target task corresponding to the target task type based on a task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking the periodic task out of the first queue, and the second function is used for taking the one-time task out of the second queue.
2. The method according to claim 1, wherein the executing the target task corresponding to the target task type based on the task scheduling manager specifically comprises:
under the condition that the target task is a timing task, storing the timing task to the first list through the interface, and executing the timing task;
under the condition that the target task is a periodic task, the periodic task is stored to the first queue through the interface, the first function is called to take out the periodic task, and the periodic task is executed;
and under the condition that the target task is a one-time task, storing the one-time task to the second queue through the interface, calling the second function to take out the one-time task, and executing the one-time task.
3. The method according to claim 2, wherein the executing the timed task specifically comprises:
starting a timer, and executing an Elapsed event of the timer when the timer times to reach a time interval of the timer;
wherein the Elapsed event executing the timer comprises:
stopping the timer, and calculating the remaining time for executing the timed task based on the target execution time and the current time of the timed task;
judging whether the residual time is less than a preset time interval or not, if the residual time is less than the preset time interval and greater than zero, asynchronously waiting for the residual time and then executing the timed task, and after the execution of the timed task is finished, exiting the execution of an Elapsed event of the timer; or,
if the remaining time is larger than the preset time interval, updating the time interval of the timer based on a reduced proportion, starting the timer, and continuously executing an Elapsed event of the timer under the condition that the time of the timer reaches the time interval of the updated timer.
4. The method according to claim 2, wherein the performing the periodic task specifically comprises:
allocating an ith thread to the periodic task based on a background thread pool;
executing the periodic task by utilizing the ith thread in the ith cycle time, wherein i is a natural number which is more than or equal to 1;
and if the periodic task is not cancelled, asynchronously waiting for a preset execution period, and starting the task execution of the next period after the waiting is finished.
5. The method according to claim 2, wherein the executing the one-time task specifically comprises:
calculating the parallelism of the one-time task based on the logic core number of a Central Processing Unit (CPU);
judging whether the number of the remaining tasks in the second queue is greater than the parallelism, and if so, taking out a plurality of tasks with the number equal to the parallelism from the second queue; otherwise, taking out all the tasks left in the second queue;
and executing a plurality of taken-out tasks in parallel by using a plurality of threads in the background thread pool, wherein one thread correspondingly executes one task.
6. The multi-type task management method of claim 5, wherein the executing the one-time task further comprises at least one of:
in the process of executing a plurality of taken-out tasks in parallel by using a plurality of pool threads in the background threads, performing waiting operation and abnormal detection operation on the execution of each task, and if the execution time exceeds a first preset threshold value or is abnormal, performing log recording;
after the execution of all tasks is finished, acquiring the total time of executing and taking out a plurality of tasks in parallel, and if the total time is greater than or equal to a second preset threshold, performing log recording;
and printing the residual task number of the second queue and the use condition of the background thread pool.
7. A multi-type task management apparatus, comprising:
the determining unit is used for determining the target task type;
the execution unit is used for executing the target task corresponding to the target task type based on the task scheduling manager;
wherein the task scheduling manager provides: the system comprises a first list, a first queue, a second queue, an interface, a first function and a second function;
the first list is used for storing timed tasks, the first queue is used for storing periodic tasks, and the second queue is used for storing disposable tasks;
the interface is used for storing the timing task to the first list, storing the periodic task to the first queue and/or storing the one-time task to the second queue;
the first function is used for taking the periodic task out of the first queue, and the second function is used for taking the one-time task out of the second queue.
8. The multi-type task management apparatus according to claim 7, wherein the execution unit specifically includes:
the first execution subunit is configured to, when the target task is a timing task, store the timing task to the first list through the interface, and execute the timing task;
the second execution subunit is configured to, when the target task is a periodic task, store the periodic task to the first queue through the interface, call the first function to take out the periodic task, and execute the periodic task;
and the third execution subunit is configured to, when the target task is a one-time task, store the one-time task to the second queue through the interface, call the second function to take out the one-time task, and execute the one-time task.
9. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of the multi-type task management method according to any one of claims 1 to 6 when executing the computer program.
10. A non-transitory computer-readable storage medium, having stored thereon a computer program, wherein the computer program, when executed by a processor, implements the steps of the multi-type task management method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110694107.0A CN115509700A (en) | 2021-06-22 | 2021-06-22 | Multi-type task management method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110694107.0A CN115509700A (en) | 2021-06-22 | 2021-06-22 | Multi-type task management method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115509700A true CN115509700A (en) | 2022-12-23 |
Family
ID=84498994
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202110694107.0A Pending CN115509700A (en) | 2021-06-22 | 2021-06-22 | Multi-type task management method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115509700A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN117992250A (en) * | 2024-01-08 | 2024-05-07 | 广州市玄武无线科技股份有限公司 | Consumption priority implementation method, system, device and storage medium |
-
2021
- 2021-06-22 CN CN202110694107.0A patent/CN115509700A/en active Pending
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN117992250A (en) * | 2024-01-08 | 2024-05-07 | 广州市玄武无线科技股份有限公司 | Consumption priority implementation method, system, device and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11157061B2 (en) | Processor management via thread status | |
CN106681811B (en) | Multithreading scheduling method and device based on thread pool | |
US20120137295A1 (en) | Method for displaying cpu utilization in a multi-processing system | |
KR101203099B1 (en) | Method for monitoring running time of OS task framework and apparatus thereof | |
TW200414055A (en) | System, method and software for reducing interrupt latency while polling in system management mode | |
CN111475312B (en) | Message driving method and device based on real-time operating system | |
CN117785431B (en) | Task scheduling distribution method and device, electronic equipment and storage medium | |
CN115509700A (en) | Multi-type task management method and device | |
CN112181748A (en) | Concurrent test method, device, equipment and storage medium based on ring queue | |
CN117573355A (en) | Task processing method, device, electronic equipment and storage medium | |
CN107423206B (en) | Method and device for measuring system management interruption time | |
CN115794446B (en) | Message processing method and device, electronic equipment and storage medium | |
CN109992388B (en) | Multi-task management system for safety-level equipment software of nuclear power plant | |
CN115687491A (en) | Data analysis task scheduling system based on relational database | |
CN115454592A (en) | Task scheduling method and device | |
US8607232B2 (en) | Identifying a transient thread and excluding the transient thread from a processor load calculation | |
KR20120003088A (en) | Apparatus and method for thread progress tracking using deterministic progress index | |
CN114610578A (en) | Resource state management method and device, electronic equipment and storage medium | |
CN112540886A (en) | CPU load value detection method and device | |
CN113032110A (en) | High-availability task scheduling method based on distributed peer-to-peer architecture design | |
US20240232052A1 (en) | Method and apparatus for monitoring - with contention mitigation - avionics application(s) running on a platform with multi-core processor, related electronic avionics system and computer program | |
US12032474B2 (en) | Computer-readable recording medium storing acceleration test program, acceleration test method, and acceleration test apparatus | |
CN111597016B (en) | System task time protection method, system, storage medium and terminal | |
CN115826731B (en) | Sleep control method and device, storage medium and computing equipment | |
CN114138433B (en) | Task timing strategy method, device and medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |