Method, system and device for managing threads in application program and medium
By scanning and analyzing application class files, identifying and managing scattered thread pools, and instrumenting agents for different SDKs, the problem of inflexible thread pool management in the existing technology is solved, and centralized management of thread pools is realized, which improves the System performance and user experience.
Patent Information
- Application Number
- CN202411430462.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-14
- Publication Date
- 2025-01-14
AI Technical Summary
When managing thread pools in applications, it is difficult to flexibly adapt to the processing requirements of different SDKs, resulting in performance problems such as increasing thread count, increasing CPU consumption, memory overflow and application lag.
By scanning and analyzing application class files, identifying and managing scattered thread pools, directing their own SDKs to centralized thread pools, instrumenting agents for third-party SDKs that do not have predefined interfaces, realizing thread pool redirection and avoiding Modify the source code.
It realizes centralized management of thread pools, improves resource utilization efficiency and system performance, reduces the overhead of multi-thread management, reduces memory and CPU consumption, and improves user experience.
Smart Images

Figure CN119311418A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of thread management, and in particular to a method, system, device and medium for managing a thread pool in an application. Background Art
[0002] In the process of daily application development, it is usually necessary to rely on multiple third-party SDKs (Software Development Kits), and each SDK usually creates its own thread pool, which greatly increases the number of threads of the application, resulting in increased CPU (Central Processing Unit) consumption, memory overflow, and even performance problems such as application freezes in severe cases. To solve this problem, thread convergence is usually used, that is, each thread pool is proxied to a custom centralized thread pool. However, this method is heavily dependent on the interface support provided by the third-party SDK. Once the third party does not provide an interface, it is necessary to modify the source code of the third party to achieve rectification, but the promotion of this rectification method is difficult, especially for the adjustment of the third-party SDK. This method often leads to incomplete convergence of threads and limited overall optimization. Therefore, it is necessary to provide a management method, system, device and medium for the thread pool in an application. Summary of the invention
[0003] In view of the above-mentioned shortcomings of the prior art, an object of the present invention is to provide a method, system, device and medium for managing a thread pool in an application, which improves the problem that the prior art cannot flexibly adapt to the processing requirements of different SDKs.
[0004] To achieve the above-mentioned purpose and other related purposes, the present invention provides a method for managing a thread pool in an application, comprising: scanning an application to be analyzed to obtain all class files on which the application depends; accessing and analyzing each class file, identifying a decentralized thread pool where each class method in each class file exists, and determining the SDK to which each class method in the decentralized thread pool belongs: if the SDK is a self-owned SDK, setting a thread pool interface so that the SDK is directed to a pre-created centralized thread pool; if the SDK is a third-party SDK and the SDK does not have a pre-defined thread pool interface, performing an instrumentation agent on the SDK so that the SDK is redirected to a pre-created centralized thread pool.
[0005] In one embodiment of the present invention, the accessing and analyzing each class file, identifying the thread pool where each class method in each class file exists, and determining the SDK to which each class method with a thread pool belongs, includes: accessing and analyzing each class file based on the ASM library, extracting all class methods included in each class file; for each class method: judging whether there is a corresponding thread pool for the class method according to the identifier of the class method, and identifying the class name to which the class method belongs when a thread pool exists; and determining the SDK to which each class method belongs according to each class name.
[0006] In one embodiment of the present invention, the SDK type to which each class method belongs is determined based on each class name, including: for each class name: comparing the class name with the third-party category library corresponding to the preset third-party SDK, to determine whether the two match: if they match, the SDK to which the class method corresponding to the class name belongs is the third-party SDK; if they do not match, the SDK to which the class method corresponding to the class name belongs is the self-owned SDK.
[0007] In one embodiment of the present invention, if the SDK is a self-owned SDK, a thread pool interface is set to direct the SDK to a pre-created centralized thread pool, including: if the SDK is a self-owned SDK, a thread pool interface is set in the SDK, and the pointing position of the thread pool interface is determined: if the thread pool interface points to the centralized thread pool, the SDK is directed to the centralized thread pool through the thread pool interface; if the thread pool interface points to a decentralized thread pool, and the SDK is not in the preset whitelist, the decentralized thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface.
[0008] In one embodiment of the present invention, if the thread pool interface points to a distributed thread pool and the SDK is not in a preset whitelist, the distributed thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface, including: determining whether the SDK is in the whitelist; if the SDK is not in the whitelist, the distributed thread pool pointed to by the thread pool interface is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface; if the SDK is in the whitelist, the SDK is instrumented as a proxy to redirect the SDK to the centralized thread pool.
[0009] In one embodiment of the present invention, if the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, the SDK is instrumented as an agent to redirect the SDK to a pre-created centralized thread pool, including: if the SDK is a third-party SDK, determining whether the SDK has a predefined thread pool interface: if so, directing the SDK to the centralized thread pool through the thread pool interface; if not, instrumenting the SDK as an agent to redirect the SDK to the centralized thread pool.
[0010] In one embodiment of the present invention, the SDK is instrumented as an agent so that the SDK is redirected to the centralized thread pool, including: based on static instrumentation, the bytecode file of the SDK is modified to replace the creation instruction of the corresponding decentralized thread pool with the acquisition instruction of the centralized thread pool.
[0011] In one embodiment of the present invention, a management system for thread pools in an application is also provided, the system comprising: a class file acquisition module, used to scan the application to be analyzed and obtain all class files on which the application depends; an SDK judgment module, used to access and analyze each class file, identify the decentralized thread pool where each class method in each class file exists, and determine the SDK to which each class method in the decentralized thread pool belongs: an SDK orientation module, used to set a thread pool interface if the SDK is a self-owned SDK, so that the SDK is directed to a pre-created centralized thread pool; an SDK redirection module, used to perform an instrumentation agent on the SDK if the SDK is a third-party SDK and the SDK does not have a pre-defined thread pool interface, so that the SDK is redirected to a pre-created centralized thread pool.
[0012] In one embodiment of the present invention, an electronic device is also provided, comprising: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the electronic device implements the thread pool management method in the application described in any one of the above items.
[0013] In one embodiment of the present invention, a computer-readable storage medium is further provided, on which a computer program is stored. When the computer program is executed by a processor of a computer, the computer executes any of the above-mentioned methods for managing a thread pool in an application.
[0014] As described above, the management method, system, device and medium of a thread pool in an application of the present invention have the following beneficial effects: by scanning and analyzing the application and all the class files it depends on, identifying and managing the decentralized thread pools therein, directing the own SDK to the centralized thread pool, performing plugging proxy for third-party SDKs that do not have predefined interfaces, and redirecting the thread pool without modifying the source code, thereby realizing centralized management of the thread pool. This method of the present invention can flexibly adapt to the processing requirements of different SDKs, effectively improve resource utilization efficiency and system performance, and reduce the overhead of multi-threaded management. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 A flowchart of a method for managing a thread pool in an application provided by an embodiment of the present invention;
[0016] Figure 2 Shown is a structural block diagram of a management system of a thread pool in an application provided by an embodiment of the present invention;
[0017] Figure 3 Shown is a structural schematic diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0018] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0019] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention, and thus the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0020] In the following description, numerous details are discussed to provide a more thorough explanation of the embodiments of the present invention. However, it is obvious to those skilled in the art that the embodiments of the present invention can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present invention difficult to understand.
[0021] The present invention provides a method for managing a thread pool in an application. By scanning and analyzing the application and all the class files it depends on, the decentralized thread pools therein are identified and managed, the own SDK is directed to the centralized thread pool, and a third-party SDK that does not have a predefined interface is plugged in as a proxy. The thread pool can be redirected without modifying the source code, thereby realizing the centralized management of the thread pool. This method of the present invention can flexibly adapt to the processing requirements of different SDKs, effectively improve resource utilization efficiency and system performance, and reduce the overhead of multi-threaded management. Furthermore, through plug-in scanning, all forms of thread and thread pool creation operations at the application and SDK layers can be accurately identified, thereby ensuring the comprehensiveness of centralized management. Utilizing the thread pool convergence method of the present invention, the system's memory and CPU consumption rates are effectively reduced, the application's freeze rate is reduced, and the user experience is greatly improved.
[0022] See also Figure 1 , the thread pool management method in the application includes the following steps:
[0023] S1. Scan the application to be analyzed and obtain all class files that the application depends on.
[0024] During the development of the application, a predefined Gradle plug-in (which may be named ThreadTrackerPlugin) is used, and a predefined Transform task (which may be named ThreadTrackerTransform) is registered in the plug-in ThreadTrackerPlugin. Among them, the Transform task can scan the application before the bytecode of the application is converted into a DEX file, traverse and process the application itself and all the libraries it depends on, thereby scanning out all the class files (i.e., class files) required for the application to run, so as to modify or analyze the class files on which the application depends. It is understandable that those skilled in the art can adaptively define the relevant content of the Gradle plug-in and Transform based on the actual application needs, and no limitation is made here.
[0025] S2. Access and analyze each class file, identify the distributed thread pools existing in each class method in each class file, and determine the SDK to which each class method having the distributed thread pool belongs.
[0026] Access and analyze each class file in the application and its dependent libraries, traverse all class methods in each class file, and check the bytecode instructions in each class method step by step, so as to identify whether there is a creation instruction for a distributed thread pool in the currently checked class method, so as to determine whether there is a distributed thread pool that has been created for this class method. Among them, a distributed thread pool refers to a thread pool that is created for the first time in a class method. Through the above process, it is possible to determine which class methods in all class methods in the application and its dependent libraries have created a distributed thread pool, and the specific SDK to which the class method belongs can be determined through the class name of the class method.
[0027] In one embodiment of the present invention, the accessing and analyzing each class file, identifying the thread pool where each class method in each class file exists, and determining the SDK to which each class method in the thread pool belongs includes:
[0028] Access and analyze each class file based on the ASM library, and extract all class methods included in each class file;
[0029] For each class method: according to the identifier of the class method, determine whether there is a corresponding thread pool for the class method, and identify the class name to which the class method belongs if there is a thread pool;
[0030] Based on each class name, determine the SDK to which each class method belongs.
[0031] All class files in the application and dependent libraries are accessed and analyzed through the ASM library. For each class file, its internal structure is traversed and analyzed through the ASM library to extract all class methods in each class file. For each class method, its bytecode instructions are analyzed to determine whether the currently analyzed class method has a decentralized thread pool creation instruction. If so, it means that the current class method has a decentralized thread pool that has been created. At this time, the class name to which the method belongs can be identified and recorded for subsequent determination of the SDK source of the method. On the contrary, if there is no decentralized thread pool for the class method, the next class method is analyzed until all class methods in the application and dependent libraries are analyzed. It should be noted that the package prefix in the class name can indicate whether the SDK to which the method belongs is a third-party SDK. If the package prefix is "Tencent", it can be regarded as a third-party SDK, and if the package prefix is "huolala", it can be regarded as a self-owned SDK. Therefore, the SDK to which each class method belongs can be determined by each class name.
[0032] Specifically, in the transform method of the predefined Transform (i.e., ThreadTrackerTransform), all class files in the project of the application are traversed. When traversing the class files, the ASM library returns a ClassVisitor object for in-depth access and analysis of the structure of the class file. When the ClassVisitor object scans a class method, it will call back the visitMethod() method of the ClassVisitor to analyze the currently scanned class method. In the callback visitMethod() method, by analyzing the bytecode instructions of the class method, the access identification information, the class to which it belongs, the method name, the descriptor and other features in the visitMethod() method are identified, thereby identifying whether the method has a creation instruction for a distributed thread pool, and identifying and outputting the class name of the method if it exists. Further, in order to improve the subsequent SDK recognition rate, all matched class names can be output to a file (which can be named thread_tracker_XXX.txt). According to the dependency tree information of the application, these class names in the above file are compared with the preset third-party category library to divide which SDK the class name belongs to.
[0033] In an embodiment of the present invention, determining the SDK type to which each class method belongs based on each class name includes:
[0034] For each class name:
[0035] Compare the class name with the third-party category library corresponding to the preset third-party SDK to determine whether the two match:
[0036] If they match, the SDK to which the class method corresponding to the class name belongs is a third-party SDK;
[0037] If they do not match, the SDK to which the class method corresponding to the class name belongs is your own SDK.
[0038] For third-party SDKs, a third-party category library corresponding to a third-party SDK will first be created in advance. In this category library, all third-party SDKs that the application depends on and their package prefixes or other features that can identify the source of the SDK will be listed. For each class name, the package prefix of the class name is compared one by one with the package prefix pre-stored in the third-party category library to determine whether the two match. If they match, it means that the SDK to which the class method corresponding to the class name belongs is a third-party SDK. Exemplarily, if the class name is com.google.maps.MapView and the package prefix com.google exists in the third-party category library, the SDK to which the class method belongs is a third-party SDK. On the contrary, if the package prefix of the class name does not match all the package prefixes in the third-party category library, it means that the corresponding class method is a self-owned SDK. Among them, a third-party SDK refers to an SDK developed by an external company or organization, but with limited program modification permissions. Self-owned SDK refers to an SDK developed internally by the application, so it does not have to rely on external suppliers and can modify the program code by itself.
[0039] S3. If the SDK is a self-owned SDK, a thread pool interface is set to direct the SDK to a pre-created centralized thread pool.
[0040] If it is determined that the SDK of a class method is a self-owned SDK, the creation location of the decentralized thread pool can be located by analyzing the source code of the self-owned SDK. At this time, a custom thread pool interface can be set in the SDK, and the self-owned SDK can be directed to the pre-created centralized thread pool by setting the interface parameters.
[0041] Specifically, in one embodiment of the present invention, if the SDK is a self-owned SDK, a thread pool interface is set to direct the SDK to a pre-created centralized thread pool, including:
[0042] If the SDK is your own SDK, set the thread pool interface in the SDK and determine the location where the thread pool interface points:
[0043] If the thread pool interface points to the centralized thread pool, directing the SDK to the centralized thread pool through the thread pool interface;
[0044] If the thread pool interface points to a decentralized thread pool and the SDK is not in the preset whitelist, the decentralized thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface.
[0045] If the SDK of a certain type of method is a self-owned SDK, since the self-owned SDK can have greater programming permissions, a custom thread pool interface can be directly set in the self-owned SDK, and the current thread pool interface can be determined by analyzing the interface parameters or the actual call results of the interface. If the thread pool interface points to a centralized thread pool, the SDK can be located in the centralized thread pool, and the tasks of the SDK can be distributed in the centralized thread pool. On the contrary, if the thread pool interface points to a decentralized thread pool that already existed before the method of this type, and the SDK is not in the preset whitelist, it means that the decentralized thread pool has certain specific requirements and needs to be processed independently, and it cannot be changed to the centralized thread pool. At this time, the decentralized thread pool is used as a centralized thread pool, and the corresponding SDK is directed to the decentralized thread pool. Among them, the whitelist records decentralized thread pools that can be centrally managed, and the decentralized thread pools in the whitelist can be unified into the centralized thread pool to achieve effective use of resources. By setting a whitelist, resource allocation can be effectively controlled to ensure that centralized management is applicable to appropriate scenarios.
[0046] In one embodiment of the present invention, if the thread pool interface points to a decentralized thread pool and the SDK is not in a preset whitelist, the decentralized thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface, including:
[0047] Determine whether the SDK is in the whitelist:
[0048] If the SDK is not in the whitelist, the decentralized thread pool pointed to by the thread pool interface is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface;
[0049] If the SDK is in the whitelist, an agent is inserted into the SDK to redirect the SDK to the centralized thread pool.
[0050] When it is determined that the SDK to which a certain type of method belongs is a proprietary SDK, and its thread pool interface is identified as pointing to a decentralized thread pool, it is also necessary to identify whether the proprietary SDK is in the preset whitelist to determine whether the SDK is processed independently. If the SDK is not in the whitelist, it means that it needs to be processed independently, so the corresponding decentralized thread pool is used as a centralized thread pool, and the SDK is directly pointed to the decentralized thread pool through the thread pool interface. Conversely, if the SDK is in the whitelist, it means that it can be processed centrally, and its thread pool interface can be pointed to the centralized thread pool by means of a plug-in proxy. Furthermore, for SDKs that are not in the whitelist, a timeout destruction mechanism can be set, that is, after the SDK runs for a preset period of time, its corresponding decentralized thread pool is destroyed, and a new decentralized thread pool is re-established when it is needed again next time, thereby reducing resource usage.
[0051] S4. If the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, an instrumentation agent is performed on the SDK to redirect the SDK to a pre-created centralized thread pool.
[0052] If the SDK to which the class method belongs is a third-party SDK, and the SDK does not support the predefined thread pool interface, it means that its tasks cannot be redirected to the centralized thread pool through simple configuration, so an instrumentation agent is required to achieve redirection.
[0053] Specifically, in one embodiment of the present invention, if the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, the SDK is instrumented as a proxy so that the SDK is redirected to a pre-created centralized thread pool, including:
[0054] If the SDK is a third-party SDK, determine whether the SDK has a predefined thread pool interface:
[0055] If yes, directing the SDK to the centralized thread pool through the thread pool interface;
[0056] If not, an instrumentation agent is performed on the SDK to redirect the SDK to the centralized thread pool.
[0057] The third-party SDK indicates that it does not have the permission to modify the source code. Therefore, if the SDK is a third-party SDK, first check whether it has a custom thread pool interface. If it does, the SDK can be directed to the pre-built centralized thread pool by modifying the parameters, so that the concurrent tasks of each SDK are processed in the centralized thread pool to improve resource utilization and reduce memory usage. On the contrary, if the SDK does not have a custom thread pool interface, it cannot be directed by simply changing the parameters. At this time, the proxy can be gradually proxied to the centralized thread pool through the plug-in proxy.
[0058] In one embodiment of the present invention, the SDK is instrumented as an agent so that the SDK is redirected to the centralized thread pool, including: based on static instrumentation, the bytecode file of the SDK is modified to replace the creation instruction of the corresponding decentralized thread pool with the acquisition instruction of the centralized thread pool. Through the static instrumentation technology, the bytecode file is directly modified in the compilation stage without modifying the source code of the SDK. Specifically, the bytecode file of the SDK can be loaded through the ASM library, and the file can be statically analyzed to identify and locate the location of the thread pool creation instruction contained therein, and the original thread pool creation instruction can be replaced with the acquisition instruction of the centralized thread pool, and a new bytecode file can be generated so that the application can use the new bytecode file at runtime. In this way, the original decentralized thread pool of the SDK can be replaced by the centralized thread pool, ensuring that all thread pool creation operations no longer generate decentralized independent thread pool instances, but uniformly point to the pre-created centralized thread pool.
[0059] See also Figure 2 , the management system 100 of the thread pool in the application includes: a class file acquisition module 110, an SDK judgment module 120, an SDK orientation module and an SDK redirection module 140. The above-mentioned class file acquisition module 110 is used to scan the application to be analyzed and obtain all class files on which the application depends. The SDK judgment module 120 is used to access and analyze each class file, identify the decentralized thread pool where each class method in each class file exists, and determine the SDK to which each class method in the decentralized thread pool belongs. The SDK orientation module 130 is used to set the thread pool interface if the SDK is its own SDK, so that the SDK is directed to a pre-created centralized thread pool. The SDK redirection module 140 is used to perform an instrumentation agent on the SDK if the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, so that the SDK is redirected to a pre-created centralized thread pool.
[0060] For the specific definition of the management system of the thread pool in the application, please refer to the definition of the management method of the thread pool in the application above, which will not be repeated here. Each module in the management system of the thread pool in the above application can be implemented in whole or in part by software, hardware and a combination thereof. Each of the above modules can be embedded in or independent of the processor in the computer device in hardware format, or can be stored in the memory in the computer device in software format, so that the processor can call the operations corresponding to each of the above modules.
[0061] It should be noted that, in order to highlight the innovative part of the present invention, the present embodiment does not introduce modules that are not closely related to solving the technical problem proposed by the present invention, but this does not mean that there are no other modules in the present embodiment.
[0062] See also Figure 3 The electronic device 1 may include a memory 12, a processor 13 and a bus, and may also include a computer program stored in the memory 12 and executable on the processor 13, such as a management program for a thread pool in an application.
[0063] Among them, the memory 12 includes at least one type of readable storage medium, and the readable storage medium includes flash memory, mobile hard disk, multimedia card, card-type memory (for example: SD or DX memory, etc.), magnetic memory, disk, optical disk, etc. In some embodiments, the memory 12 can be an internal storage unit of the electronic device 1, such as a mobile hard disk of the electronic device 1. In other embodiments, the memory 12 can also be an external storage device of the electronic device 1, such as a plug-in mobile hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the electronic device 1. Further, the memory 12 can also include both an internal storage unit of the electronic device 1 and an external storage device. The memory 12 can not only be used to store application software and various types of data installed in the electronic device 1, such as the code for managing the thread pool in the application, but also can be used to temporarily store data that has been output or is to be output.
[0064] In some embodiments, the processor 13 may be composed of an integrated circuit, for example, a single packaged integrated circuit, or a plurality of packaged integrated circuits with the same or different functions, including one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and combinations of various control chips. The processor 13 is the control core (Control Unit) of the electronic device 1, and uses various interfaces and lines to connect various components of the entire electronic device 1, and executes or executes programs or modules stored in the memory 12 (such as the management program of the thread pool in the application program, etc.), and calls the data stored in the memory 12 to execute various functions of the electronic device 1 and process data.
[0065] The processor 13 executes the operating system and various installed applications of the electronic device 1. The processor 13 executes the application to implement the steps in the thread pool management method in the above application.
[0066] Exemplarily, the computer program may be divided into one or more modules, which are stored in the memory 12 and executed by the processor 13 to complete the present application. The one or more modules may be a series of computer program instruction segments capable of completing specific functions, which are used to describe the execution process of the computer program in the electronic device 1. For example, the computer program may be divided into a class file acquisition module 110, an SDK judgment module 120, an SDK orientation module, and an SDK redirection module 140.
[0067] The above-mentioned integrated unit implemented in the form of a software function module can be stored in a computer-readable storage medium, and the computer-readable storage medium can be non-volatile or volatile. The above-mentioned software function module is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, a computer device, or a network device, etc.) or a processor to perform part of the functions of the thread pool management method in the application described in each embodiment of the present application.
[0068] In summary, the present invention discloses a method, system, device and medium for managing a thread pool in an application, which scans and analyzes all class files of the application and its dependencies, identifies and manages the decentralized thread pools therein, directs the own SDK to the centralized thread pool, and performs plug-in proxy for third-party SDKs that do not have predefined interfaces, so as to realize the redirection of the thread pool without modifying the source code, thereby realizing the centralized management of the thread pool. This method of the present invention can flexibly adapt to the processing requirements of different SDKs, effectively improve resource utilization efficiency and system performance, and reduce the cost of multi-threaded management. Further, through plug-in scanning, all forms of thread and thread pool creation operations at the application and SDK layers can be accurately identified, thereby ensuring the comprehensiveness of centralized management. Utilizing the thread pool convergence method of the present invention, the memory and CPU consumption rate of the system are effectively reduced, the jam rate of the application is reduced, and the user experience is greatly improved. Therefore, the present invention effectively overcomes the various shortcomings in the prior art and has a high industrial utilization value.
[0069] The above embodiments are merely illustrative of the principles and effects of the present invention, and are not intended to limit the present invention. Anyone familiar with the art may modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by a person of ordinary skill in the art without departing from the spirit and technical concept disclosed by the present invention shall still be covered by the claims of the present invention.
Claims
1. A method for managing a thread pool in an application, characterized in that: The method comprises: Scan the application to be analyzed to obtain all class files that the application depends on; Access and analyze each class file, identify the distributed thread pools that exist in each class method in each class file, and determine the SDK to which each class method with a distributed thread pool belongs: If the SDK is a self-owned SDK, a thread pool interface is set to direct the SDK to a pre-created centralized thread pool; If the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, a proxy is instrumented on the SDK to redirect the SDK to a pre-created centralized thread pool.
2. The method for managing a thread pool in an application according to claim 1, characterized in that: The accessing and analyzing each class file, identifying the thread pool where each class method in each class file exists, and determining the SDK to which each class method in the thread pool belongs, includes: Access and analyze each class file based on the ASM library, and extract all class methods included in each class file; For each class method: according to the identifier of the class method, determine whether there is a corresponding thread pool for the class method, and identify the class name to which the class method belongs if there is a thread pool; Based on each class name, determine the SDK to which each class method belongs.
3. The method for managing a thread pool in an application according to claim 2, characterized in that: Determining the SDK type to which each class method belongs based on each class name includes: For each class name: Compare the class name with the third-party category library corresponding to the preset third-party SDK to determine whether the two match: If they match, the SDK to which the class method corresponding to the class name belongs is a third-party SDK; If they do not match, the SDK to which the class method corresponding to the class name belongs is your own SDK.
4. The method for managing a thread pool in an application according to claim 1, characterized in that: If the SDK is a self-owned SDK, a thread pool interface is set to direct the SDK to a pre-created centralized thread pool, including: If the SDK is your own SDK, set the thread pool interface in the SDK and determine the location where the thread pool interface points: If the thread pool interface points to the centralized thread pool, directing the SDK to the centralized thread pool through the thread pool interface; If the thread pool interface points to a decentralized thread pool and the SDK is not in the preset whitelist, the decentralized thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface.
5. The method for managing a thread pool in an application according to claim 4, characterized in that: If the thread pool interface points to a decentralized thread pool and the SDK is not in a preset whitelist, the decentralized thread pool is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface, including: Determine whether the SDK is in the whitelist: If the SDK is not in the whitelist, the decentralized thread pool pointed to by the thread pool interface is used as a centralized thread pool, and the SDK is directed to the centralized thread pool through the thread pool interface; If the SDK is in the whitelist, an agent is inserted into the SDK to redirect the SDK to the centralized thread pool.
6. The method for managing a thread pool in an application according to claim 1, characterized in that: If the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, the SDK is instrumented with a proxy so that the SDK is redirected to a pre-created centralized thread pool, including: If the SDK is a third-party SDK, determine whether the SDK has a predefined thread pool interface: If yes, directing the SDK to the centralized thread pool through the thread pool interface; If not, an instrumentation agent is performed on the SDK to redirect the SDK to the centralized thread pool.
7. The method for managing a thread pool in an application according to claim 5 or 6, characterized in that: The inserting agent for the SDK to redirect the SDK to the centralized thread pool includes: based on static inserting, modifying the bytecode file of the SDK to replace the creation instruction of the corresponding decentralized thread pool with the acquisition instruction of the centralized thread pool.
8. A thread pool management system in an application, characterized in that: The system comprises: A class file acquisition module is used to scan the application to be analyzed and acquire all class files that the application depends on; SDK judgment module, used to access and analyze each class file, identify the distributed thread pools in each class method in each class file, and determine the SDK to which each class method with a distributed thread pool belongs: SDK orientation module, used to set the thread pool interface if the SDK is a self-owned SDK, so as to direct the SDK to a pre-created centralized thread pool; The SDK redirection module is used to perform an instrumentation proxy on the SDK if the SDK is a third-party SDK and the SDK does not have a predefined thread pool interface, so as to redirect the SDK to a pre-created centralized thread pool.
9. An electronic device, characterized in that: The electronic device comprises: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the electronic device to implement the thread pool management method in the application as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor of a computer, the computer is caused to execute the method for managing a thread pool in an application program as claimed in any one of claims 1 to 7.