A multi-threaded program implementation method, device and related equipment
By analyzing the abstract syntax tree of the objective function, obtaining the call dependency graph and splitting it into a thread pool, combining and optimizing the thread length, the problem of low efficiency of single-threaded development to multi-threaded programs is solved, and efficient multi-threaded program construction is achieved and optimize.
Patent Information
- Application Number
- CN202110001428.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-04
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2041-01-04
AI Technical Summary
Multithreaded programs obtained on the basis of single-thread development are less efficient, and the prior art is prone to destroy the intuitiveness and readability of the program.
By obtaining the call dependency graph based on the abstract syntax tree of the objective function, split the call dependency graph to obtain the thread pool of multiple threads, take out the threads with a length smaller than the set value from the thread pool and combine it, form a combined thread, and combine it in the combined thread Add it back to the thread pool when the length meets the requirements.
Dynamically constructing multi-threaded programs improves the efficiency of obtaining multi-threaded programs, optimizes thread length, enhances the multi-threading capabilities of programs, and maintains the intuitiveness and readability of programs.
Smart Images

Figure CN114721657B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a multi-threaded program implementation method, device and related equipment. Background Art
[0002] By increasing the CPU frequency as much as possible to improve the computing power of the computer system, the computer can have multiple CPU cores and hyperthreading capabilities. For program developers, single-threaded development is a more intuitive and less error-prone approach that can avoid thread pool management problems, data dependency problems, and thread safety problems caused by multithreading. However, programs developed with single threads cannot fully utilize the multithreading performance of the computer.
[0003] At present, the main method of obtaining a multi-threaded program based on single-threaded development is to read and rewrite the entire Java file. This method easily destroys the intuitiveness of the program and has poor readability, resulting in reduced modification efficiency of the rewritten Java file, thereby affecting the efficiency of obtaining the multi-threaded program. It can be seen that the efficiency of obtaining a multi-threaded program based on single-threaded development in the prior art is low. Summary of the invention
[0004] The embodiment of the present invention provides a multi-thread program implementation method, apparatus and related equipment to solve the problem of low efficiency of multi-thread programs obtained on the basis of single-thread development.
[0005] In a first aspect, an embodiment of the present invention provides a multi-threaded program implementation method, including:
[0006] According to the abstract syntax tree of the target function, a call dependency graph is obtained, wherein the call dependency graph includes dependency relationships between statements of the target function;
[0007] Splitting the call dependency graph to obtain a thread pool including multiple threads;
[0008] Taking out threads whose length is less than a set value from the thread pool and combining them to obtain a combined thread;
[0009] When the length of the combined thread is greater than or equal to the set value, the combined thread is added to the thread pool.
[0010] In a second aspect, an embodiment of the present invention provides a multi-threaded program implementation device, including:
[0011] An acquisition module, configured to acquire a call dependency graph according to an abstract syntax tree of a target function, wherein the call dependency graph includes dependency relationships between statements of the target function;
[0012] A splitting module, used for splitting the call dependency graph to obtain a thread pool including multiple threads;
[0013] A combining module, used for taking out threads whose length is less than a set value from the thread pool and combining them to obtain a combined thread;
[0014] An adding module is used to add the combined thread to the thread pool when the length of the combined thread is greater than or equal to the set value.
[0015] In a third aspect, an embodiment of the present invention further provides a communication device, comprising: a transceiver, a memory, a processor, and a program stored in the memory and executable on the processor; characterized in that the processor is used to read the program in the memory to implement the steps in the method described in the first aspect above.
[0016] In a fourth aspect, an embodiment of the present invention further provides a readable storage medium for storing a program, wherein the program, when executed by a processor, implements the steps in the method described in the first aspect.
[0017] In the embodiment of the present invention, a call dependency graph is obtained according to an abstract syntax tree of a target function; the call dependency graph is split to obtain a thread pool including multiple threads; threads whose lengths are less than a set value are combined from the multiple threads to obtain a combined thread; and when the length of the combined thread is not less than the set value, the combined thread is returned to the thread pool. By optimizing the length of threads by combining threads with shorter lengths, a multi-threaded program can be dynamically constructed when executing a program in a thread pool, thereby improving the efficiency of obtaining a multi-threaded program. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 This is one of the flowcharts of a multi-threaded program implementation method provided by an embodiment of the present invention;
[0019] Figure 2 This is the second flowchart of a multi-threaded program implementation method provided by an embodiment of the present invention;
[0020] Figure 3 This is a third flowchart of a multi-threaded program implementation method provided by an embodiment of the present invention;
[0021] Figure 4 It is one of the structural diagrams of a multi-threaded program implementation device provided by an embodiment of the present invention;
[0022] Figure 5 This is a second structural diagram of a multi-threaded program implementation device provided by an embodiment of the present invention;
[0023] Figure 6 This is a third structural diagram of a multi-threaded program implementation device provided by an embodiment of the present invention;
[0024] Figure 7 This is a fourth structural diagram of a multi-threaded program implementation device provided by an embodiment of the present invention;
[0025] Figure 8 It is a structural diagram of a communication device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0026] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0027] The terms "first", "second" etc. in the embodiments of the present invention are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. In addition, the terms "include" and "have" and any variation thereof are intended to cover non-exclusive inclusions, for example, the process, method, system, product or equipment comprising a series of steps or units need not be limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or equipment. In addition, "and / or" is used in the present application to represent at least one of the connected objects, such as A and / or B and / or C, which represents 7 situations including single A, single B, single C, and A and B all exist, B and C all exist, A and C all exist, and A, B and C all exist.
[0028] See also Figure 1 , Figure 1 is a flowchart of a multi-threaded program implementation method provided by an embodiment of the present invention, such as Figure 1 As shown, the following steps are included:
[0029] Step 101: Obtain a call dependency graph based on the abstract syntax tree of the target function, where the call dependency graph includes dependency relationships between statements of the target function.
[0030] Among them, the target function is a function marked by the developer to enable the automatic multi-threaded optimization function. During compilation, the annotations in the code can be read to obtain the target function; further, through the interface provided by the Java compiler, the abstract syntax tree inside the target function can be obtained; further, through the abstract syntax tree, a call dependency graph including the dependency relationships between statements of the target function can be obtained.
[0031] It should be noted that the objective function includes a first statement and a second statement. After the first statement is executed, the second statement needs to be executed immediately. Therefore, there is a dependency relationship between the first statement and the second statement of the objective function.
[0032] In addition, the generation of the call dependency graph can be based on a directed graph. In the call dependency graph, it mainly includes element points and edges. The element points are statements of the target function, and the edges are connecting lines between the statements of the target function.
[0033] Step 102: split the call dependency graph to obtain a thread pool including multiple threads.
[0034] It should be noted that before obtaining a thread pool including multiple threads, it is necessary to read the system settings to obtain setting information such as the maximum number of threads in the thread pool, the default timeout period, and the minimum number of rows for starting to split threads.
[0035] By splitting the call dependency graph, a thread pool including multiple threads can be obtained. Figure 2 As shown, in the call dependency graph, "1", "21", "22", "23", "31", "32" and "4" represent statements of the target function, and the dependency relationship between statements is represented by connecting lines. By splitting the call dependency graph, we can get the first thread "1-21-31-4", the second thread "1-22-31-4", the third thread "1-22-4", the fourth thread "1-22-32-4" and the fifth thread "1-23-32-4". The above threads can form a thread pool. It can be seen that the length of the third thread is smaller than that of other threads. At the same time, the number of threads in the thread pool cannot exceed the maximum number of threads in the thread pool.
[0036] In addition, for a call dependency graph with many element points, the threads can be split by the following steps:
[0037] First, analyze the call dependency graph and select the element point with out-degree greater than 1 and closest to the starting point in the graph, which is recorded as M element point. The out-degree of an element point greater than 1 indicates that the result of the statement corresponding to the element point may be required by multiple other statements, so multi-threading capabilities may be required;
[0038] Next, select the element point closest to the end point and with an in-degree greater than 1, recorded as the N element point. An element point with an in-degree greater than 1 indicates that the element point can receive results from multiple different threads;
[0039] Next, in the call dependency graph, obtain all paths between the M element point and the N element point;
[0040] Next, randomly select two paths from all the paths. If the two paths have an intersection point, the two paths are marked as the same group.
[0041] Finally, after the above processing is performed on all paths, if the path can be divided into multiple groups and the number of at least two groups of paths exceeds the set value of the thread length, then the corresponding number of threads is enabled through the thread pool, and the statements contained in the multiple groups of paths are marked with thread numbers in the order of dependencies; if the path combination cannot meet the above requirements, then the starting element point and / or the end element point selected in the call dependency graph are discarded, and the element point closest to the starting element point and / or the end element point and with an out-degree and / or in-degree greater than 1 is selected as the new starting element point and / or the end element point, and the above analysis and processing are performed again until the path including the new starting element point and / or the end element point meets the above requirements. It should be noted that if the starting point or the end point in a path is in a loop structure or a selection structure, which may pass through the starting element point but not necessarily the end element point when the program is executed, then the starting element point and the end element point of this path are skipped, and other paths are selected again.
[0042] Step 103: Take out threads whose length is less than the set value from the thread pool and combine them to obtain a combined thread.
[0043] It should be noted that when splitting threads, there may be a small number of very long threads and most of the threads are relatively short. In this case, too many threads need to be enabled when executing the program. Because most of the threads are relatively short, they may occupy the number of threads in the thread pool, causing other threads to have to wait. The time spent on starting the threads may be long, resulting in low program execution efficiency.
[0044] The thread length setting value can be determined according to the actual application scenario and is not limited here. In the thread pool, short threads whose length is less than the set value are taken out. At this time, the threads left in the thread pool are long threads whose length meets the requirements. By combining two or more short threads, a long thread whose length meets the requirements can be obtained, so that the number of short threads in the thread pool can be reduced.
[0045] Step 104: When the length of the combined thread is greater than or equal to the set value, the combined thread is added to the thread pool.
[0046] Among them, if the length of the combined thread is greater than or equal to the set value, it means that the length of the combined thread has met the requirements, and the combined thread obtained by combining the short threads taken out of the thread pool can be added to the thread pool. The length of the threads in the optimized thread pool meets the requirements of the set value. When executing the program in the optimized thread pool, a multi-threaded program can be dynamically constructed.
[0047] It should be noted that the statements of all thread numbers can be taken out and put into internal subclasses that inherit the Callable interface or the Runnable interface according to the thread numbers; when the program executes to the corresponding position, the above internal subclasses can be called, and the statements with thread numbers can be put into the thread pool for execution, so as to dynamically build multi-threaded programs and improve the multi-threaded capabilities of the program.
[0048] In the embodiment of the present invention, a call dependency graph is obtained according to an abstract syntax tree of a target function; the call dependency graph is split to obtain a thread pool including multiple threads; threads whose lengths are less than a set value are combined from the multiple threads to obtain a combined thread; and when the length of the combined thread is not less than the set value, the combined thread is returned to the thread pool. By optimizing the length of threads by combining threads with shorter lengths, a multi-threaded program can be dynamically constructed when executing a program in a thread pool, thereby improving the efficiency of obtaining a multi-threaded program.
[0049] As an optional implementation, a call dependency graph is obtained according to an abstract syntax tree of the target function, including:
[0050] According to the abstract syntax tree of the target function, obtain the statement of the target function;
[0051] Connect the statements with dependencies in the target function to obtain the call dependency graph.
[0052] Among them, in the abstract syntax tree of the target function, the variables used in a single statement and the objects finally assigned can be obtained. Each statement of the target function is abstracted as an element point on a directed graph. According to the general properties of the directed graph, the element point also records the variable name that the statement depends on and the variable name that is finally assigned. If the element point represents a method of an object, the object that calls this method is considered to be both the variable used in the statement and the variable that is finally assigned. In addition, the execution time of the statement can be roughly estimated based on the line number of the statement of the target function itself and the number of possible execution lines of the statement. For example, a basic formula is 1. If it corresponds to a function, the number of lines of the function is one line.
[0053] In addition, based on the obtained element points of the call dependency graph, edges are created for the call dependency graph. First, the first element point in the call dependency graph is called, and the first element point is any element point in the call dependency graph; further, the target element point corresponding to the previous statement that the first element point assigns a value to the variable that it depends on is searched, and the target statement corresponding to the target element point in the abstract syntax tree of the target function is closest to the first statement corresponding to the first element point; further, the target element point and the first element point are connected to create an edge from the target element point to the first element point. The edge in the call dependency graph can indicate that the first statement depends on the target statement. During the program execution process, it is necessary to ensure that the target statement is executed first, and then the first statement is executed. In this way, the call dependency graph of the target function is generated.
[0054] In this implementation, statements of the target function are obtained based on the abstract syntax tree of the target function, and then statements with dependencies in the target function are connected to obtain a call dependency graph of the target function. The statements of the target function serve as element points of the call dependency graph, and the dependencies between statements serve as edges of the call dependency graph. The call dependency graph has a simple structure, and threads can be obtained based on the call dependency graph to improve the efficiency of obtaining threads.
[0055] Optionally, threads whose length is less than a set value are taken out from the thread pool for combination to obtain a combined thread, including:
[0056] Take out multiple first threads whose length is less than a set value from the thread pool;
[0057] N first threads among the multiple first threads are combined to obtain a combined thread, and the number of execution statements of the N first threads is less than the number of execution statements of the remaining threads of the multiple first threads.
[0058] Among them, the length of the thread can indicate the number of statements executed in the thread. If the length of multiple threads in the thread pool is less than the set value, it may mean that some threads in the thread pool are too short, which may affect the efficiency of program execution. Therefore, it is necessary to combine the short threads in the thread pool to optimize the thread pool.
[0059] In a thread pool including multiple threads, multiple first threads whose lengths are less than a set value are taken out. Generally speaking, to meet the thread combination condition, the number of first threads is greater than or equal to 2; further, N first threads among the multiple first threads are combined to obtain a combined thread, where N is an integer greater than or equal to 2. When the length of the combined thread meets the length requirement, the combined thread is added to the thread pool. In addition, when the length of only one first thread does not meet the requirement and there is no other first thread combined with it, the first thread is added to the thread pool. Taking N equal to 2 as an example, multiple first threads can be arranged from low to high according to the number of execution statements of the threads, and then two threads with fewer execution statements than other threads are merged into one combined thread.
[0060] In this implementation, since multiple first threads whose lengths are less than a set value are taken out from the thread pool, and N of the multiple first threads are combined to obtain a combined thread, the length of the thread can be increased, the thread pool can be optimized, and the efficiency of obtaining a multi-threaded program can be improved.
[0061] Optionally, the multi-threaded program implementation method further includes:
[0062] When the length of the combined thread is less than the set value, the combined thread is updated to the plurality of first threads.
[0063] Among them, if the length of the combined thread is less than the set value, it means that the number of execution statements in the combined thread is too small and still does not meet the requirements. Therefore, the combined thread whose length does not meet the requirements can be updated to multiple first threads, and the combined thread continues to combine threads as the first thread. In the case that only one first thread does not meet the requirements, the first thread has no other threads to be combined with it, then the first thread can be added to the thread pool.
[0064] In this implementation, since the combined thread is updated to multiple first threads when the length of the combined thread is less than the set value, the combined thread can continue to combine threads as the first thread, which can ensure the proportion of threads in the thread pool that meet the length requirements, and can optimize the thread pool, thereby improving the efficiency of obtaining multi-threaded programs.
[0065] A specific implementation method, such as Figure 3 As shown, the steps to optimize the thread pool are as follows:
[0066] Step 1: Read the settings. Read the system settings to obtain the maximum number of threads in the thread pool, the default timeout, the minimum number of rows for starting to split threads, and other setting information.
[0067] Step 2: Read the Java compiler syntax tree. Through the interface provided by the Java compiler, the abstract syntax tree inside the target function marked by the developer as enabling the automatic multi-threaded optimization function can be obtained.
[0068] Step 3: Generate a call dependency graph. Through the abstract syntax tree of the target function, a call dependency graph including the dependency relationship between statements of the target function can be obtained. The generation of the call dependency graph can be based on a directed graph. In the call dependency graph, it mainly includes element points and edges. The element points are statements of the target function, and the edges are connecting lines between statements of the target function.
[0069] Step 4, split the call dependency graph. This step includes: Step 4.1, obtain the point with an in-degree greater than 1 closest to the starting point in the graph as the path starting point and obtain the point with an out-degree greater than 1 closest to the starting and ending points in the graph as the path ending point; Step 4.2, obtain all paths from the starting point to the ending point; Step 4.3, grouping, if there is an intersection, merge the two groups, that is, if the first path and the second path have the same element point, then merge the first path and the second path into one group; Step 4.4, determine whether the conditions that the remaining number of groups is greater than 1 and the group length is sufficient are met; Step 4.5, if the conditions that the remaining number of groups is greater than 1 and the group length is sufficient are not met, exclude this pair of starting points and ending points, and take another pair of path starting points and ending points with the second longest relative distance, that is, abandon the starting element point and / or ending element point selected at the beginning in the call dependency graph, and find an element point with an out-degree and / or in-degree greater than 1 that is farther away from the starting element point and / or ending element point as the new starting element point and / or ending element point; If the conditions that the remaining number of groups is greater than 1 and the group length is sufficient are met, execute step 5.
[0070] Step 5: Write the functions in these groups into the thread temporary class in the original line number order.
[0071] Step 6: Sort the threads from short to long, and then merge the threads in sequence until they meet the requirements. If the lengths of multiple threads in the thread pool do not meet the requirements, sort the multiple threads from short to long, and merge the threads in sequence to ensure that the lengths of all threads are greater than or equal to the set value, or only one thread has a length less than the set value.
[0072] See also Figure 4 , Figure 4 is one of the structural diagrams of a multi-threaded program implementation device provided by an embodiment of the present invention, such as Figure 4 As shown, the multi-threaded program implementation device 400 includes:
[0073] An acquisition module 401 is used to acquire a call dependency graph according to an abstract syntax tree of a target function, wherein the call dependency graph includes dependency relationships between statements of the target function;
[0074] A splitting module 402 is used to split the call dependency graph to obtain a thread pool including multiple threads;
[0075] A combining module 403 is used to take out threads whose length is less than a set value from the thread pool and combine them to obtain a combined thread;
[0076] The adding module 404 is used to add the combined thread to the thread pool when the length of the combined thread is greater than or equal to a set value.
[0077] Optional, such as Figure 5 As shown, the acquisition module 401 includes:
[0078] An acquisition submodule 4011 is used to acquire the statements of the target function according to the abstract syntax tree of the target function;
[0079] The connection submodule 4012 is used to connect the statements with dependency relationships in the target function to obtain a call dependency graph.
[0080] Optional, such as Figure 6 As shown, the combination module 403 includes:
[0081] The fetching submodule 4031 is used to fetch a plurality of first threads whose lengths are smaller than a set value from the thread pool;
[0082] The combining submodule 4032 is used to combine N first threads among the multiple first threads to obtain a combined thread, wherein the number of execution statements of the N first threads is less than the number of execution statements of the remaining threads of the multiple first threads.
[0083] Optional, such as Figure 7 As shown, the combination module 403 also includes:
[0084] The updating submodule 4033 is used to update the combined thread into the plurality of first threads when the length of the combined thread is less than a set value.
[0085] The multi-threaded program implementation device 400 can implement the embodiment of the present invention. Figure 1 The various processes of the method embodiment and the same beneficial effects are achieved, and will not be described again here to avoid repetition.
[0086] The embodiment of the present invention also provides a communication device. Figure 8 The communication device may include a processor 801, a memory 802, and a program 8021 stored in the memory 802 and executable on the processor 801. When the program 8021 is executed by the processor 801, Figure 1 Any steps in the corresponding method embodiments and achieving the same beneficial effects will not be repeated here.
[0087] A person skilled in the art can understand that all or part of the steps of implementing the above-mentioned embodiment method can be completed by hardware related to program instructions, and the program can be stored in a readable medium. The embodiment of the present invention also provides a readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the above-mentioned Figure 1 Any steps in the corresponding method embodiments can achieve the same technical effect and will not be described again here to avoid repetition.
[0088] The storage medium may be a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0089] The above is a preferred implementation of the embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for implementing a multi-threaded program, characterized in that: include: According to the abstract syntax tree of the target function, a call dependency graph is obtained, wherein the call dependency graph includes dependency relationships between statements of the target function; Splitting the call dependency graph to obtain a thread pool including a plurality of threads, wherein the thread is a path between two element points in the call dependency graph; Taking out threads whose length is less than a set value from the thread pool and combining them to obtain a combined thread; When the length of the combined thread is greater than or equal to the set value, the combined thread is added to the thread pool.
2. The multi-threaded program implementation method according to claim 1, characterized in that: The step of obtaining a call dependency graph based on the abstract syntax tree of the target function includes: Acquire a statement of the target function according to the abstract syntax tree of the target function; The statements with dependency relationships in the target function are connected to obtain the call dependency graph.
3. The multi-threaded program implementation method according to claim 1, characterized in that: The taking out threads whose length is less than a set value from the thread pool and combining them to obtain a combined thread comprises: Taking out a plurality of first threads whose lengths are smaller than the set value from the thread pool; N first threads among the multiple first threads are combined to obtain the combined thread, wherein the number of execution statements of the N first threads is less than the number of execution statements of the remaining threads among the multiple first threads.
4. The multi-threaded program implementation method according to claim 3, characterized in that: The method further comprises: When the length of the combined thread is less than the set value, the combined thread is updated to the plurality of first threads.
5. A multi-threaded program implementation device, characterized in that: include: An acquisition module, configured to acquire a call dependency graph according to an abstract syntax tree of a target function, wherein the call dependency graph includes dependency relationships between statements of the target function; A splitting module, used for splitting the call dependency graph to obtain a thread pool including a plurality of threads, wherein the thread is a path between two element points in the call dependency graph; A combining module, used for taking out threads whose length is less than a set value from the thread pool and combining them to obtain a combined thread; An adding module is used to add the combined thread to the thread pool when the length of the combined thread is greater than or equal to the set value.
6. The multi-threaded program implementation device according to claim 5, characterized in that: The acquisition module comprises: An acquisition submodule, used for acquiring a statement of the target function according to an abstract syntax tree of the target function; The connection submodule is used to connect the statements with dependency relationships in the target function to obtain the call dependency graph.
7. The multi-threaded program implementation device according to claim 5, characterized in that: The combined module comprises: A fetching submodule, used for fetching a plurality of first threads whose lengths are smaller than the set value from the thread pool; The combining submodule is used to combine N first threads among the multiple first threads to obtain the combined thread, wherein the number of execution statements of the N first threads is less than the number of execution statements of the remaining threads among the multiple first threads.
8. The multi-threaded program implementation device according to claim 7, characterized in that: Also includes: An updating submodule is used to update the combined thread to the multiple first threads when the length of the combined thread is less than the set value.
9. A communication device, comprising: A transceiver, a memory, a processor and a program stored in the memory and executable on the processor; wherein the processor is used to read the program in the memory to implement the steps in the multi-threaded program implementation method as described in any one of claims 1 to 4.
10. A readable storage medium for storing a program, characterized in that: When the program is executed by a processor, the steps in the multi-thread program implementation method according to any one of claims 1 to 4 are implemented.
Citation Information
Patent Citations
Communication device and method for updating software thereof
CN102541592A
Program generation method and apparatus
CN104991773A