Fixed thread pool adjusting method, controller and storage medium
By dynamically adjusting the number of threads in a fixed thread pool, the task processing problem of thread pool in different load scenarios is solved, and the response performance and resource utilization are improved.
Patent Information
- Application Number
- CN202411435829.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-14
- Publication Date
- 2024-12-20
AI Technical Summary
The existing fixed task processing thread pool cannot expand the number of threads in high-load scenarios, resulting in task backlog and response delay. In low-load scenarios, thread resources are severely wasted and cannot meet the task processing needs in different load scenarios.
By obtaining the worker thread state in a fixed thread pool, dynamically adjusting the number of threads, increasing or decreasing the number of threads according to the number of running threads and the number of free threads, ensuring the adaptability of the thread pool. Specific methods include destroying idle threads at low loads, creating new threads at high loads, and adjusting the number of threads through preset standards and ranges.
Dynamic adjustment of the number of threads is realized, which avoids task response delay and resource waste, improves task processing efficiency and resource utilization, and ensures the effectiveness of thread pool in different load scenarios.
Smart Images

Figure CN119166361A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and specifically provides an adjustment method, a controller and a storage medium for a fixed thread pool. Background Art
[0002] In the prior art, when the CyberRT development framework creates the first coroutine in the entire program, it will initialize the coroutine stack object pool, that is, pre-allocate sufficient memory space according to the number of coroutines configured in the configuration file. Each application using the CyberRT development framework will apply for 400M of virtual memory, and the VxWorks operating system will immediately map the applied virtual memory to the physical memory. However, if each process using the CyberRT development framework needs to apply for 400M of memory space, the operating system will soon have a memory overflow. Therefore, in order to prevent the process using the CyberRT development framework from occupying too much physical memory on the VXWorks operating system, the cross-domain communication task scheduling mechanism based on the CyberRT development framework, after being adapted to the VxWorks operating system, adopts a fixed task processing thread pool, and limits the size of the physical memory occupied by the process in the VxWorks operating system by fixing the number of threads in the thread pool.
[0003] However, this cross-domain communication task processing architecture uses a fixed task processing thread pool, which means that a certain number of thread control blocks and stack space need to be pre-allocated, and this solution also has real-time defects. In some high-load scenarios, when the number of tasks exceeds the capacity of the thread pool, the thread pool cannot expand the number of threads according to actual needs and cannot meet the needs of task processing, which may cause task backlogs and delays in task response. In low-load or idle scenarios, a fixed number of threads still exist, which will take up additional memory and CPU resources and increase the overhead of switching between threads. Therefore, this method of fixing the task processing thread pool cannot meet the task processing needs under different load scenarios, and there may be problems of task response delays or resource waste.
[0004] Accordingly, a new solution is needed in the art to solve the above problems. Summary of the invention
[0005] In order to overcome the above defects, the present application is proposed to provide a solution or at least partially solve the problem of thread response delay or resource waste in the existing method of fixed task processing thread pool.
[0006] In a first aspect, the present application provides a method for adjusting a fixed thread pool, comprising:
[0007] Get the working status of all working threads in the fixed thread pool;
[0008] Determine the amount of running threads according to the working status of all the working threads, where the amount of running threads is the number of working threads in the fixed thread pool that are in a running state;
[0009] The total number of working threads in the fixed thread pool is dynamically adjusted according to the amount of running threads.
[0010] In a technical solution for adjusting the fixed thread pool, the dynamic adjustment of the total number of working threads in the fixed thread pool includes:
[0011] Based on the first preset standard and the second preset standard, and according to the size of the running threads, adjusting the total number of working threads in the fixed thread pool;
[0012] The first preset standard and the second preset standard are determined according to the total number of working threads in the fixed thread pool, and are used to determine whether the total number of working threads needs to be adjusted.
[0013] In one technical solution of the above-mentioned adjustment of the fixed thread pool, the first preset standard is smaller than the second preset standard;
[0014] The adjusting the total number of working threads in the fixed thread pool includes:
[0015] If the number of running threads is less than or equal to the first preset standard within the preset time threshold, and the total number of working threads is greater than the lower limit of the preset number range, reducing the total number of working threads;
[0016] If the number of running threads is greater than or equal to the second preset standard within the preset time threshold, and the total number of working threads is less than the upper limit of the preset number range, increase the total number of working threads;
[0017] Among them, the preset number range is the number adjustment range of the total number of working threads.
[0018] In a technical solution for adjusting the fixed thread pool, reducing the total number of working threads includes:
[0019] Recording the idle time of the working thread in the fixed thread pool that is in an idle state, where the idle time is the duration of the working thread in the idle state;
[0020] Determining an idle working thread for destruction according to the idle time;
[0021] Stop the idle working thread and remove the idle working thread from the fixed thread pool;
[0022] The removed idle worker thread is destroyed.
[0023] In a technical solution for adjusting the fixed thread pool, the step of determining the idle working thread to be destroyed according to the idle time includes:
[0024] The working thread with the longest idle time is determined as the idle working thread.
[0025] In a technical solution for adjusting the fixed thread pool, the step of destroying the idle working thread to be removed includes:
[0026] By using the task wake-up function, determining whether there is a task in a waiting state in the idle working thread;
[0027] If it exists, stop and destroy the idle working thread;
[0028] If not present, destroy the idle worker thread.
[0029] In a technical solution for adjusting the fixed thread pool, increasing the total number of working threads includes:
[0030] Utilizing the task manager in the fixed thread pool, determining a target thread slot, wherein the target thread slot is used to allocate computing resources to the newly created working thread;
[0031] Creating a new working thread based on the target thread slot to increase the total number of working threads;
[0032] Based on the task manager, tasks are assigned to the new working thread.
[0033] In a technical solution for adjusting the fixed thread pool, creating a new working thread based on the target thread slot includes:
[0034] Setting the thread name, index, thread number and working status of the new working thread;
[0035] The new working thread is created according to the thread name, the index, the thread number and the working status.
[0036] In a technical solution for adjusting the fixed thread pool, the step of determining the target thread slot includes:
[0037] Searching for a thread slot in an idle and terminated state in the fixed thread pool, and using the thread slot as the target thread slot;
[0038] If the thread slot in the idle and terminated state is not found, the target thread slot is created at the end of the thread list of the fixed thread pool.
[0039] In a technical solution for adjusting the fixed thread pool, after adjusting the total number of working threads in the fixed thread pool, the method further includes:
[0040] Update the running thread amount and the idle thread amount, wherein the idle thread amount is the number of working threads in the fixed thread pool that are in an idle state;
[0041] According to the updated running thread quantity and the idle thread quantity, the current total number of working threads in the fixed thread pool is determined to update the total number of working threads, the first preset standard and the second preset standard.
[0042] In a technical solution for adjusting the fixed thread pool, obtaining the working status of all working threads in the fixed thread pool includes:
[0043] Based on the monitoring thread, the working status of all the working threads is obtained, and the monitoring thread is a thread pre-created outside the fixed thread pool for monitoring the working status of the working threads in the fixed thread pool.
[0044] In a second aspect, a controller is provided, which includes at least one processor and at least one memory, wherein the memory is suitable for storing multiple program codes, and the program codes are suitable for being loaded and run by the processor to execute the fixed thread pool adjustment method described in any one of the technical solutions of the above-mentioned fixed thread pool adjustment method.
[0045] In a third aspect, a computer-readable storage medium is provided, which stores a plurality of program codes, wherein the program codes are suitable for being loaded and run by a processor to execute the fixed thread pool adjustment method described in any one of the technical solutions of the above-mentioned fixed thread pool adjustment method.
[0046] Solution 1. A method for adjusting a fixed thread pool, characterized in that the method comprises:
[0047] Get the working status of all working threads in the fixed thread pool;
[0048] Determine the amount of running threads according to the working status of all the working threads, where the amount of running threads is the number of working threads in the fixed thread pool that are in a running state;
[0049] The total number of working threads in the fixed thread pool is dynamically adjusted according to the amount of running threads.
[0050] Solution 2. The method for adjusting the fixed thread pool according to Solution 1 is characterized in that:
[0051] The dynamically adjusting the total number of working threads in the fixed thread pool includes:
[0052] Based on the first preset standard and the second preset standard, and according to the size of the running threads, adjusting the total number of working threads in the fixed thread pool;
[0053] The first preset standard and the second preset standard are determined according to the total number of working threads in the fixed thread pool, and are used to determine whether the total number of working threads needs to be adjusted.
[0054] Solution 3. The method for adjusting a fixed thread pool according to Solution 2, wherein the first preset standard is smaller than the second preset standard;
[0055] The adjusting the total number of working threads in the fixed thread pool includes:
[0056] If the number of running threads is less than or equal to the first preset standard within the preset time threshold, and the total number of working threads is greater than the lower limit of the preset number range, reducing the total number of working threads;
[0057] If the number of running threads is greater than or equal to the second preset standard within the preset time threshold, and the total number of working threads is less than the upper limit of the preset number range, increase the total number of working threads;
[0058] Among them, the preset number range is the number adjustment range of the total number of working threads.
[0059] Solution 4. The method for adjusting the fixed thread pool according to Solution 3 is characterized in that:
[0060] The reducing the total number of working threads comprises:
[0061] Recording the idle time of the working thread in the fixed thread pool that is in an idle state, where the idle time is the duration of the working thread in the idle state;
[0062] Determining an idle working thread for destruction according to the idle time;
[0063] Stop the idle working thread and remove the idle working thread from the fixed thread pool;
[0064] The removed idle worker thread is destroyed.
[0065] Solution 5. The method for adjusting the fixed thread pool according to Solution 4 is characterized in that:
[0066] The step of determining an idle working thread for destruction according to the idle time includes:
[0067] The working thread with the longest idle time is determined as the idle working thread.
[0068] Solution 6. The method for adjusting the fixed thread pool according to Solution 4 is characterized in that:
[0069] The step of destroying the idle working thread to be removed includes:
[0070] By using the task wake-up function, determining whether there is a task in a waiting state in the idle working thread;
[0071] If it exists, stop and destroy the idle working thread;
[0072] If not present, destroy the idle worker thread.
[0073] Solution 7. The method for adjusting the fixed thread pool according to Solution 3 is characterized in that:
[0074] The increasing the total number of working threads comprises:
[0075] Utilizing the task manager in the fixed thread pool, determining a target thread slot, wherein the target thread slot is used to allocate computing resources to the newly created working thread;
[0076] Creating a new working thread based on the target thread slot to increase the total number of working threads;
[0077] Based on the task manager, tasks are assigned to the new working thread.
[0078] Solution 8. The method for adjusting the fixed thread pool according to Solution 7 is characterized in that:
[0079] The creating a new working thread based on the target thread slot includes:
[0080] Setting the thread name, index, thread number and working status of the new working thread;
[0081] The new working thread is created according to the thread name, the index, the thread number and the working status.
[0082] Solution 9. The method for adjusting the fixed thread pool according to Solution 7 is characterized in that:
[0083] The determining of the target thread slot includes:
[0084] Searching for a thread slot in an idle and terminated state in the fixed thread pool, and using the thread slot as the target thread slot;
[0085] If the thread slot in the idle and terminated state is not found, the target thread slot is created at the end of the thread list of the fixed thread pool.
[0086] Solution 10. The method for adjusting the fixed thread pool according to Solution 1 is characterized in that:
[0087] After adjusting the total number of working threads in the fixed thread pool, the method further includes:
[0088] Update the running thread amount and the idle thread amount, wherein the idle thread amount is the number of working threads in the fixed thread pool that are in an idle state;
[0089] According to the updated running thread quantity and the idle thread quantity, the current total number of working threads in the fixed thread pool is determined to update the total number of working threads, the first preset standard and the second preset standard.
[0090] Solution 11. The method for adjusting a fixed thread pool according to any one of Solutions 1 to 10, characterized in that:
[0091] The obtaining of the working status of all working threads in the fixed thread pool includes:
[0092] Based on the monitoring thread, the working status of all the working threads is obtained, and the monitoring thread is a thread pre-created outside the fixed thread pool for monitoring the working status of the working threads in the fixed thread pool.
[0093] Solution 12. A controller comprising at least one processor and at least one memory, wherein the memory is suitable for storing multiple program codes, and wherein the program codes are suitable for being loaded and run by the processor to execute the fixed thread pool adjustment method described in any one of Solutions 1 to 11.
[0094] Solution 13. A computer-readable storage medium storing a plurality of program codes, wherein the program codes are suitable for being loaded and run by a processor to execute the fixed thread pool adjustment method described in any one of Solutions 1 to 11.
[0095] The above one or more technical solutions of this application have at least one or more of the following Beneficial effects:
[0096] In the technical solution for implementing the present application, the present application obtains the working status of the working threads in the fixed thread pool, determines the amount of running threads according to the working status of all working threads, and dynamically adjusts the total number of working threads in the fixed thread pool according to the amount of running threads. Through the above configuration method, by regularly obtaining the working status of the working threads, the amount of running threads running in the thread pool can be obtained, and the number of threads in the fixed thread pool can be dynamically adjusted according to the proportion of the amount of running threads in the total number of working threads. In this way, the load of the working threads in the current fixed thread pool can be determined according to the amount of running threads in the fixed thread pool, so that it can be timely determined whether the number of working threads in the fixed thread pool needs to be adjusted, so that the number of threads can be expanded as needed to avoid task response delays, and idle threads can be released in time, saving computing resources and improving the response performance and resource utilization of tasks.
[0097] Furthermore, when more than the preset standard of work threads are in the running state, a new work thread can be created to avoid task backlogs and response delays caused by the inability to expand the number of fixed number of threads according to actual needs in high-load scenarios, thereby ensuring the efficient operation of tasks in the thread. When less than or equal to the preset standard of work threads are in the running state, idle work threads can be destroyed to avoid resource waste caused by switching between too many work threads in low-load scenarios. Therefore, the present application can adapt to task processing requirements in different load scenarios by dynamically adjusting the number of threads, thereby optimizing thread resource utilization and task execution efficiency.
[0098] Furthermore, a preset number range may be pre-set when adjusting the total number of working threads, so as to adjust the number of working threads within a certain number range and avoid operating system memory overflow due to excessive physical memory occupied by threads. BRIEF DESCRIPTION OF THE DRAWINGS
[0099] The disclosure of the present application will become more easily understood with reference to the accompanying drawings. It is easy for those skilled in the art to understand that these drawings are only for illustrative purposes and are not intended to limit the scope of protection of the present application. In addition, similar numbers in the drawings are used to represent similar components, among which:
[0100] Figure 1 It is a flowchart of the main steps of a method for adjusting a fixed thread pool according to an embodiment of the present application;
[0101] Figure 2 It is a main block diagram schematic diagram of a method for adjusting a fixed thread pool according to an implementation method of an embodiment of the present application;
[0102] Figure 3 This is a flowchart of the main steps of an implementation method for reducing the total number of working threads according to an embodiment of the present application;
[0103] Figure 4 It is a flowchart of the main steps of an implementation method of increasing the total number of working threads in an embodiment of the present application. DETAILED DESCRIPTION
[0104] Some embodiments of the present application are described below with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are only used to explain the technical principles of the present application and are not intended to limit the protection scope of the present application.
[0105] In the description of the present application, "module" and "processor" may include hardware, software or a combination of the two. A module may include hardware circuits, various suitable sensors, communication ports, memory, and may also include software parts, such as program code, or a combination of software and hardware. The processor may be a central processing unit, a microprocessor, an image processor, a digital signal processor or any other suitable processor. The processor has data and / or signal processing functions. The processor may be implemented in software, hardware or a combination of the two. Non-temporary computer-readable storage media include any suitable medium that can store program code, such as a disk, a hard disk, an optical disk, a flash memory, a read-only memory, a random access memory, etc. The term "A and / or B" means all possible combinations of A and B, such as only A, only B or A and B. The term "at least one A or B" or "at least one of A and B" has a similar meaning to "A and / or B" and may include only A, only B or A and B. The singular terms "one" and "the" may also include plural forms.
[0106] Here we first explain some terms involved in this application.
[0107] A fixed thread pool is a special type of thread pool that maintains a fixed number of threads, and the number of threads does not increase as the number of tasks increases. This type of thread pool maintains a fixed number of threads at any time, and the threads in the thread pool can be reused to perform multiple tasks. If all threads are busy, new tasks will be put into a waiting queue until an idle thread is available.
[0108] See attached Figure 1 , Figure 1 FIG. 1 is a flow chart of the main steps of a method for adjusting a fixed thread pool according to an embodiment of the present application. Figure 1 As shown, the method for adjusting the fixed thread pool in the embodiment of the present application mainly includes the following steps S101 and S102.
[0109] Step S101: Obtain the working status of all working threads in the fixed thread pool.
[0110] In this embodiment, the fixed thread pool contains a fixed number of worker threads. The worker threads can have multiple working states, including working state (isRunning), idle state (isIdle), dead state (isDead) and created state (isCreated).
[0111] Among them, the working thread can take the task with the highest priority and ready state from the task queue and execute the task. The working thread in the idle state does not execute tasks and can enter the dormant state and wait to be awakened. The working thread in the dead state can be removed from the fixed thread pool and destroyed after being stopped.
[0112] In one implementation, the fixed thread pool is a thread pool under the VxWorks operating system. Furthermore, since the FixedThreadPool (fixed size thread pool) is part of the Executors class in Java, and in the VxWorks operating system, VxWorks does not fully support Java or all of its features, the implementation of the Java thread pool may be limited, so the fixed thread pool may also be a thread pool implemented using the thread management function provided by the VxWorks operating system.
[0113] In one implementation, obtaining the working status of all working threads in a fixed thread pool may include:
[0114] Based on the monitoring thread, obtain the working status of all working threads in the fixed thread pool.
[0115] In this embodiment, the monitoring thread can obtain the working status of the working threads of the fixed thread pool, the number of working threads in the running state, and the number of working threads in the idle state through the MonitorThread::ManageWorkers() function.
[0116] In this embodiment, see the attached Figure 2 , Figure 2 FIG. 1 is a schematic diagram of a main block diagram of a method for adjusting a fixed thread pool according to an embodiment of the present application. Figure 2 As shown, the monitoring thread (equivalent to Figure 2 monitor_thread in) and fixed thread pool (equivalent to Figure 2 The workers in the .
[0117] in, Figure 2 The _config in is an associative container for storing data of key-value pairs. _config stores multiple key-value pairs, with the key being the thread group name (equivalent to Figure 2 group_name1 or group_name2 in the , and the value is the program configuration file that constructs the corresponding thread group (equivalent to Figure 2 Required parameters when using ExeConfig in .
[0118] in, Figure 2 Thread1, thread2 and thread3 are all worker threads in the fixed thread pool.
[0119] in, Figure 2 The tasks_lists in the table are task lists executed by worker threads in the fixed thread pool in the same ExeConfig. The task list can contain multiple priority queues, such as Figure 2 priority_queue1, priority_queue2, and priority_queue3 in the queue. Each priority list contains multiple tasks (equivalent to Figure 2 Each task is sorted according to its priority, so that the task with the highest priority in the task queue is executed first.
[0120] In one implementation, the value may include a fixed thread pool length and a priority queue length, wherein the fixed thread pool length is used to limit the number of threads in the fixed thread pool, and the priority queue length is used to limit the maximum number of tasks included in the priority queue.
[0121] In one implementation, the monitoring thread can obtain the execution status of each worker thread in the fixed thread pool on the task list, thereby obtaining the working status of the worker thread. For example, if the execution status is that the worker thread is executing the task, the working status of the worker thread is the running status; if the execution status is that the worker thread has not executed the task or has completed the task, the working status of the worker thread is the idle status.
[0122] In this embodiment, the fixed thread pool can be used to manage the working status of the working threads, for example, to store the thread name, index, thread number and working status of each working thread, so as to facilitate the monitoring thread to obtain the working status of the working thread.
[0123] In this implementation, the monitoring thread may periodically obtain the working status of the working threads and record the number of working threads currently in operation, so as to detect the load status of the fixed thread pool in a timely manner.
[0124] In one implementation, the monitoring thread is further used to record the length of time that the working thread is in an idle state.
[0125] Step S102: Determine the amount of running threads according to the working status of all working threads.
[0126] In this embodiment, the running thread amount is the number of working threads in the fixed thread pool that are in a running state.
[0127] In one implementation, the number of working threads currently in operation and the duration for which the working threads are in operation can be obtained in real time through the monitoring thread.
[0128] Step S103: dynamically adjusting the total number of working threads in the fixed thread pool according to the number of running threads.
[0129] In this embodiment, the total number of working threads in the fixed thread pool is dynamically adjusted, and the total number of working threads can be reduced or increased by destroying existing working threads or creating new working threads.
[0130] In one implementation, step S103 may further include step S1031:
[0131] Step S1031: Based on the first preset standard and the second preset standard, and according to the size of the running threads, the total number of working threads in the fixed thread pool is adjusted.
[0132] In this embodiment, the first preset standard and the second preset standard are determined according to the total number of working threads in the fixed thread pool, and are used to determine whether the total number of working threads needs to be adjusted.
[0133] In one implementation, the first preset standard may be smaller than the second preset standard, and step S1031 may further include step S10311 and step S10312:
[0134] Step S10311: If the number of running threads is less than or equal to the first preset standard within the preset time threshold, and the total number of working threads is greater than the lower limit of the preset number range, reduce the total number of working threads.
[0135] Step S10312: If the number of running threads is greater than or equal to the second preset standard within the preset time threshold, and the total number of working threads is less than the upper limit of the preset number range, the total number of working threads is increased.
[0136] In this embodiment, the preset time threshold is a critical value used to determine whether the fixed thread pool has been in a low load or high load state within a preset time. By setting the preset time threshold, frequent adjustments to the total number of working threads in the fixed thread pool in a short period of time can be avoided, which is conducive to the stable operation of the fixed thread pool.
[0137] In this embodiment, the preset number range is the number adjustment range of the total number of working threads. The preset number range is used to adjust the number of working threads within a reasonable number range to avoid the problem of excessive physical memory occupied by working threads and memory overflow of the VxWorks operating system due to continuous creation of new working threads in a scenario where the fixed thread pool is large.
[0138] In one implementation, the first preset standard and the second preset standard may be the same, such as the first preset standard and the second preset standard may both be a certain proportion of the total number of working threads in the fixed thread pool.
[0139] In one embodiment, the first preset standard and the second preset standard may also be different, for example, the first preset standard may be equal to a certain proportion of the total number of working threads in the fixed thread pool, and the second preset standard may be equal to the total number of working threads in the fixed thread pool. As an example, the first preset standard may be 1 / 4 of the total number of working threads in the fixed thread pool, and the second preset standard may be the total number of working threads in the fixed thread pool.
[0140] In one implementation, when the second preset standard is equal to the total number of working threads in the fixed thread pool, step S10312 may further include:
[0141] If the amount of running threads is equal to the second preset standard within the preset time threshold, that is, the threads in the fixed thread pool are all in the running state, and there are still tasks to be processed in the task list corresponding to the fixed thread pool, the total number of working threads is increased.
[0142] In one embodiment, see the attached Figure 3 , Figure 3 1 is a flow chart of the main steps of an implementation method for reducing the total number of working threads according to an embodiment of the present application. Figure 3 As shown, reducing the total number of working threads may further include steps S201 to S204:
[0143] Step S201: Based on the monitoring thread, the idle time of the working threads in the fixed thread pool that are in an idle state is recorded.
[0144] The idle time is the duration that the working thread is in an idle state.
[0145] Step S202: Determine an idle working thread to be destroyed according to the idle time.
[0146] In one implementation, the work thread with the longest idle time may be determined as the idle work thread.
[0147] Step S203: Stop the idle working thread and remove the idle working thread from the fixed thread pool.
[0148] Step S204: destroying the removed idle working thread.
[0149] In one implementation, step S204 may further include:
[0150] Through the task wake-up function, it is determined whether there is a task in the waiting state in the idle working thread.
[0151] If it exists, stop and destroy the idle worker thread; if it does not exist, destroy the idle worker thread.
[0152] In this embodiment, the task wake-up function may be a SemaphorePost() function, which is used to wake up tasks in a waiting state in a task list to prevent task blocking after destroying an idle working thread, thereby ensuring safe destruction of the working thread.
[0153] In this embodiment, see the attached Figure 4 , Figure 4 This is a flow chart of the main steps of an implementation method for increasing the total number of working threads according to an embodiment of the present application. Figure 4 As shown, increasing the total number of working threads may further include steps S301 to S303:
[0154] Step S301: using the task manager in the fixed thread pool to determine the target thread slot.
[0155] In this implementation, the task manager may be a task scheduler (Executor) in a fixed thread pool.
[0156] In this embodiment, the target thread slot is used to allocate computing resources to the newly created worker thread. The thread slot refers to the basic unit for task scheduling and resource allocation. A slot can be understood as a virtual computing unit that can be used to carry one or more data processing tasks. The number of slots determines the number of tasks running simultaneously. When a worker thread is created, it can apply for a new thread slot, or it can search for a thread slot that is idle or dead, allocate computing resources to a specific task, and ensure that the task can run smoothly.
[0157] In one implementation, determining the target thread slot may further include step S3011 and step S3012:
[0158] Step S3011: Search for a thread slot in an idle and terminated state in the fixed thread pool, and use the thread slot as a target thread slot.
[0159] Step S3012: If no thread slot in the idle and terminated state is found, a target thread slot is created at the end of the thread list of the fixed thread pool.
[0160] Step S302: Create a new working thread based on the target thread slot to increase the total number of working threads.
[0161] In this implementation, the task manager may call the CreateWorker() function to create a new worker thread.
[0162] In one implementation, creating a new work thread may further include:
[0163] Set the thread name, index, thread number and working status of the new working thread;
[0164] Create a new working thread based on the thread name, index, thread number and working status.
[0165] Step S303: Based on the task manager, assign tasks to the new working thread.
[0166] In one implementation, a task in a ready state may be assigned to a new working thread by traversing a task list (task_list) managed by a task manager and used for execution by a fixed thread pool.
[0167] In one embodiment, after adjusting the total number of working threads in the fixed thread pool, the method further includes step S401 and step S402:
[0168] Step S401: Update the number of running threads and the number of idle threads.
[0169] In this implementation, the idle thread amount is the number of idle working threads in the fixed thread pool.
[0170] Step S402: Determine the current total number of working threads in the fixed thread pool according to the updated number of running threads and the number of idle threads, so as to update the total number of working threads, the first preset standard and the second preset standard.
[0171] In one implementation, updating the thread amount and the idle thread amount may further include:
[0172] Increase the number of running threads in the fixed thread pool by using the MonitorThread::IncRunningThreadNum() function in the monitoring thread, and reduce the number of running threads by using the MonitorThread::DecRunningThreadNum() function in the monitoring thread;
[0173] The number of idle threads is increased by the MonitorThread::IncIdleThreadNum() function in the monitoring thread, and the number of idle threads is reduced by the MonitorThread::DecIdleThreadNum() function in the monitoring thread.
[0174] In this embodiment, the total number of working threads, the first preset standard and the second preset standard are updated in real time to facilitate subsequent adjustment of the total number of working threads in the fixed thread pool, which can improve the accuracy of determining whether to adjust the fixed thread pool.
[0175] Based on the above steps S101-S103, the working status of the working thread can be obtained regularly, so that the amount of running threads in the thread pool can be obtained, and the number of threads in the fixed thread pool can be dynamically adjusted according to the proportion of the amount of running threads in the total number of working threads. In this way, the load of the working threads in the current fixed thread pool can be determined according to the amount of running threads in the fixed thread pool, so that it can be timely determined whether the number of working threads in the fixed thread pool needs to be adjusted, so that the number of threads can be expanded as needed to avoid task response delays, and idle threads can be released in time, saving computing resources and improving the response performance and resource utilization of tasks.
[0176] Furthermore, when more than the preset standard of work threads are in the running state, a new work thread can be created to avoid task backlogs and response delays caused by the inability to expand the number of fixed number of threads according to actual needs in high-load scenarios, thereby ensuring the efficient operation of tasks in the thread. When less than or equal to the preset standard of work threads are in the running state, idle work threads can be destroyed to avoid resource waste caused by switching between too many work threads in low-load scenarios. Therefore, the present application can adapt to task processing requirements in different load scenarios by dynamically adjusting the number of threads, thereby optimizing thread resource utilization and task execution efficiency.
[0177] Furthermore, a preset number range may be pre-set when adjusting the total number of working threads, so as to adjust the number of working threads within a certain number range and avoid operating system memory overflow due to excessive physical memory occupied by threads.
[0178] It should be pointed out that although the various steps in the above embodiments are described in a specific order, those skilled in the art can understand that in order to achieve the effect of the present application, different steps do not have to be executed in such an order. They can be executed simultaneously (in parallel) or in other orders. These changes are within the scope of protection of the present application.
[0179] It is understood by those skilled in the art that all or part of the processes in the method for implementing the above-mentioned embodiment of the present application can also be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of each of the above-mentioned method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable storage medium may include: any entity or device, medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory, random access memory, electric carrier signal, telecommunication signal and software distribution medium that can carry the computer program code. It should be noted that the content contained in the computer-readable storage medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable storage media do not include electric carrier signals and telecommunication signals.
[0180] Furthermore, the present application also provides a controller. In a controller embodiment according to the present application, the controller includes a processor and a memory, the memory can be configured to store a program for executing the method for adjusting the fixed thread pool of the above method embodiment, and the processor can be configured to execute the program in the memory, which includes but is not limited to the program for executing the method for adjusting the fixed thread pool of the above method embodiment. For ease of explanation, only the parts related to the embodiment of the present application are shown. For specific technical details not disclosed, please refer to the method part of the embodiment of the present application. The controller can be a controller device formed by various electronic devices.
[0181] In the embodiment of the present application, the controller may be a controller device formed by various electronic devices. In some possible implementations, the controller may include multiple memories and multiple processors. The program for executing the adjustment method of the fixed thread pool of the above-mentioned method embodiment can be divided into multiple subprograms, and each subprogram can be loaded and run by the processor to execute the different steps of the adjustment method of the fixed thread pool of the above-mentioned method embodiment. Specifically, each subprogram can be stored in different memories, and each processor can be configured to execute the program in one or more memories to jointly implement the adjustment method of the fixed thread pool of the above-mentioned method embodiment, that is, each processor executes the different steps of the adjustment method of the fixed thread pool of the above-mentioned method embodiment, to jointly implement the adjustment method of the fixed thread pool of the above-mentioned method embodiment.
[0182] The above-mentioned multiple processors may be processors deployed on the same device. For example, the above-mentioned controller may be a high-performance device composed of multiple processors, and the above-mentioned multiple processors may be processors configured on the high-performance device. In addition, the above-mentioned multiple processors may also be processors deployed on different devices. For example, the above-mentioned controller may be a server cluster, and the above-mentioned multiple processors may be processors on different servers in the server cluster.
[0183] Furthermore, the present application also provides a computer-readable storage medium. In a computer-readable storage medium embodiment according to the present application, the computer-readable storage medium can be configured to store a program for executing the adjustment method of the fixed thread pool of the above-mentioned method embodiment, and the program can be loaded and run by the processor to implement the above-mentioned adjustment method of the fixed thread pool. For ease of explanation, only the parts related to the embodiment of the present application are shown. For specific technical details not disclosed, please refer to the method part of the embodiment of the present application. The computer-readable storage medium can be a memory device formed by various electronic devices. Optionally, the computer-readable storage medium in the embodiment of the present application is a non-temporary computer-readable storage medium.
[0184] Further, it should be understood that since the setting of each module is only for illustrating the functional units of the device of the present application, the physical devices corresponding to these modules may be the processor itself, or a part of the software in the processor, a part of the hardware, or a part of the combination of software and hardware. Therefore, the number of each module in the figure is only schematic.
[0185] It is understood by those skilled in the art that each module in the device can be adaptively split or merged. Such splitting or merging of specific modules will not cause the technical solution to deviate from the principle of the present application, and therefore, the technical solutions after splitting or merging will fall within the protection scope of the present application.
[0186] So far, the technical solutions of the present application have been described in conjunction with the preferred embodiments shown in the accompanying drawings. However, it is easy for those skilled in the art to understand that the protection scope of the present application is obviously not limited to these specific embodiments. Without departing from the principles of the present application, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will fall within the protection scope of the present application.
Claims
1. A method for adjusting a fixed thread pool, It is characterized in that The method comprises: Get the working status of all working threads in the fixed thread pool; Determine the amount of running threads according to the working status of all the working threads, where the amount of running threads is the number of working threads in the fixed thread pool that are in a running state; The total number of working threads in the fixed thread pool is dynamically adjusted according to the amount of running threads.
2. The method for adjusting a fixed thread pool according to claim 1, It is characterized in that The dynamically adjusting the total number of working threads in the fixed thread pool includes: Based on the first preset standard and the second preset standard, and according to the size of the running threads, adjusting the total number of working threads in the fixed thread pool; The first preset standard and the second preset standard are determined according to the total number of working threads in the fixed thread pool, and are used to determine whether the total number of working threads needs to be adjusted.
3. The method for adjusting a fixed thread pool according to claim 2, It is characterized in that The first preset standard is smaller than the second preset standard; The adjusting the total number of working threads in the fixed thread pool includes: If the number of running threads is less than or equal to the first preset standard within the preset time threshold, and the total number of working threads is greater than the lower limit of the preset number range, reducing the total number of working threads; If the number of running threads is greater than or equal to the second preset standard within the preset time threshold, and the total number of working threads is less than the upper limit of the preset number range, increase the total number of working threads; Among them, the preset number range is the number adjustment range of the total number of working threads.
4. The method for adjusting a fixed thread pool according to claim 3, It is characterized in that The reducing the total number of working threads comprises: Recording the idle time of the working thread in the fixed thread pool that is in an idle state, where the idle time is the duration of the working thread in the idle state; Determining an idle working thread for destruction according to the idle time; Stop the idle working thread and remove the idle working thread from the fixed thread pool; The removed idle worker thread is destroyed.
5. The method for adjusting a fixed thread pool according to claim 4, It is characterized in that The step of determining an idle working thread for destruction according to the idle time includes: The working thread with the longest idle time is determined as the idle working thread.
6. The method for adjusting a fixed thread pool according to claim 4, It is characterized in that The step of destroying the idle working thread to be removed comprises: By using the task wake-up function, determining whether there is a task in a waiting state in the idle working thread; If it exists, stop and destroy the idle working thread; If not present, destroy the idle worker thread.
7. The method for adjusting a fixed thread pool according to claim 3, It is characterized in that The increasing the total number of working threads comprises: Utilizing the task manager in the fixed thread pool, determining a target thread slot, wherein the target thread slot is used to allocate computing resources to the newly created working thread; Creating a new working thread based on the target thread slot to increase the total number of working threads; Based on the task manager, tasks are assigned to the new working thread.
8. The method for adjusting a fixed thread pool according to claim 7, It is characterized in that The creating a new working thread based on the target thread slot includes: Setting the thread name, index, thread number and working status of the new working thread; The new working thread is created according to the thread name, the index, the thread number and the working status.
9. The method for adjusting a fixed thread pool according to claim 7, It is characterized in that The determining of the target thread slot includes: Searching for a thread slot in an idle and terminated state in the fixed thread pool, and using the thread slot as the target thread slot; If the thread slot in the idle and terminated state is not found, the target thread slot is created at the end of the thread list of the fixed thread pool.
10. The method for adjusting a fixed thread pool according to claim 1, It is characterized in that After adjusting the total number of working threads in the fixed thread pool, the method further includes: Update the running thread amount and the idle thread amount, wherein the idle thread amount is the number of working threads in the fixed thread pool that are in an idle state; According to the updated running thread quantity and the idle thread quantity, the current total quantity of working threads in the fixed thread pool is determined to update the total quantity of working threads, the first preset standard and the second preset standard.
