Thread pool adjustment method, device, storage medium and electronic device
By periodically obtaining the task pool and thread pool parameters, adaptively adjusting the thread pool capacity, solving the problems of task accumulation and resource waste, and improving the efficiency and resource utilization of thread pools.
Patent Information
- Application Number
- CN202110343860.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-30
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2041-03-30
AI Technical Summary
In the multi-threaded task pool, task accumulation, low task processing timeliness and waste of system resources due to business peaks and troughs.
By obtaining the parameters of the task pool and thread pool at preset times, determining the target parameters of the target thread pool based on these parameters, and adjusting the current thread pool capacity to achieve adaptive thread pool capacity adjustment.
When there are too many tasks, improve the efficiency and performance of thread pool execution services, minimize task accumulation, reduce the number of threads when there are too few tasks, and save system resources.
Smart Images

Figure CN113157410B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a thread pool adjustment method, device, storage medium and electronic device. Background Art
[0002] In computer programming, a thread pool is a software design method for implementing concurrent execution in a computer program. By maintaining a thread pool, performance can be improved and execution delays can be avoided.
[0003] The main benefit of a thread pool over creating a new thread for each character is that thread pool creation and destruction are limited to the initial creation pool, which leads to better performance and better system stability. However, when multithreading tasks in a task pool, the number of tasks in the task pool varies due to business peaks and valleys. When threads in the thread pool process tasks, it may cause problems such as task accumulation, low task processing timeliness, and waste of system resources. Summary of the invention
[0004] The embodiments of the present application provide a thread pool adjustment method, device, storage medium and electronic device, which can solve the technical problems of task accumulation and system resource waste. The technical solution is as follows:
[0005] In a first aspect, an embodiment of the present application provides a thread pool adjustment method, comprising:
[0006] Obtaining task parameters of a task pool and current thread parameters of a current thread pool corresponding to the task pool at preset intervals;
[0007] Determine the target thread parameter of the target thread pool corresponding to the task pool according to the task parameter and the current thread parameter;
[0008] The capacity of the current thread pool is adjusted according to the target thread parameter.
[0009] In a second aspect, an embodiment of the present application provides a thread pool adjustment device, the device comprising:
[0010] An acquisition module, used for acquiring the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool at preset intervals;
[0011] A determination module, used to determine a target thread parameter of a target thread pool corresponding to the task pool according to the task parameter and the current thread parameter;
[0012] The adjustment module is used to adjust the capacity of the current thread pool according to the target thread parameter.
[0013] In a third aspect, an embodiment of the present application provides a computer storage medium, wherein the computer storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor and executing the above-mentioned method steps.
[0014] In a fourth aspect, an embodiment of the present application provides an electronic device, comprising: a memory and a processor; wherein the memory stores a computer program, and the computer program is suitable for being loaded by the processor and executing the above-mentioned method steps.
[0015] The beneficial effects brought about by the technical solutions provided by some embodiments of the present application include at least:
[0016] When the thread pool adjustment method, device, storage medium and electronic device are working, the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool are obtained at preset time intervals, the target thread parameters of the target thread pool corresponding to the task pool are determined according to the task parameters and the current thread parameters, and the capacity of the current thread pool is adjusted according to the target thread parameters. In the present application, the electronic device can simply and accurately adjust the capacity of the current thread pool according to the preset calculation strategy by periodically determining the task parameters of the task pool and the current thread parameters of the corresponding current thread pool at preset time intervals. When there are too many tasks, the efficiency and performance of the thread pool execution service can be improved to minimize task accumulation. When there are too few tasks, the number of threads can be reduced to save system resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art 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 drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 is a schematic diagram of a network architecture of a regulation system provided in an embodiment of the present application;
[0019] Figure 2 It is a flowchart of a thread pool adjustment method provided in an embodiment of the present application;
[0020] Figure 3 This is another flowchart of a thread pool adjustment method provided by an embodiment of the present application;
[0021] Figure 4 This is a schematic diagram of thread pool initialization configuration provided by an embodiment of the present application;
[0022] Figure 5It is a schematic diagram of a thread processing task provided in an embodiment of the present application;
[0023] Figure 6 This is a schematic diagram of thread pool expansion provided in an embodiment of the present application;
[0024] Figure 7 This is a schematic diagram of thread pool shrinking provided in an embodiment of the present application;
[0025] Figure 8 It is a structural schematic diagram of a thread pool adjustment device provided in an embodiment of the present application;
[0026] Fig. 9 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0027] In order to make the objectives, technical solutions and advantages of the present application more clear, the embodiments of the present application will be further described in detail below with reference to the accompanying drawings.
[0028] When the following description is designed with drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present application. Instead, they are only examples of devices and methods consistent with some aspects of the present application as detailed in the attached claims.
[0029] In the description of this application, it should be understood that the terms "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or implying relative importance. For those of ordinary skill in the art, the specific meanings of the above terms in this application can be understood according to specific circumstances. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the previously associated objects are in an "or" relationship.
[0030] The present application is described in detail below with reference to specific embodiments.
[0031] Figure 1 An exemplary system architecture 100 that can be applied to the asynchronous calling method of the present application is shown.
[0032] like Figure 1As shown, the system architecture 100 may include a user 101, an electronic device 102, and a network 103. The network 103 is used to provide a medium for a communication link between the user 101 and the electronic device 102. Among them, the system architecture 100 can be applied to various practical application scenarios. For example, when the system architecture 100 is applied to online live broadcasting, the user 101 can be a teacher or student or a student's parent in an online live broadcast course, as well as R&D personnel of various departments, etc. The user 101 can be used to trigger various instructions on the electronic device 102, and the electronic device 102 can be a teacher client, a student client, and a server end providing services in the background, etc. The electronic device 102 can be responsible for reading various instructions triggered by the user 101, decoding the instructions and executing the instructions. Exemplarily, in the process of conducting an online live broadcast course, the user 101 (for example, but not limited to teacher A) arranges an online exam, and then requires students to submit electronic test papers within a specified time, and the teacher client performs online marking. The automatic marking system in the teacher client may be composed of a Grpc system framework, including multiple programming languages such as golang and python for programming, and interactively providing call services. Multiple users 101 (such as but not limited to A and student B) can simultaneously click the load button by the mouse to trigger the upload of the test paper electronic document 1 service, and then the electronic device 102 (such as but not limited to the teacher client) simultaneously obtains the marking request of the test paper electronic document 1, in order to improve the efficiency of the online live course. The electronic device needs to asynchronously process multiple test paper marking requests, then the electronic device can call the corresponding asynchronous interface to encapsulate the multiple marking requests into multiple corresponding asynchronous tasks, deliver them to the task pool, and then process the multiple asynchronous tasks simultaneously through the multiple threads in the thread pool, and finally send the marking results to each student and other related operations. In the actual use process, due to the increasing scale of online education, users 101 may suddenly increase in a certain period of time, resulting in the situation that the amount of tasks may appear more and less, when the amount of tasks in the task pool of the electronic device 102 is too large, in order to handle the excessive tasks, the electronic device 102 can expand the capacity of the thread pool, and when the amount of tasks in the task pool is too small, the electronic device 102 can reduce the capacity of the thread pool, which helps the electronic device 102 solve technical problems such as task accumulation and system resource waste.
[0033] The electronic device 102 may be hardware or software. When the electronic device 102 is hardware, it may be various electronic devices with the function of adaptively adjusting the thread pool size, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers. When the electronic device 102 is software, it may be implemented as multiple software or software modules (for example, to provide asynchronous call services), or it may be implemented as a single software or software module, which is not specifically limited here.
[0034] The network 103 may include various types of wired communication links or wireless communication links, and the user 101 and the electronic device 102 may exchange data through the network 103. For example, the wired communication link may include optical fiber, twisted pair or coaxial cable, and the wireless communication link may include Bluetooth communication link, Wireless-Fidelity (Wi-Fi) communication link or microwave communication link.
[0035] It should be understood that Figure 1 The number of users 101, electronic devices 102 and networks 103 in FIG. 1 is only illustrative. Depending on the implementation requirements, there may be any number of users 101, electronic devices 102 and networks 103.
[0036] In the following method embodiments, for the sake of convenience, only the electronic device is used as the execution subject of each step for introduction and description.
[0037] The following will be combined with the attached Figures 2 to 3 , the thread pool adjustment method provided in the embodiment of the present application is introduced in detail.
[0038] See also Figure 2 , provides a flow chart of a thread pool adjustment method according to an embodiment of the present application. The method may include the following steps:
[0039] S201. Obtaining task parameters of a task pool and current thread parameters of a current thread pool corresponding to the task pool at preset intervals.
[0040] Generally, a task pool may refer to a component of a business layer in an electronic device that stores tasks, which can be considered as a container for tasks, and is an instance pool for sharing tasks between threads. The task pool is completely transparent to the client layer, for example, the task pool may be in the form of a queue, a table, a database, etc. When an electronic device application is started, a task pool is created, and is cleared when it is stopped. Due to the cache mechanism for tasks provided by the task pool, the system overhead generated by frequently creating and recycling task instances can be reduced, and storage resources can be saved. After an electronic device triggers a service request, a corresponding service task will be generated based on the service request. The electronic device can process the service task through a thread. First, the storage address of the task pool can be obtained, and then the electronic device can store the service task in the storage space corresponding to the storage address of the task pool, and then the corresponding electronic device resources are loaded in sequence according to the preset execution strategy through the thread in the idle state to process the service task. The number of task pools can be one or more, and an appropriate number of task pools can be set according to the actual task volume, which helps to improve the operating efficiency of the electronic device. The task pool can be stored locally in the electronic device or uploaded to the cloud (for example but not limited to a storage server), and then the electronic devices can interact through data transmission, which helps to reduce the storage space of the electronic device and improve the operating efficiency.
[0041] The task parameters may refer to information such as the configuration and operation status of the task pool, such as: the number of core tasks, the total number of tasks, the waiting time for waiting for new tasks, the time unit, the rejection strategy, the task threshold, the cleaning time for processing the task pool, the number of currently stored tasks, and the number of tasks being executed. The electronic device can initialize the task pool according to the relevant information, and then obtain some parameters in real time during the task execution process. For example, the electronic device can receive the initialization instruction from the user, customize the initialization process of the task pool, and obtain the task pool with the initial information of 5 core tasks, 10 total tasks, and 3 minutes waiting time for waiting for new tasks. Then, during the processing process, the electronic device can obtain the number of tasks currently stored in the task pool as 2, including task 1 of uploading online test electronic documents of student user a and task 2 of uploading online test electronic documents of student user b, and the electronic device determines that the number of tasks being executed is 2, including task 3 of uploading online test electronic documents of student user c and task 4 of uploading online test electronic documents of student user d.
[0042] The thread pool can be understood as a container of a thread usage mode. In the actual use of electronic equipment to process tasks, too many threads will bring scheduling overhead, which will affect cache locality and overall performance. The thread pool maintains multiple threads, waiting for the supervisor to assign concurrently executable tasks. This avoids the cost of creating and destroying threads when processing short-time tasks. The thread pool can not only ensure the full utilization of the kernel, but also prevent over-scheduling. The number of available threads should depend on the number of available concurrent processors, processor cores, memory, network programming interfaces, etc. For example, the number of threads is generally more appropriate to take the number of central processors + 2. Too many threads will cause additional thread switching overhead. The scalability of the thread pool has a greater impact on performance. For example, creating too many threads will waste certain resources, and some threads will not be fully used; destroying too many threads will cause a waste of time to create new threads again; creating threads too slowly will cause long waiting times and poor performance; destroying threads too slowly will cause other threads to starve for resources. In this application, the capacity of the thread pool is adaptively set by the electronic device to help improve resource utilization and operating efficiency, and then the tasks in the task pool can be processed simultaneously by the threads in the thread pool to improve task processing efficiency.
[0043] The electronic device can obtain the current thread parameters of the thread pool by real-time monitoring of the thread pool through the initialization parameters or properties provided by the thread pool. For example, the electronic device can read the pre-defined number of tasks that the thread pool needs to execute (taskCount), the number of tasks that the thread pool has completed during operation (completedTaskCount), the maximum number of threads that the thread pool has ever created (largestPoolSize), the number of threads in the thread pool (getPoolSize) and other parameters to determine the current thread parameters of the thread pool. The electronic device can determine whether the thread pool is full through the parameter largestPoolSize. If it is equal to the maximum size of the thread pool, it means that the thread pool is already fully loaded. If the thread pool is not destroyed, the threads in the pool will not be automatically destroyed, so the parameter getPoolSize can only increase but not decrease, and the electronic device can determine the number of currently active threads. In a possible embodiment, the electronic device can also monitor by extending the thread pool. For example, the electronic device inherits the thread pool and overrides the beforeExecute, afterExecute and terminated methods of the thread pool. It can determine the current thread parameters of the thread pool before and after the task is executed and before the thread pool is closed (for example, but not limited to the average execution time, maximum execution time and minimum execution time of the monitored task).
[0044] Because the number of services may change with the increase or decrease of users or other factors (such as but not limited to time), the electronic device in the present application can first adaptively configure the thread pool capacity based on the relevant parameters of the task pool and the thread pool, so as to reasonably and efficiently process the services, and improve the resource utilization rate and the operating efficiency of the electronic device. For example: the electronic device can obtain the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool every 5 minutes, and then determine whether the current thread pool needs to be reconfigured.
[0045] S202. Determine a target thread parameter of a target thread pool corresponding to the task pool according to the task parameter and the current thread parameter.
[0046] Generally, the target thread pool may refer to a thread pool determined by the task parameters and the current thread parameters. The target thread pool here does not specifically refer to a certain type or a class of specific thread pools, and is not specifically limited in order to distinguish it from other thread pools that have been created for other purposes and to correspond to the current thread pool. The target thread parameters may refer to the parameters of the target thread pool, which may include information such as the number of core threads, the total number of threads, and the waiting time for waiting for new threads. The electronic device initializes the configuration of the target thread pool through the target thread parameters. In a possible embodiment, the electronic device may also receive a custom configuration instruction from a user to initialize the configuration of the target thread pool, for example: the custom configuration instruction includes information such as the number of core threads being 20, the total number of threads being 50, and the waiting time for waiting for new threads being 30 seconds to configure the target thread pool.
[0047] After the electronic device obtains the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool, the target thread parameters of the target thread pool corresponding to the task pool can be determined according to the task parameters and the current thread parameters. For example: the electronic device determines that the task parameters include the number of tasks in the task pool is 50, and the current thread parameters include the number of idle threads is 2, then the electronic device can determine that the target thread parameters of the target thread pool corresponding to the task pool include parameter information such as the maximum number of threads can be 50, the minimum number of threads can be 10, etc., and reasonably calculate the target thread parameters based on the electronic device resources, which helps to improve business processing efficiency.
[0048] S203. Adjust the capacity of the current thread pool according to the target thread parameters.
[0049] Generally, after the electronic device determines the target thread parameter of the target thread pool corresponding to the task pool, the capacity of the current thread pool can be adjusted according to the target thread parameter. For example, when the electronic device determines that the storage capacity of the target thread is 100K, the current number of tasks is most reasonable after evaluation and calculation. Then the current thread pool with a storage capacity of 50K can be expanded to 100K. There are many ways for electronic devices to adjust capacity. For example, the electronic device can expand and shrink the capacity on the basis of the current thread pool, or destroy the current thread pool and create a new target thread pool, and open up a corresponding size of capacity in other electronic devices (such as but not limited to cloud servers) or storage addresses to expand the current thread pool. In a possible embodiment, the electronic device can also open a spare thread pool to handle excessive tasks. For example, the electronic device can re-initialize the thread pool A with a storage capacity of 50K to handle excessive tasks together with the current thread pool, etc. The present application does not specifically limit the way to adjust the capacity of the current thread pool, and can be automatically adjusted by the user according to the actual use situation, which helps to improve the flexibility and diversity of electronic equipment in processing services and improve the user experience.
[0050] It can be seen from the above content that the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool are obtained at preset time intervals, the target thread parameters of the target thread pool corresponding to the task pool are determined according to the task parameters and the current thread parameters, and the capacity of the current thread pool is adjusted according to the target thread parameters. In this application, the electronic device can simply and accurately adjust the capacity of the current thread pool according to the preset calculation strategy by periodically determining the task parameters of the task pool and the current thread parameters of the corresponding current thread pool at preset time intervals. When there are too many tasks, the efficiency and performance of the thread pool execution service can be improved to minimize task accumulation. When there are too few tasks, the number of threads can be reduced to save system resources.
[0051] See also Figure 3 , another flow chart of a thread pool adjustment method is provided for an embodiment of the present application. The thread pool adjustment method may include the following steps:
[0052] S301. Obtain initialization configuration information, and determine at least a default number of threads, a minimum number of threads, and a maximum number of threads of the current thread pool according to the initialization configuration information.
[0053] Generally, electronic devices can obtain delegation notifications in the thread pool, or determine whether to initialize by monitoring return values, or traversing the task list, etc. If it is determined that the thread pool is initialized, initialization processing is required, which can reduce the error rate of the electronic device when processing business. The electronic device can receive initialization configuration information uploaded by the user, and then the electronic device can at least determine the default number of threads, the minimum number of threads and the maximum number of threads of the current thread pool according to the initialization configuration information. For example: the electronic device determines that the initialization configuration message of the thread pool may at least include the number of core threads of the thread pool is 5, the number of non-core threads is 3, the thread pool threshold is 6, the entry and exit method is first-in-first-out, the timeout blocking period is 60 seconds, the default number of threads is 4, the minimum number of threads is 2 (for example, but not limited to, the default number of threads can be set to twice the minimum number of threads, etc.) and the maximum number of threads is 10, etc. Figure 4 As shown, the user can customize the input of the default number of threads as 4, the minimum number of threads as 2, the maximum number of threads as 20 and other information through the initialization interface on the display screen of the electronic device, and then upload the relevant information by clicking the "Confirm Upload" button on the display screen of the electronic device. The electronic device can also display a thread pool capacity diagram on the display screen, and then number the area corresponding to each thread, for example: thread area No. 1, thread area No. 2, thread area No. 3, thread area No. 4, and thread area No. 20, etc., and can also display relevant information such as the corresponding storage address.
[0054] S302: Initialize the current thread pool based on the initialization configuration information, the default number of threads, the minimum number of threads, and the maximum number of threads.
[0055] Generally, initialization can refer to the practice of assigning initial values to data objects or variables. How to initialize depends on the programming language used and the storage type and other properties of the object to be initialized. The program structure used for initialization is called an initializer or initialization list. After obtaining the initialization configuration information, the electronic device can initialize the current thread pool. For example, the electronic device determines that the minimum number of threads maintained by the current thread pool in the initialization function is 10, the maximum number of threads maintained by the current thread pool is 50, the cache queue is 10, the allowed idle time is 60, and other information, and then runs the initialization function, and then determines whether the initialization is successful through the return value or callback function and other information.
[0056] S303. Obtain the number of currently stored tasks in the task pool and the number of currently running threads in the current thread pool corresponding to the task pool at preset intervals.
[0057] Generally, after initializing the current thread pool, the electronic device can obtain the number of currently stored tasks in the task pool and the number of currently running threads in the current thread pool corresponding to the task pool at preset intervals. For example, the electronic device can monitor the task pool at scheduled times every 30 minutes, at 18:00 after 17:30, by using the initialization parameters or properties provided by the task pool to obtain the number of currently stored tasks in the task pool as 25, and determine that the number of currently running threads in the current thread pool corresponding to the task pool is 10. Tasks in the task pool are processed by threads in the thread pool to perform corresponding services, so it is necessary to ensure that the number of threads in the thread pool matches the number of tasks in the task pool. Figure 5 As shown, there can be multiple tasks in the task pool, and then there can be multiple threads in the asynchronous thread pool. The electronic device pre-sets the execution strategy through the system framework (such as but not limited to the Executor framework), and each thread executes the corresponding task, for example: thread 1 processes task 1, thread 2 processes task 2, thread 3 processes task 3, etc. Each thread is associated with a corresponding kernel thread, and then the central processing unit resources are scheduled through the thread scheduler on the electronic device (such as but not limited to the OS Kernel). Each thread will eventually be mapped to the central processing unit for processing. If the central processing unit has multiple cores, multiple thread tasks can be executed in parallel.
[0058] S304: Determine whether the number of tasks is less than or equal to the minimum number of threads.
[0059] Generally, after obtaining the number of tasks currently stored in the task pool and the number of threads currently running in the current thread pool corresponding to the task pool, the electronic device can determine whether the number of tasks is less than or equal to the minimum number of threads. For example, the electronic device determines that the number of tasks is 25, and when initializing the thread pool, the minimum number of threads determined is 2, then it is determined that the number of tasks is greater than the minimum number of threads.
[0060] S305: Determine a first number of threads according to the number of currently running threads, and determine whether the first number of threads is less than the minimum number of threads.
[0061] Generally, when the electronic device determines that the number of tasks is less than or equal to the minimum number of threads, the electronic device can determine the first number of threads based on the number of threads currently running. For example, the electronic device determines that the number of tasks is 11, the minimum number of threads is 15, and the number of threads currently running in the current thread pool is 10. The formula: the first number of threads = the number of threads currently running in the current thread pool / 2 can be used to determine that the first number of threads is 5. The specific calculation method for determining the first number of threads in this application is not specifically limited. The formula and formula parameters can be determined and adjusted by researchers based on empirical values, which helps electronic devices to simply and conveniently determine the most appropriate thread pool capacity. Then the electronic device also needs to determine whether the first number of threads is less than the minimum number of threads.
[0062] S306: Determine target thread parameters based on the minimum number of threads.
[0063] Generally, when the electronic device determines that the first number of threads is not less than the minimum number of threads, the electronic device can determine the target thread parameters based on the minimum number of threads. For example, if the electronic device determines that the first number of threads is 5 and the minimum number of threads is 2, the electronic device can use the minimum number of threads as 2 as the target thread parameter.
[0064] S307: Determine the target thread parameter based on the first number of threads.
[0065] Generally, when the electronic device determines that the first number of threads is less than the minimum number of threads, the electronic device can determine the target thread parameter based on the first number of threads. For example, if the electronic device determines that the first number of threads is 5 and the minimum number of threads is 10, the electronic device can use the first number of threads as 8 as the target thread parameter. This determination method can be determined and adjusted by empirical values without specific limitation.
[0066] S308. Determine the second number of threads according to the minimum number of threads and the number of tasks, and determine whether the second number of threads is greater than the number of currently running threads.
[0067] Generally, when the electronic device determines that the number of tasks is greater than the minimum number of threads, the electronic device can determine the second number of threads based on the minimum number of threads and the number of tasks. For example, the electronic device determines that the number of tasks is 2000 and the minimum number of threads is 15. The electronic device can determine that the second number of threads is 18 according to the formula: the second number of threads = the number of tasks / 1000+1+the minimum number of threads. The specific calculation method for determining the second number of threads in this application is not specifically limited. The formula and formula parameters can be determined and adjusted by empirical values, and are not specifically limited, which helps the electronic device to simply and conveniently determine the most appropriate thread pool capacity. Then the electronic device also needs to determine whether the second number of threads is greater than the number of threads currently running.
[0068] S309: Determine target thread parameters based on the current number of threads in the current thread pool.
[0069] Generally, when the electronic device determines that the second number of threads is not greater than the number of currently running threads, the electronic device can determine the target thread parameters based on the current number of threads in the current thread pool. For example, if the electronic device determines that the current number of threads in the current thread pool is 18, the current number of threads in the current thread pool is 10 as the target thread parameter. This determination method can be determined by the experience of researchers and is not specifically limited.
[0070] S310. Determine the target thread parameter based on the smaller value between the second number of threads and the maximum number of threads.
[0071] Generally, when the electronic device determines that the second number of threads is greater than the number of currently running threads, the electronic device can determine the target thread parameter based on the smaller value of the second number of threads and the maximum number of threads. For example, if the electronic device determines that the second number of threads is 18 and the maximum number of threads is 20, the electronic device can use the second number of threads as 18 as the target thread parameter. This determination method can be determined and adjusted by empirical values and is not specifically limited.
[0072] S311. Determine the corresponding target thread number based on the target thread parameter, and judge whether the occupied capacity corresponding to the target thread number is greater than the capacity of the current thread pool.
[0073] Generally, for each new thread created by a user, the electronic device will allocate a certain amount of memory to the user's process space as the stack of the new thread. If the stack is too large, it will waste electronic device resources. If the stack is too small, it will cause memory overflow and operation crash. The thread memory may include stack frames, local variables, method parameters, etc. The electronic device may configure the thread by setting the parameter information of the thread memory, and detect the thread usage information (such as but not limited to physical memory, number of threads, thread type, etc.) through query instructions (such as but not limited to smaps instructions, etc.). After determining the target thread parameter, the electronic device may determine the corresponding target thread number based on the target thread parameter, and determine whether the occupied capacity corresponding to the target thread number is greater than the capacity of the current thread pool. For example, the electronic device determines that the target thread parameter includes the corresponding target thread number as 18, and then further determines that the occupied capacity corresponding to 18 threads is 25M, and determines that the capacity of the current thread pool is 20M. The capacity and number of thread pools in electronic devices are not necessarily positively correlated. The capacity of the thread pool may be 10M, but there are only two threads. The capacity of the thread pool may be 5M, but there may be 5 threads. Therefore, it is more accurate to adjust the thread pool by comparing the capacity of the thread pool in this application.
[0074] S312: Expand the capacity of the current thread pool based on the difference between the occupied capacity corresponding to the target number of threads and the capacity of the current thread pool.
[0075] Generally, after the electronic device determines that the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool, it will expand the capacity of the current thread pool. For example, the electronic device determines that the occupied capacity corresponding to the target number of threads is 25M, and determines that the capacity of the current thread pool is 20M. The electronic device can calculate that the difference between the two is 5M, and then the electronic device needs to expand the capacity of the current thread pool from 20M to 25M. In one possible embodiment, when the power station equipment determines that the occupied capacity corresponding to the target number of threads is equal to the capacity of the current thread pool, it will not expand or shrink the capacity after determining that the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool. Figure 6 As shown, the current thread pool in the electronic device has capacities corresponding to four threads, namely, thread 1, thread 2, thread 3 and thread 4. Then, the electronic device can perform capacity expansion processing based on the current thread pool to increase the capacities of two threads, thread 4 and thread 5.
[0076] S313: Scaling the current thread pool based on the difference between the capacity of the current thread pool and the occupied capacity corresponding to the target number of threads.
[0077] Generally, after the electronic device determines that the occupied capacity corresponding to the target number of threads is less than the capacity of the current thread pool, it will shrink the current thread pool. For example, if the electronic device determines that the occupied capacity corresponding to the target number of threads is 25M and the capacity of the current thread pool is 30M, the electronic device can calculate that the difference between the two is 5M, and then the electronic device needs to expand the capacity of the current thread pool from 30M to 25M. Figure 7 As shown, the current thread pool in the electronic device has the capacity corresponding to six threads: thread 1, thread 2, thread 3, thread 4, thread 5 and thread 6. Then the electronic device can perform downscaling processing on the basis of the current thread pool to reduce the capacity of two idle threads: thread 4 and thread 5. In a possible embodiment, the electronic device can also receive modification instructions from the user in real time, and mediate the current thread pool based on the modification instructions. In a possible embodiment, the electronic device can also display the relevant information of the current thread pool through a display screen, which helps the user to intuitively monitor the thread running status of the electronic device, and can analyze and process in time if an error occurs.
[0078] In a possible embodiment, the electronic device can also collect target thread parameters within a period of time, and then determine the target thread pool parameters of the target thread pool in different time periods, and intelligently adjust the capacity of the current thread pool within the determined time period. For example: the electronic device collects all target thread parameters in March, and the capacity of the current thread pool is 30M from 18:00 to 20:00 every day, and 20M at other times. Then, starting from April, the electronic device can expand the current thread pool with a capacity of 20M to 30M from 18:00 to 20:00 every day, and shrink the current thread pool with a capacity of 30M to 20M at other times, thereby achieving the purpose of optimizing the resource utilization of the electronic device, effectively preventing thread pool blocking, and also avoiding the waste of resources caused by some thread pools being too idle or too busy due to the mismatch between thread pool resources and task volume.
[0079] When the scheme of the embodiment of the present application is executed, the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool are obtained at preset time intervals, the target thread parameters of the target thread pool corresponding to the task pool are determined according to the task parameters and the current thread parameters, and the capacity of the current thread pool is adjusted according to the target thread parameters. In the present application, the electronic device can simply and accurately adjust the capacity of the current thread pool according to the preset calculation strategy by periodically determining the task parameters of the task pool and the current thread parameters of the corresponding current thread pool at preset time intervals. When there are too many tasks, the efficiency and performance of the thread pool execution service can be improved to minimize the task accumulation. When there are too few tasks, the number of threads can be reduced to save system resources.
[0080] The following is an embodiment of the device of the present application, which can be used to execute the embodiment of the method of the present application. For details not disclosed in the embodiment of the device of the present application, please refer to the embodiment of the method of the present application.
[0081] See also Figure 8 , which shows a schematic diagram of the structure of a thread pool adjustment device provided by an exemplary embodiment of the present application, hereinafter referred to as the adjustment device 8. The adjustment device 8 can be implemented as all or part of the terminal through software, hardware or a combination of both. It includes:
[0082] An acquisition module 801 is used to acquire the task parameters of a task pool and the current thread parameters of a current thread pool corresponding to the task pool at preset intervals;
[0083] A determination module 802 is used to determine a target thread parameter of a target thread pool corresponding to the task pool according to the task parameter and the current thread parameter;
[0084] The adjustment module 803 is used to adjust the capacity of the current thread pool according to the target thread parameter.
[0085] In a possible embodiment, the adjusting device 8 further includes:
[0086] A second acquisition module is used to acquire initialization configuration information, and determine at least a default number of threads, a minimum number of threads, and a maximum number of threads of the current thread pool according to the initialization configuration information;
[0087] The second determining module is used to initialize the current thread pool based on the initialization configuration information, the default number of threads, the minimum number of threads and the maximum number of threads.
[0088] In a possible embodiment, the determining module 802 includes:
[0089] A first determining unit, configured to determine a first number of threads according to the number of currently running threads if the number of tasks is less than or equal to the minimum number of threads;
[0090] a second determining unit, configured to determine a target thread parameter based on the minimum number of threads if the first number of threads is less than the minimum number of threads;
[0091] The third determining unit is configured to determine the target thread parameter based on the first number of threads if the first number of threads is greater than or equal to the minimum number of threads.
[0092] In a possible embodiment, the determining module 802 includes:
[0093] a fourth determining unit, configured to determine the second number of threads according to the minimum number of threads and the number of tasks if the number of tasks is greater than the minimum number of threads;
[0094] a fifth determining unit, configured to determine a target thread parameter based on the current number of threads in the current thread pool if the second number of threads is less than or equal to the number of currently running threads;
[0095] The sixth determining unit is configured to determine the target thread parameter based on a smaller value between the second number of threads and the maximum number of threads if the second number of threads is greater than the number of currently running threads.
[0096] In a possible embodiment, the adjustment module 803 includes:
[0097] A comparison unit, configured to determine the corresponding target thread number based on the target thread parameter, and compare the occupied capacity corresponding to the target thread number with the capacity of the current thread pool;
[0098] The capacity expansion unit is used to expand the current thread pool based on the difference between the occupied capacity corresponding to the target number of threads and the capacity of the current thread pool if the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool.
[0099] In a possible embodiment, the adjustment module 803 includes:
[0100] The capacity reduction unit is used to reduce the capacity of the current thread pool based on the difference between the capacity of the current thread pool and the occupied capacity corresponding to the target number of threads if the occupied capacity corresponding to the target number of threads is less than the capacity of the current thread pool.
[0101] The embodiments of this application and Figures 2 to 3 The method embodiments are based on the same concept, and the technical effects they bring are also the same. The specific process can be referred to Figures 2 to 3 The description of the method embodiment will not be repeated here.
[0102] The device 8 can be a field-programmable gate array (FPGA), a dedicated integrated chip, a system on chip (SoC), a central processor unit (CPU), a network processor (NP), a digital signal processing circuit, a microcontroller unit (MCU), and a programmable logic device (PLD) or other integrated chips.
[0103] When the scheme of the embodiment of the present application is executed, the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool are obtained at preset time intervals, the target thread parameters of the target thread pool corresponding to the task pool are determined according to the task parameters and the current thread parameters, and the capacity of the current thread pool is adjusted according to the target thread parameters. In the present application, the electronic device can simply and accurately adjust the capacity of the current thread pool according to the preset calculation strategy by periodically determining the task parameters of the task pool and the current thread parameters of the corresponding current thread pool at preset time intervals. When there are too many tasks, the efficiency and performance of the thread pool execution service can be improved to minimize the task accumulation. When there are too few tasks, the number of threads can be reduced to save system resources.
[0104] The present application also provides a computer storage medium, which can store multiple instructions, which are suitable for being loaded by a processor and executing the above method steps. The specific execution process can be seen in Figure 2 or Figure 3 The specific description of the illustrated embodiment will not be repeated here.
[0105] The present application also provides a computer program product, which stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement the template control method described in the above embodiments.
[0106] See also Fig. 9 , is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Fig. 9 As shown, the electronic device 9 may include: at least one processor 901 , at least one network interface 904 , a user interface 903 , a memory 905 , and at least one communication bus 902 .
[0107] The communication bus 902 is used to realize the connection and communication between these components.
[0108] The user interface 903 may include a display screen (Display) and a camera (Camera), and the optional user interface 903 may also include a standard wired interface and a wireless interface.
[0109] The network interface 904 may optionally include a standard wired interface or a wireless interface (such as a WI-FI interface).
[0110] Among them, the processor 901 may include one or more processing cores. The processor 901 uses various interfaces and lines to connect various parts in the entire terminal 900, and executes various functions and processes data of the terminal 900 by running or executing instructions, programs, code sets or instruction sets stored in the memory 905, and calling data stored in the memory 905. Optionally, the processor 901 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 901 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; and the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 901, but implemented separately through a chip.
[0111] Among them, the memory 905 may include a random access memory (Random Access Memory, RAM) and may also include a read-only memory (Read-Only Memory). Optionally, the memory 905 includes a non-transitory computer-readable storage medium. The memory 905 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 905 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 905 may also be optionally at least one storage device located away from the aforementioned processor 901. As Fig. 9 As shown, the memory 905 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a thread pool adjustment application.
[0112] exist Fig. 9 In the electronic device 900 shown, the user interface 903 is mainly used to provide an input interface for the user and obtain data input by the user; and the processor 901 can be used to call the thread pool adjustment application stored in the memory 905, and specifically perform the following operations:
[0113] Obtaining task parameters of a task pool and current thread parameters of a current thread pool corresponding to the task pool at preset intervals;
[0114] Determine the target thread parameter of the target thread pool corresponding to the task pool according to the task parameter and the current thread parameter;
[0115] The capacity of the current thread pool is adjusted according to the target thread parameter.
[0116] In one embodiment, before the processor 901 executes the step of obtaining the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool at every preset time, it is also used to execute:
[0117] Obtaining initialization configuration information, and determining at least a default number of threads, a minimum number of threads, and a maximum number of threads of the current thread pool according to the initialization configuration information;
[0118] Initialize the current thread pool based on the initialization configuration information, the default number of threads, the minimum number of threads, and the maximum number of threads.
[0119] In one embodiment, when the processor 901 executes the step of obtaining the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool at every preset time, the following steps are specifically performed:
[0120] The number of currently stored tasks in the task pool and the number of currently running threads in the current thread pool corresponding to the task pool are obtained at preset intervals.
[0121] In one embodiment, when the processor 901 determines the target thread parameter of the target thread pool corresponding to the task pool according to the task parameter and the current thread parameter, it specifically performs:
[0122] If the number of tasks is less than or equal to the minimum number of threads, determining the first number of threads according to the number of currently running threads;
[0123] If the first number of threads is less than the minimum number of threads, determining a target thread parameter based on the minimum number of threads;
[0124] If the first number of threads is greater than or equal to the minimum number of threads, the target thread parameter is determined based on the first number of threads.
[0125] In one embodiment, when the processor 901 determines the target thread parameter of the target thread pool corresponding to the task pool according to the task parameter and the current thread parameter, it further specifically executes:
[0126] If the number of tasks is greater than the minimum number of threads, determining the second number of threads according to the minimum number of threads and the number of tasks;
[0127] If the second number of threads is less than or equal to the number of currently running threads, determining the target thread parameters based on the current number of threads in the current thread pool;
[0128] If the second number of threads is greater than the number of currently running threads, the target thread parameter is determined based on a smaller value between the second number of threads and the maximum number of threads.
[0129] In one embodiment, when the processor 901 performs the step of adjusting the capacity of the current thread pool according to the target thread parameter, the processor 901 specifically performs:
[0130] Determining a corresponding target thread number based on the target thread parameter, and comparing an occupied capacity corresponding to the target thread number with a capacity of the current thread pool;
[0131] If the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool, the current thread pool is expanded based on the difference between the occupied capacity corresponding to the target number of threads and the capacity of the current thread pool.
[0132] In one embodiment, when the processor 901 performs the step of adjusting the capacity of the current thread pool according to the target thread parameter, the processor 901 specifically performs:
[0133] If the occupied capacity corresponding to the target number of threads is smaller than the capacity of the current thread pool, the current thread pool is scaled down based on the difference between the capacity of the current thread pool and the occupied capacity corresponding to the target number of threads.
[0134] The technical concept and Figure 2 or Figure 3 The technical concept is the same as that of Figure 2 or Figure 3 The method embodiments are not described in detail here.
[0135] In an embodiment of the present application, the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool are obtained at preset time intervals, the target thread parameters of the target thread pool corresponding to the task pool are determined according to the task parameters and the current thread parameters, and the capacity of the current thread pool is adjusted according to the target thread parameters. In the present application, the electronic device can simply and accurately adjust the capacity of the current thread pool according to the preset calculation strategy by periodically determining the task parameters of the task pool and the current thread parameters of the corresponding current thread pool at preset time intervals. When there are too many tasks, the efficiency and performance of the thread pool execution service can be improved to minimize the task accumulation. When there are too few tasks, the number of threads can be reduced to save system resources.
[0136] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium can be a disk, an optical disk, a read-only storage memory, or a random access memory, etc.
[0137] The above disclosure is only the preferred embodiment of the present application, which certainly cannot be used to limit the scope of rights of the present application. Therefore, equivalent changes made according to the claims of the present application are still within the scope covered by the present application.
Claims
1. A thread pool adjustment method, characterized in that: The method comprises: Obtaining task parameters of a task pool and current thread parameters of a current thread pool corresponding to the task pool at preset intervals, wherein the task parameters include at least the number of tasks currently stored in the task pool, and the current thread parameters include at least the number of threads currently running in the current thread pool corresponding to the task pool; If the number of tasks is less than or equal to the minimum number of threads, determining a first number of threads according to the number of currently running threads, and determining a target thread parameter according to the first number of threads and the minimum number of threads; If the number of tasks is greater than the minimum number of threads, determining a second number of threads according to the minimum number of threads and the number of tasks, and determining the target thread parameter according to the second number of threads and the number of currently running threads; Adjusting the capacity of the current thread pool according to the target thread parameter; The step of determining a target thread parameter according to the first number of threads and the minimum number of threads includes: If the first number of threads is less than the minimum number of threads, determining the target thread parameter based on the first number of threads; If the first number of threads is greater than or equal to the minimum number of threads, determining the target thread parameter based on the minimum number of threads; The determining the target thread parameter according to the second number of threads and the number of currently running threads includes: If the second number of threads is less than or equal to the number of currently running threads, determining the target thread parameter based on the current number of threads in the current thread pool; If the second number of threads is greater than the number of currently running threads, determining the target thread parameter based on the smaller value between the second number of threads and the maximum number of threads; The adjusting the capacity of the current thread pool according to the target thread parameter includes: Determining a corresponding target thread number based on the target thread parameter, and comparing an occupied capacity corresponding to the target thread number with a capacity of the current thread pool; If the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool, the current thread pool is expanded based on the difference between the occupied capacity corresponding to the target number of threads and the capacity of the current thread pool; If the occupied capacity corresponding to the target number of threads is smaller than the capacity of the current thread pool, the current thread pool is scaled down based on the difference between the capacity of the current thread pool and the occupied capacity corresponding to the target number of threads.
2. The method according to claim 1, characterized in that: Before obtaining the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool at preset intervals, the method further includes: Obtaining initialization configuration information, and determining at least a default number of threads, a minimum number of threads, and a maximum number of threads of the current thread pool according to the initialization configuration information; Initialize the current thread pool based on the initialization configuration information, the default number of threads, the minimum number of threads, and the maximum number of threads.
3. The method according to claim 2, characterized in that: The step of obtaining the task parameters of the task pool and the current thread parameters of the current thread pool corresponding to the task pool at preset intervals includes: The number of currently stored tasks in the task pool and the number of currently running threads in the current thread pool corresponding to the task pool are obtained at preset time intervals.
4. A thread pool adjustment device, characterized in that: include: An acquisition module, used for acquiring task parameters of a task pool and current thread parameters of a current thread pool corresponding to the task pool at preset intervals, wherein the task parameters include at least the number of tasks currently stored in the task pool, and the current thread parameters include at least the number of threads currently running in the current thread pool corresponding to the task pool; a determination module, configured to determine a first number of threads according to the number of currently running threads if the number of tasks is less than or equal to the minimum number of threads, and determine a target thread parameter according to the first number of threads and the minimum number of threads; The determination module is further configured to determine, if the number of tasks is greater than the minimum number of threads, a second number of threads according to the minimum number of threads and the number of tasks, and determine the target thread parameter according to the second number of threads and the number of currently running threads; An adjustment module, used for adjusting the capacity of the current thread pool according to the target thread parameter; The determination module is further configured to determine the target thread parameter based on the first number of threads if the first number of threads is less than the minimum number of threads; If the first number of threads is greater than or equal to the minimum number of threads, determining the target thread parameter based on the minimum number of threads; The determination module is further configured to determine the target thread parameter based on the current number of threads in the current thread pool if the second number of threads is less than or equal to the number of currently running threads; If the second number of threads is greater than the number of currently running threads, determining the target thread parameter based on the smaller value between the second number of threads and the maximum number of threads; The adjustment module is further used to determine the corresponding target thread number based on the target thread parameter, and compare the occupied capacity corresponding to the target thread number with the capacity of the current thread pool; If the occupied capacity corresponding to the target number of threads is greater than the capacity of the current thread pool, the current thread pool is expanded based on the difference between the occupied capacity corresponding to the target number of threads and the capacity of the current thread pool; if the occupied capacity corresponding to the target number of threads is less than the capacity of the current thread pool, the current thread pool is reduced based on the difference between the capacity of the current thread pool and the capacity corresponding to the target number of threads.
5. A computer storage medium, characterized in that: The computer storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the method steps as claimed in any one of claims 1 to 3.
6. An electronic device, characterized in that: include: A memory and a processor; wherein the memory stores a computer program, and the computer program is suitable for being loaded by the processor and executing the method steps as claimed in any one of claims 1 to 3.
