Thread scheduling method and device, computer equipment, readable storage medium and program product
Through custom thread pool and estimated time-consuming mechanism, thread priority is dynamically adjusted, task accumulation problems caused by limited thread resources are solved, and thread resource utilization and task processing efficiency are improved.
Patent Information
- Application Number
- CN202411716412.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-11
AI Technical Summary
The existing thread pool processing mechanism mainly schedules threads in the order of submission time, resulting in limited thread resources, and task instances accumulate in blocking queues, affecting thread resource utilization and task execution efficiency.
Through a custom thread pool, the submitted threads are rejected when the core thread is full and added to the waiting queue, estimate the time consumption of each thread, dynamically adjust thread priority, interrupt long-time tasks, and prioritize tasks that are shorter-time.
It improves the utilization rate of thread resources, optimizes the task execution order, reduces the impact of long-term tasks on short-term tasks, and improves the overall task processing capability.
Smart Images

Figure CN119597427A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of big data technology, and in particular to a thread scheduling method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] A thread is the basic unit for operating system operation scheduling and the smallest unit of program execution. A thread pool is used to manage multiple threads. It creates a certain number of threads in advance to handle tasks, avoiding frequent creation and destruction of threads, thereby improving resource utilization and response speed.
[0003] Currently, after the task instance is initialized, it is usually packaged in a thread and submitted to the thread pool for analysis. In this process, the thread will continue to loop and analyze the running results of the task instance until the analysis is completed. However, the existing thread pool processing mechanism mainly schedules threads in the order of submission time. Due to the limited thread resources of the thread pool, it is easy for task instances to accumulate in the blocking queue, affecting the utilization of thread resources and causing low task execution efficiency. Summary of the invention
[0004] Based on this, it is necessary to provide a thread scheduling method, apparatus, computer device, computer-readable storage medium and computer program product that can improve thread resource utilization in response to the above technical problems.
[0005] In a first aspect, the present application provides a thread scheduling method, comprising:
[0006] Submitting a first thread to a thread pool; the thread pool is used to add the first thread to a waiting queue and output a first running state of the first thread when the number of currently running second threads reaches a maximum number of core threads;
[0007] In response to monitoring the first running state, obtaining an estimated time consumption of the first thread in the waiting queue and an estimated time consumption of the plurality of second threads;
[0008] Determine the first thread with the shortest estimated time consumption in the waiting queue as the first target thread, and determine the second thread with the longest estimated time consumption among the plurality of second threads as the second target thread;
[0009] When the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset duration, the operation of the second target thread is interrupted, and the first target thread is submitted to the thread pool.
[0010] In one embodiment, the thread pool is further used to run the first thread and output a second running state of the first thread when the number of the currently running second threads does not reach the maximum number of core threads, wherein the second running state includes a completion state or an interruption state, and after submitting the first thread to the thread pool, the method further includes:
[0011] In response to monitoring the completion state or the interruption state, the estimated time consumption of the first thread in the waiting queue is obtained, and the first thread with the shortest estimated time consumption in the waiting queue is submitted to the thread pool.
[0012] In one of the embodiments, the thread pool is further used to store a mapping relationship between the first thread and the executed task instance in response to receiving the first thread; the mapping relationship is used to determine the second running state of the first thread.
[0013] In one embodiment, before submitting the first thread to the thread pool, the method includes:
[0014] A response strategy for access requests to a blocking queue of a thread pool is redefined; the response strategy is used to indicate that the blocking queue does not support access to data.
[0015] In one embodiment, the estimated time consumption is determined by:
[0016] Obtain the number of bytes of the stream file generated by the task instance included in the thread execution and the rate at which the workflow processor processes bytes under the task instance;
[0017] The ratio of the number of bytes to the rate is calculated to obtain the estimated time consumption of the thread.
[0018] In one embodiment, calculating the ratio of the number of bytes to the rate to obtain the estimated time consumption of the thread includes:
[0019] Obtaining the historical estimated time consumption corresponding to the thread; the error rate between the historical estimated time consumption and the corresponding actual running time is less than a preset threshold;
[0020] The ratio of the number of bytes to the rate is calculated, and the average of the ratio and the historical estimated time consumption is calculated to obtain the estimated time consumption of the thread.
[0021] In a second aspect, the present application further provides a thread scheduling device, comprising:
[0022] A submission module, used for submitting a first thread to a thread pool; the thread pool is used for adding the first thread to a waiting queue and outputting a first running state of the first thread when the number of currently running second threads reaches a maximum number of core threads;
[0023] A prediction module, configured to obtain an estimated time consumption of a first thread in the waiting queue and an estimated time consumption of a plurality of second threads in response to monitoring the first running state;
[0024] A determination module, configured to determine a first thread with the shortest estimated time consumption in the waiting queue as a first target thread, and determine a second thread with the longest estimated time consumption among the plurality of second threads as a second target thread;
[0025] A switching module is used to interrupt the operation of the second target thread and submit the first target thread to the thread pool when the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset duration.
[0026] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any of the above-described methods when executing the computer program.
[0027] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of any of the methods described above when executed by a processor.
[0028] In a fifth aspect, the present application also provides a computer program product, including a computer program, which implements the steps of any of the methods described above when executed by a processor.
[0029] The thread scheduling method, device, computer equipment, computer-readable storage medium and computer program product above, when the currently running thread reaches the maximum number of core threads, that is, when the core threads are full, the submitted threads are rejected by the custom thread pool, and these threads are added to the custom queue to compare the estimated time consumption, and the execution order of the tasks is optimized by dynamically adjusting the threads being executed, interrupting the time-consuming tasks, and giving priority to the short-time tasks, thereby reducing the blocking effect of long-running tasks on short-time tasks and improving the overall utilization of thread resources. Further, by dynamically responding to the running state of the thread, the task with the shortest time consumption in the waiting queue is given priority when the thread resources are idle, which can reduce the idle waiting time of the task and improve the overall task processing capability. Further, by introducing a mapping relationship to manage the connection between the thread and the task instance, the dynamic management capability of the thread pool is enhanced. Further, by redefining the blocking queue of the thread pool, it is possible to automatically reject the newly submitted threads when the core threads are full, so as to facilitate the transfer of these threads to the custom scheduling priority optimization logic. Furthermore, by analyzing the processing speed of the workflow in real time, the estimated time consumption of the thread can be dynamically determined, thereby improving the flexibility and accuracy of thread scheduling; by continuously calibrating the estimated time consumption based on historical data, the reliability of thread scheduling can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0031] Figure 1 A schematic diagram of a process flow of a thread scheduling method in one embodiment;
[0032] Figure 2 A schematic diagram of a flow chart of a thread scheduling method in another embodiment;
[0033] Figure 3 A flowchart of a thread scheduling method in another embodiment;
[0034] Figure 4 A schematic diagram of a flow chart of a method for determining estimated time consumption in one embodiment;
[0035] Figure 5 is a structural block diagram of a thread scheduling device in one embodiment;
[0036] Figure 6 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0037] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0038] The execution process of the thread pool in the related art includes: packaging the task instance in a thread and submitting it to the thread pool; if the core thread is full, adding the thread to the blocking queue; if the core thread is not full, executing the thread, analyzing the operation of the task instance, until the analysis is completed and the thread exits. However, the related art mainly schedules threads according to the order of submission time. When some tasks that take a long time to run are submitted to the thread pool first, and some tasks that take a short time to run are submitted to the thread pool later, the task instance corresponding to the executing thread takes a long time to run, and the system thread resources are limited, resulting in the thread that takes a short time to run can only queue in the blocking queue, resulting in poor utilization efficiency of thread resources and poor timeliness of analyzing the operation of the task instance.
[0039] Based on this, the embodiment of the present application provides a thread scheduling method. This embodiment uses the method applied to a server as an example for illustration. It can be understood that the method can also be applied to a terminal. In this embodiment, if Figure 1 As shown, the method comprises the following steps:
[0040] Step S102, submitting the first thread to the thread pool; the thread pool is used to add the first thread to the waiting queue and output the first running state of the first thread when the number of currently running second threads reaches the maximum number of core threads.
[0041] Among them, the first thread and the second thread can be used to analyze the running results of the contained task instances. The thread pool can be used to trigger a custom thread submission rejection strategy when the currently active thread, that is, the second thread, reaches the maximum number of core threads, and add the thread whose submission is rejected to the waiting queue to implement custom processing of the thread. In one possible implementation, the waiting queue and the thread rejection strategy can be implemented based on a custom class. For example, the waiting queue can be implemented based on a custom bounded ordered queue (Bounded Ordered Queue) class, which can include: a custom Bounded Ordered Queue class is used to store elements that implement Comparable; class object initialization design can include: (1) int capacity queue upper limit capacity; (2) List <e>elements element array; class object method design may include: (1) enqueue (E item) element into the queue, when the element is entered into the queue, it is determined whether the elements have reached the upper limit of the capacity. If the elements have reached the upper limit, the element will not be stored. If the elements have not reached the upper limit, it is determined whether the elements array already contains the pre-added elements. If the elements already contain the elements, the element will not be stored. If the elements do not contain the elements, the elements will be added; (2) E dequeue () element dequeue, if the elements are empty, null is returned. If the elements are not empty, the elements are sorted and the first element is returned and the element is removed from the elements; (3) is Empty () queue empty judgment, return whether the elements are empty; (4) size () queue capacity size, return elements size; (5) elements () get all the elements in the queue, return elements shallow copy object processing. The thread rejection strategy can be implemented based on the custom thread pool rejection strategy (Enqueue Thread Rejected Execution Handler) class, which can include: defining the thread rejection processing class Enqueue Thread Rejected Execution Handler, implementing the Rejected ExecutionHandler class, which is used to add the rejected threads to the queue Bounded Ordered Queue, implementing the rejected Execution method, the rejected thread r is of the Analyze Task type, the thread pool executor is of the DynamicAdjustable Thread Pool Executor type, calling the executor's remove Thread to remove the thread, setting the Thread State of the rejected Analyze Task to REJECTED, adding the rejected thread r to the queue BoundedOrdered Queue, and calling the thread processing algorithm Thread Process Algorithm.
[0042] Step S104, in response to monitoring the first running state, obtaining the estimated time consumption of the first thread and the estimated time consumption of multiple second threads in the waiting queue.
[0043] The first running state detected can be used to indicate that there are threads (including the first thread) whose submission is rejected. Exemplarily, before submitting to the thread pool, each thread (including the first thread and the second thread) can first call a task instance estimated time consumption algorithm to calculate the task instance estimated time consumption.
[0044] Step S106: determine the first thread with the shortest estimated time consumption in the waiting queue as the first target thread, and determine the second thread with the longest estimated time consumption among the plurality of second threads as the second target thread.
[0045] Exemplarily, the priority of each thread can be determined according to the estimated time consumption of each thread, so that the priority of the thread is negatively correlated with the estimated time consumption. For example, the first thread with the shortest estimated time consumption in the waiting queue is determined as the first target thread with the highest priority. The second thread with the longest estimated time consumption among the multiple second threads currently running is determined as the second target thread with the lowest priority.
[0046] Step S108, when the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset duration, interrupt the operation of the second target thread and submit the first target thread to the thread pool.
[0047] For example, the thread processing algorithm can be used to determine the Thread State according to the currently incoming thread. If it is REJECTED, it means that the thread submission rejection policy is triggered, and the Bounded Ordered Queue<Analyze Task> Get the first target thread A with the highest priority, and then get the second target thread B with the lowest priority from the memory container, and compare the difference between the estimated time consumption and the configured time consumption difference (for example, 1 minute). If the estimated time consumption difference is close to the configured time consumption difference, the first target thread A will be requeued. If the estimated time consumption difference is much larger than the configured time consumption difference, the second target thread will be interrupted to make the thread resources idle, and the first target thread A will be resubmitted to the thread pool. In this way, the loss of resources and performance caused by switching threads with a short time consumption difference can be reduced.
[0048] In a possible implementation, the process of the thread processing algorithm may include: defining an analysis thread processing algorithm class Thread Process Algorithm; defining a public method process(Analyze Task analyze Task,Dynamic Adjustable Thread Pool Executor executor,long delta); performing different processing according to the Thread State value of the analyze Task, which may include: if it is REJECTED, taking out the highest priority thread highest from the Bounded Ordered Queue queue, if highest is not null, obtaining the lowest priority thread lowest in the executor execution, obtaining the configured time difference delta, obtaining the highest estimated time t1, obtaining the lowest estimated time t2, if the difference between t2 and t1 is not much greater than delt, requeuing highest; if the difference between t2 and t1 is much greater than delt, interrupting the lowest thread, calling the executor's remove Thread to remove the lowest thread, putting the lowest into the queue, and putting the lowest into the queue.
[0049] In the above thread scheduling method, when the number of currently running threads reaches the maximum number of core threads, that is, when the core threads are full, the submitted threads are rejected through the custom thread pool, and these threads are added to the custom queue to compare the estimated time consumption. By dynamically adjusting the executing threads, the time-consuming tasks are interrupted, and the short-time tasks are given priority. The execution order of tasks is optimized, the blocking effect of long-running tasks on short-time tasks is reduced, and the overall utilization of thread resources is improved.
[0050] In an exemplary embodiment, the thread pool may also be used to run the first thread and output the second running state of the first thread when the number of the currently running second threads does not reach the maximum number of core threads, such as Figure 2 As shown, the thread scheduling method may further include:
[0051] Step S109, in response to monitoring the completion state or the interruption state, obtaining the estimated time consumption of the first thread in the waiting queue, and submitting the first thread with the shortest estimated time consumption in the waiting queue to the thread pool.
[0052] In a possible implementation, the first thread and the second thread can be implemented based on a custom analysis thread (Analyze Task) class. The custom analysis thread class may include: (1) the class member variable Thread State ts is set to EXECUTING by default; (2) a parameterized constructor task instance object, the instance object includes an estimated time consumption; (3) implementation of the Runnable interface, implementation of the run() method, task instance analysis and processing, and determination of whether the thread is interrupted; if the thread is interrupted, ts is set to INTERRUPTED and the thread exits; if the thread is not interrupted, if the analysis is completed, ts is set to COMPLETED and the thread exits; (4) implementation of Comparable<Analyze Task> Interface, implement the compare To(Analyze Task task) method to compare two Analyze Task tasks.
[0053] In a possible implementation, different processing is performed according to the Thread State value of the analyze Task, which may also include: if it is COMPLETED or INTERRUPTED, taking out the highest priority thread highest from the Bounded Ordered Queue, and if highest is not null, the executor submits the analysis thread highest.
[0054] In this embodiment, by dynamically responding to the running status of the thread, the shortest-consuming task in the waiting queue is submitted preferentially when the thread resources are idle, thereby reducing the idle waiting time of the task and improving the overall task processing capability.
[0055] Furthermore, the thread pool may also be used to store a mapping relationship between the first thread and the executed task instance in response to receiving the first thread; the mapping relationship is used to determine the second running state of the first thread.
[0056] Exemplarily, monitoring the second running state can be used to indicate that there are threads (including the first thread and the second thread) that have finished running. Exemplarily, when a thread is submitted to the thread pool, the before Execute method is executed, the parent class before Execute is called, and the thread is added to the cache container Map.<Analyze Task,Thread> , execute the execute method and call the parent class execute; if the thread refuses to submit, Map<Analyze Task,Thread> Remove the thread, set the Thread State to the analysis thread according to the running state of the task instance, and call the thread processing algorithm; if the thread does not refuse to submit, when the thread runs through the thread pool, execute the after Execute method, and the custom thread oil rejection strategy rejected Execution method is triggered, and the rejected submission thread is added to the waiting queue Bounded Ordered Queue. When the after Execute method is executed, the thread will be removed from the cache container, and the thread will be distinguished whether it is executed normally in the thread pool or interrupted and exited according to whether the task instance bound to the thread is completed normally.
[0057] In a possible implementation, you can customize the Dynamic Adjustable Thread Pool Executor class to implement the native thread pool mechanism and customize the intervention logic. The customized Dynamic Adjustable Thread Pool can include: class member object thread cache container Map<Analyze Task,Thread> , storing the threads being executed in the thread pool<Analyze Task,Thread> , inheriting the Thread Pool Executor class, which can include: (1) the core custom parameters of this class construction method: None Operation Queue type, Enqueue ThreadRejected Execution Handler type, parent class construction method; (2) overriding the before Execute method and calling the parent class before Execute method,<Analyze Task,Thread> Map into Map<Analyze Task, Thread> ; (3) Rewrite the after Execute method, call the parent class after Execute method, call the remove Thread method, obtain the task instance state of the Analyze Task, and determine whether the state is complete. If the analysis is complete, the Thread State of the Analyze Task is set to COMPLETED. If the analysis is not complete, the Thread State of the Analyze Task is set to INTERRUPTED, and the thread processing algorithm Thread Process Algorithm is called for processing; (4) Custom member methods getLowest Priority Thread Couple(), Map<Analyze Task,Thread> The container gets the lowest priority in execution<Analyze Task,Thread> ; (5) Custom member method remove Thread(Analyze Tasktask) from Map<Analyze Task, Thread> The container removes the thread.
[0058] In this embodiment, a mapping relationship is introduced to manage the connection between threads and task instances, thereby enhancing the dynamic management capability of the thread pool.
[0059] In an exemplary embodiment, Figure 3 As shown, the thread scheduling method may further include:
[0060] Step S101, redefine the response strategy for access requests to the blocking queue of the thread pool; the response strategy is used to indicate that the blocking queue does not support access to data.
[0061] In a possible implementation, a blocking queue (None Operation Queue) class can be customized, which is one of the construction parameters of a custom thread pool. It is a Blocking Queue type, but after customization, it can achieve no access to threads, and can immediately trigger a thread rejection policy when the number of active threads in the thread pool reaches the maximum number of core threads. The custom blocking queue class can include: implementing the Blocking Queue interface, and rewriting the main methods, which can include: (1) the offer(E e) method directly returns false to implement that the queue does not actually store elements; (2) the poll(long time out.Time Unit unit) method directly returns null to implement that there are no elements to be taken from the queue; (3) the is Empty() method directly returns false to implement the judgment that the queue is empty; (4) the size() method returns 0 to implement the judgment that the queue size is 0; (5) the take() method returns null to implement that there are no elements to be taken.
[0062] In this embodiment, by redefining the blocking queue of the thread pool, it is possible to automatically reject newly submitted threads when the core threads are full, so as to facilitate the transfer of these threads to the customized scheduling priority optimization logic.
[0063] In an exemplary embodiment, the estimated time consumption of a thread can be determined in real time according to the processing speed of the current workflow, such as Figure 4 As shown, the above-mentioned method for determining the estimated time consumption may include:
[0064] Step A1, obtaining the number of bytes of the stream file generated by the task instance included in the thread execution and the rate at which the workflow processor processes bytes under the task instance.
[0065] Step A2, calculate the ratio of the number of bytes to the rate to obtain the estimated time consumption of the thread.
[0066] Exemplarily, the estimated time consumption of a thread can be determined by the estimated time consumption analysis algorithm of a task instance. The process of the estimated time consumption analysis algorithm of a task instance may include: querying the total number of stream files generated by this task instance and the total byte size, the number of concurrent processes of the workflow processor, the byte size of each stream file, the number of stream files processed, and the processing time; calculating the unit processing byte rate v (unit: bytes per second) under the task instance and the unit concurrency of the workflow processor, that is, the processor concurrency; calculating the ratio of the total byte size of the stream file to v to obtain the estimated time consumption t.
[0067] Among them, the task example can be a detailed description of the operation of each NiFi (Niagara Files) workflow each time it is scheduled to execute. NiFi workflow is a scheduled execution task of NiFi. The estimated time required for each task instance generated by a NiFi workflow each time is also the time required for each workflow scheduling.
[0068] For further information, please refer to Figure 4 , the above step A2 may include:
[0069] Step A21, obtaining the historical estimated time consumption corresponding to the thread; the error rate between the historical estimated time consumption and the corresponding actual running time is less than a preset threshold.
[0070] Step A22, calculate the ratio of the number of bytes to the rate, and calculate the average of the ratio and the historical estimated time consumption to obtain the estimated time consumption of the thread.
[0071] Among them, the historical estimated time consumption corresponding to the thread can be recorded in the workflow historical processing data. The historical processing data can be aggregated to perform statistical averages, and the workflow historical processing data can be obtained for statistical calculations. Exemplarily, the estimated time consumption precision calibration algorithm can be used to calculate the accuracy of each estimated time consumption and the error with the actual time consumption based on the estimated time consumption of each task instance and the actual time consumption after the operation is completed, and more data can be used to calculate a more reasonable estimated time consumption value. The process of the estimated time consumption precision calibration can include: the estimated time consumption analysis algorithm analyzes the estimated time consumption of the task instance, the actual time consumption after the task instance is completed, calculates the time consumption error rate r1, obtains the set allowed time consumption error rate r2, if the absolute value of r1 is much smaller than r2, the estimated time consumption can be trusted and added to the historical processing data; if the absolute value of r1 is close to r2, the estimated time consumption cannot be trusted, and the reason for the large error rate is analyzed.
[0072] In this embodiment, by analyzing the processing speed of the workflow in real time, the estimated time consumption of the thread can be dynamically determined, thereby improving the flexibility and accuracy of thread scheduling; by continuously calibrating the estimated time consumption based on historical data, the reliability of thread scheduling can be improved.
[0073] In summary, in the above thread scheduling method, when the currently running threads reach the maximum number of core threads, that is, when the core threads are full, the submitted threads are rejected by the custom thread pool, and these threads are added to the custom queue to compare the estimated time consumption. By dynamically adjusting the executing threads, the time-consuming tasks are interrupted, and the short-time tasks are processed first, the execution order of the tasks is optimized, the blocking effect of long-running tasks on short-time tasks is reduced, and the overall utilization rate of thread resources is improved. Further, by dynamically responding to the running state of the thread, the shortest time-consuming tasks in the waiting queue are submitted first when the thread resources are idle, the idle waiting time of the tasks can be reduced, and the overall task processing capacity can be improved. Further, by introducing a mapping relationship to manage the connection between threads and task instances, the dynamic management capability of the thread pool is enhanced. Further, by redefining the blocking queue of the thread pool, it is possible to automatically reject the newly submitted threads when the core threads are full, so as to facilitate the transfer of these threads to the custom scheduling priority optimization logic. Furthermore, by analyzing the processing speed of the workflow in real time, the estimated time consumption of the thread can be dynamically determined, thereby improving the flexibility and accuracy of thread scheduling; by continuously calibrating the estimated time consumption based on historical data, the reliability of thread scheduling can be improved.
[0074] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0075] Based on the same inventive concept, the embodiment of the present application also provides a thread scheduling device for implementing the thread scheduling method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more thread scheduling device embodiments provided below can refer to the limitations on the thread scheduling method above, and will not be repeated here.
[0076] In an exemplary embodiment, Figure 5 As shown, a thread scheduling device 200 is provided, comprising: a submission module 201, a prediction module 202, a determination module 203 and a determination module 204, wherein:
[0077] The submission module 201 is used to submit the first thread to the thread pool; the thread pool is used to add the first thread to the waiting queue and output the first running state of the first thread when the number of currently running second threads reaches the maximum number of core threads.
[0078] The prediction module 202 is used to obtain the estimated time consumption of the first thread and the estimated time consumption of multiple second threads in the waiting queue in response to monitoring the first running state.
[0079] The determination module 203 is used to determine the first thread with the shortest estimated time consumption in the waiting queue as the first target thread, and determine the second thread with the longest estimated time consumption among the plurality of second threads as the second target thread.
[0080] The switching module 204 is used to interrupt the operation of the second target thread and submit the first target thread to the thread pool when the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset time length.
[0081] In an exemplary embodiment, the thread pool is further used to run the first thread and output the second running state of the first thread when the number of the currently running second threads does not reach the maximum number of core threads. The switching module 204 is also used to:
[0082] In response to monitoring the completion state or the interruption state, the estimated time consumption of the first thread in the waiting queue is obtained, and the first thread with the shortest estimated time consumption in the waiting queue is submitted to the thread pool.
[0083] In an exemplary embodiment, the thread pool is further used to store a mapping relationship between the first thread and the executed task instance in response to receiving the first thread; the mapping relationship is used to determine the second running state of the first thread.
[0084] In an exemplary embodiment, the thread scheduling device 200 further includes:
[0085] A definition module is used to redefine the response strategy for access requests to the blocking queue of the thread pool; the response strategy is used to indicate that the blocking queue does not support access to data.
[0086] In an exemplary embodiment, the prediction module 202 is further used to:
[0087] Get the number of bytes of the stream file generated by the task instance included in the thread execution and the rate at which the workflow processor processes bytes under the task instance;
[0088] Calculate the ratio of the number of bytes to the rate to get the estimated time consumed by the thread.
[0089] In an exemplary embodiment, the prediction module 202 is further used to:
[0090] Obtain the historical estimated time consumption corresponding to the thread; the error rate between the historical estimated time consumption and the corresponding actual running time is less than a preset threshold;
[0091] Calculate the ratio of the number of bytes to the rate, and calculate the average of the ratio and the historical estimated time consumption to obtain the estimated time consumption of the thread.
[0092] Each module in the thread scheduling device can be implemented in whole or in part by software, hardware or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0093] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 6 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a thread scheduling method is implemented.
[0094] Those skilled in the art will understand that Figure 6 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0095] In an exemplary embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above-mentioned method embodiments when executing the computer program.
[0096] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0097] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0098] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.
[0099] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0100] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.< / e>
Claims
1. A thread scheduling method, characterized in that: The method comprises: Submitting a first thread to a thread pool; the thread pool is used to add the first thread to a waiting queue and output a first running state of the first thread when the number of currently running second threads reaches a maximum number of core threads; In response to monitoring the first running state, obtaining an estimated time consumption of the first thread in the waiting queue and an estimated time consumption of the plurality of second threads; Determine the first thread with the shortest estimated time consumption in the waiting queue as the first target thread, and determine the second thread with the longest estimated time consumption among the plurality of second threads as the second target thread; When the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset duration, the operation of the second target thread is interrupted, and the first target thread is submitted to the thread pool.
2. The method according to claim 1, characterized in that The thread pool is further configured to run the first thread and output a second running state of the first thread when the number of the second threads currently running does not reach the maximum number of core threads, wherein the second running state includes a completion state or an interruption state, and after submitting the first thread to the thread pool, the method further includes: In response to monitoring the completion state or the interruption state, the estimated time consumption of the first thread in the waiting queue is obtained, and the first thread with the shortest estimated time consumption in the waiting queue is submitted to the thread pool.
3. The method according to claim 2, characterized in that The thread pool is further configured to store a mapping relationship between the first thread and the executed task instance in response to receiving the first thread; the mapping relationship is used to determine a second running state of the first thread.
4. The method according to claim 1, characterized in that Before submitting the first thread to the thread pool, the method further comprises: A response strategy for access requests to a blocking queue of a thread pool is redefined; the response strategy is used to indicate that the blocking queue does not support access to data.
5. The method according to claim 1, characterized in that The method for determining the estimated time consumption includes: Obtain the number of bytes of the stream file generated by the task instance included in the thread execution and the rate at which the workflow processor processes bytes under the task instance; The ratio of the number of bytes to the rate is calculated to obtain the estimated time consumption of the thread.
6. The method according to claim 5, characterized in that The calculating the ratio of the number of bytes to the rate to obtain the estimated time consumption of the thread includes: Obtaining the historical estimated time consumption corresponding to the thread; the error rate between the historical estimated time consumption and the corresponding actual running time is less than a preset threshold; The ratio of the number of bytes to the rate is calculated, and the average of the ratio and the historical estimated time consumption is calculated to obtain the estimated time consumption of the thread.
7. A thread scheduling device, characterized in that: The device comprises: A submission module, used for submitting a first thread to a thread pool; the thread pool is used for adding the first thread to a waiting queue and outputting a first running state of the first thread when the number of currently running second threads reaches a maximum number of core threads; A prediction module, configured to obtain an estimated time consumption of a first thread in the waiting queue and an estimated time consumption of a plurality of second threads in response to monitoring the first running state; A determination module, configured to determine a first thread with the shortest estimated time consumption in the waiting queue as a first target thread, and determine a second thread with the longest estimated time consumption among the plurality of second threads as a second target thread; A switching module is used to interrupt the operation of the second target thread and submit the first target thread to the thread pool when the difference between the estimated time consumption of the second target thread and the estimated time consumption of the first target thread is greater than a preset duration.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.