Multi-thread cooperative execution method based on double linked list
By using the data structure and node state management method based on bidirectional linked lists in multi-threaded collaborative execution scenarios, the problem that traditional lock structure cannot meet the specified order and conditional execution of multi-threads is solved, and efficient and accurate thread concurrency control is achieved.
Patent Information
- Application Number
- CN202411893538.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-03-07
AI Technical Summary
The traditional lock structure has limitations in the scenario of multi-threading execution in the specified order and conditional execution, and cannot meet the requirements of multi-threading execution in the specified order and conditional execution.
A multi-threaded collaborative execution method based on a two-way linked list is adopted, and the execution order and conditions of threads are controlled by building a data structure to reflect the execution order and conditions in the business scenario.
Multi-threading execution in specified order and conditions is realized, which avoids confusion and conflict in multi-threading execution, and simplifies the complexity and coding difficulty of thread concurrent control scenarios.
Smart Images

Figure CN119576578A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a multi-threaded collaborative execution method based on a bidirectional linked list. Background Art
[0002] In concurrent programming, locks are a commonly used synchronization mechanism to ensure data consistency and integrity when multiple threads access shared resources.
[0003] When solving concurrency problems, traditional lock structures (such as mutex locks and read-write locks) need to unlock threads through program control and obtain lock resources through free competition. However, this mechanism has obvious limitations when it comes to scenarios where multiple threads are executed in a specified order and under specified conditions. Summary of the invention
[0004] In view of this, it is necessary to provide a multi-threaded collaborative execution method based on a bidirectional linked list to achieve the purpose of executing multiple threads in a specified order and under specified conditions.
[0005] In order to achieve the above object, the present invention provides a multi-threaded collaborative execution method based on a bidirectional linked list, comprising: Construct a data structure based on business scenario requirements; the data structure is a bidirectional linked list; When the node state corresponding to the first node in the data structure is successful and the execution condition corresponding to the second node is satisfied, the thread corresponding to the second node is executed, and the node state of the third node is switched to listening; the second node is a subordinate node of the first node; the third node is a subordinate node of the second node; When the node statuses of all nodes in the data structure are successful, the execution of threads in the data structure is stopped.
[0006] In a possible implementation, the step of constructing a data structure based on business scenario requirements includes: Constructing the data structure based on the execution conditions and execution order of each thread in the business scenario requirements; The data structure includes a plurality of nodes, each of which corresponds to a different thread, an upper node, a lower node and an execution condition.
[0007] In a possible implementation, the initial node in the data structure is a node whose execution condition is empty.
[0008] In a possible implementation, the method further includes: When the node status corresponding to the first node is failed, the node status of all subordinate nodes of the first node are switched to canceled.
[0009] In a possible implementation, the method further includes: When the node status corresponding to the first node is successful and the execution condition corresponding to the second node is not satisfied, the node status of the second node is switched to canceled, and the node status of all subordinate nodes of the second node are switched to canceled.
[0010] In a possible implementation, after building the data structure based on the business scenario requirements, the method further includes: Mark the node status of all nodes as initialized; When the threads corresponding to all nodes call the lock resource and enter the dormant state, the node status of all nodes is switched to ready.
[0011] The present invention also provides a multi-threaded collaborative execution device based on a bidirectional linked list, comprising: A construction module is used to construct a data structure based on business scenario requirements; the data structure is a bidirectional linked list; An execution module, configured to execute a thread corresponding to a first node in the data structure and switch a node state of a third node to listening when a node state corresponding to the first node in the data structure is successful and an execution condition corresponding to the second node is satisfied; the second node is a subordinate node of the first node; the third node is a subordinate node of the second node; The stopping module is used to stop executing the threads in the data structure when the node status of all nodes in the data structure is success.
[0012] On the other hand, the present invention also provides a computer program product, including a computer program, which, when executed by a processor, implements the multi-threaded collaborative execution method based on a bidirectional linked list described in any of the above implementations.
[0013] The beneficial effects of the present invention are as follows: the multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention can construct a bidirectional linked list data structure according to business scenario requirements. When the node status corresponding to the first node in the data structure is successful and the execution condition corresponding to the second node is met, the thread corresponding to the second node is executed, and the node status of the third node is switched to listening. When the node status of all nodes in the data structure is successful, the execution of the threads in the data structure is stopped. By combining the bidirectional linked list and the node status, it is possible to ensure that tasks are executed in a specific order and conditions, avoiding confusion and conflicts in multi-threaded execution. By providing a lock structure with preset unlocking conditions, precise thread concurrency control can be achieved. After being triggered, all threads will be automatically triggered according to the specified conditions and order, simplifying the complexity and coding difficulty of the thread concurrency control scenario. It can be used in scenarios where multi-threaded collaborative work is required, and multi-threaded serial or parallel execution in a specified order can be achieved by setting unlocking conditions. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0015] Figure 1 One of the method flow charts of an embodiment of the multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention; Figure 2 A second method flow chart of an embodiment of a multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention; Figure 3 A schematic diagram of the structure of an embodiment of a multi-threaded collaborative execution device based on a bidirectional linked list provided by the present invention. DETAILED DESCRIPTION
[0016] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only 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 those skilled in the art without creative work are within the scope of protection of the present invention.
[0017] In the description of the embodiments of the present invention, unless otherwise specified, "multiple" means two or more than two. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" may mean: A exists alone, A and B exist at the same time, and B exists alone.
[0018] The descriptions of "first", "second", etc. involved in the embodiments of the present invention are only for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the number of the indicated technical features. Therefore, the technical features defined as "first" or "second" may explicitly or implicitly include at least one of the features.
[0019] Figure 1 One of the method flow charts of an embodiment of the multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention is as follows Figure 1 As shown, the multi-threaded collaborative execution method based on a bidirectional linked list includes: S101. Construct a data structure based on business scenario requirements; the data structure is a bidirectional linked list; S102: When the node state corresponding to the first node in the data structure is success and the execution condition corresponding to the second node is satisfied, the thread corresponding to the second node is executed, and the node state of the third node is switched to listening; the second node is a subordinate node of the first node; the third node is a subordinate node of the second node; S103: When the node status of all nodes in the data structure is success, stop executing the threads in the data structure.
[0020] In S101, a bidirectional linked list data structure may be constructed according to actual business scenario requirements. The bidirectional linked list is composed of multiple nodes, each of which corresponds to a different thread, upper node, lower node, and execution condition.
[0021] Determine the order and relationship of each node in the linked list according to the business logic to ensure that it can correctly reflect the execution order in the business scenario.
[0022] In S102, the execution of a thread may be triggered according to the state of a node and the execution condition of a lower-level node in the linked list structure.
[0023] The first node is any node in the data structure. By judging the node state of the first node, when the node state corresponding to the first node is successful, it indicates that the thread corresponding to the first node is executed and the execution result is successful.
[0024] Then further determine whether the execution condition of the subordinate node of the first node (i.e., the second node) is met. When the execution condition of the second node is met, start the thread corresponding to the second node. At the same time, switch the state of the subordinate node of the second node (the third node) to listening, which indicates that the third node may be triggered when the next condition is met.
[0025] In S103, similarly, when the thread corresponding to the second node is executed completely and the execution result is successful, the node state of the second node is switched to success.
[0026] It is further determined whether the execution condition of the third node is satisfied. If satisfied, the thread corresponding to the third node is executed, and the state of the subordinate node of the third node is switched to listening.
[0027] When the node status of all nodes in the data structure is success, it indicates that all threads have been successfully completed, and the threads in the data structure are stopped, that is, the entire execution process ends.
[0028] Through the above steps, all threads can be executed in the specified order and conditions.
[0029] Compared with the prior art, the multi-threaded collaborative execution method based on a bidirectional linked list provided in an embodiment of the present invention can construct a bidirectional linked list data structure according to business scenario requirements. When the node status corresponding to the first node in the data structure is successful and the execution condition corresponding to the second node is met, the thread corresponding to the second node is executed, and the node status of the third node is switched to listening. When the node status of all nodes in the data structure is successful, the execution of threads in the data structure is stopped. By combining the bidirectional linked list and the node status, it is possible to ensure that tasks are executed in a specific order and conditions, avoiding confusion and conflicts in multi-threaded execution. By providing a lock structure with preset unlocking conditions, precise thread concurrency control can be achieved. After triggering, all threads will be automatically triggered according to the specified conditions and order, simplifying the complexity and coding difficulty of thread concurrency control scenarios. It can be used in scenarios where multi-threaded collaboration is required, and multi-threaded serial or parallel execution in a specified order can be achieved by setting unlocking conditions.
[0030] In some embodiments of the present invention, the step of constructing a data structure based on business scenario requirements includes: Constructing the data structure based on the execution conditions and execution order of each thread in the business scenario requirements; The data structure includes a plurality of nodes, each of which corresponds to a different thread, an upper node, a lower node and an execution condition.
[0031] In an embodiment of the present invention, the data structure storing all thread information, states and conditions is a bidirectional linked list, each node of the linked list may have multiple upper nodes and multiple lower nodes, and a scenario is used to describe the structure.
[0032] Exemplarily, when the business scenario requirement is: after threads a and b are successfully executed, thread c is executed.
[0033] The constructed data structure is as follows: [Node 1: {thread: a, parent node: [], child node: [node 3], execution condition: []}, Node 2: {thread: b, parent node: [], child node: [node 3], execution condition: []}, Node 3: {Thread: c, parent node: [node 1, node 2], child node: [], execution condition: [thread a executes successfully, thread b executes successfully]}].
[0034] In this data structure, node 1 and node 2 have no parent node, the execution condition is empty, and the subordinate nodes of node 1 and node 2 are both node 3. Node 3 has two parent nodes, corresponding to two execution conditions, which can be execution success, execution failure, and execution completion.
[0035] The states marked by the nodes can include initialization, preparation, not started, listening, execution, success, failure, cancellation, etc.
[0036] The multi-threaded collaborative execution method based on a bidirectional linked list provided in an embodiment of the present invention can dynamically construct and execute tasks according to actual business needs, ensuring the efficient operation of the entire system. The data structure constructed through business scenario requirements is a lock structure with preset unlocking conditions, which can achieve more accurate thread concurrency control. After triggering, all threads will be automatically triggered according to the specified conditions and sequence, simplifying the complexity and coding difficulty of thread concurrency control scenarios.
[0037] In some embodiments of the present invention, the initial node in the data structure is a node whose execution condition is empty.
[0038] The initial node in the data structure is a node with an empty execution condition. An empty execution condition indicates that the node has no execution condition. The corresponding thread can be executed under any condition. Starting from the initial node, the threads in the entire linked list can be executed in sequence.
[0039] When the thread corresponding to the initial node is executed and the execution result is successful, the node state of the initial node is switched to success, and further determine whether the execution conditions of the subordinate nodes of the initial node are met. If they are met, the thread corresponding to the subordinate node of the initial node is started. At the same time, the state of the subordinate node of the subordinate node is switched to listening.
[0040] It is understandable that the number of initial nodes may be multiple, such as node 1 and node 2 in the above data structure are both initial nodes, and threads corresponding to multiple initial nodes may run in parallel.
[0041] In addition, the number of upper-level nodes and lower-level nodes corresponding to each node may also be multiple.
[0042] In some embodiments of the present invention, after building the data structure based on the business scenario requirements, the method further includes: Mark the node status of all nodes as initialized; When the threads corresponding to all nodes call the lock resource and enter the dormant state, the node status of all nodes is switched to ready.
[0043] After building the data structure based on business scenario requirements, the status of all nodes needs to be marked as initialized.
[0044] Then, when the threads corresponding to all nodes call the lock resource to enter the dormant state, the status of all nodes is switched to ready to facilitate subsequent execution control.
[0045] In some embodiments of the present invention, it further comprises: When the node status corresponding to the first node is failed, the node status of all subordinate nodes of the first node are switched to canceled.
[0046] Understandably, when the node status corresponding to the first node is failed, indicating that the thread corresponding to the first node fails to execute, the node status of all subordinate nodes of the first node are switched to cancel to prevent the threads corresponding to these nodes from being executed incorrectly.
[0047] In some embodiments of the present invention, it further comprises: When the node status corresponding to the first node is successful and the execution condition corresponding to the second node is not satisfied, the node status of the second node is switched to canceled, and the node status of all subordinate nodes of the second node are switched to canceled.
[0048] It can be understood that when the status of the first node is successful, but the execution condition of the second node is not met, the status of the second node is switched to cancel, interrupting the thread task execution, and the status of all subordinate nodes of the second node is also switched to cancel to ensure that only the threads corresponding to the nodes that meet the execution conditions will be executed.
[0049] The multi-threaded collaborative execution method based on a bidirectional linked list provided in an embodiment of the present invention can stop the execution of related threads in time when an error occurs through a clear interrupt processing logic, thereby avoiding waste of resources and potential error accumulation.
[0050] Figure 2 The second method flow chart of an embodiment of the multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention is as follows: Figure 2 As shown, the multi-threaded collaborative execution method based on a bidirectional linked list includes: S201. Build a data structure according to business scenario requirements, and mark the status of all nodes as 'initialization'.
[0051] S202, all threads call lock resources and enter sleep mode, at which time all node states switch to 'ready'.
[0052] S203, triggering the start, checking that all node states are 'ready', switching all node states to 'start', and waking up all threads whose execution conditions are empty.
[0053] S204, the running thread will first check whether the execution condition is met, and will go to different branches here: (a) If the current node status is 'cancel', the thread task execution is interrupted and the status of the subordinate node is switched to 'cancel'; (b) The thread of the upper node has not been completed, so the node status is switched to "listening" and enters sleep mode; (c) If the execution condition is not met, the node state is switched to 'cancel' and 'branch (a)' is executed; (d) The execution condition is met and the thread starts executing the task.
[0054] S205, after the thread task is executed, the node status will be switched to "success" or "failure" according to the execution result. If the status is "failure", the status of all subordinate nodes will be switched to "cancel". After the operation is completed, all threads of the subordinate nodes will be awakened.
[0055] S206: When the execution node is the last node, the entire lock function is executed and the lock resources are released.
[0056] Through the above process, all threads can be executed in the specified order and conditions.
[0057] The multi-threaded collaborative execution method based on a bidirectional linked list provided in an embodiment of the present invention provides a lock structure with pre-set unlocking conditions, which can achieve more accurate thread concurrency control. After triggering, all threads will be automatically triggered according to the specified conditions and sequence, simplifying the complexity and coding difficulty of thread concurrency control scenarios.
[0058] The present invention can be used in scenarios where multi-threaded collaboration is required, and can achieve serial or parallel execution of multiple threads in a specified order by setting unlocking conditions. It can also be applied to concurrent control scenarios such as workflows to solve the problems of complex thread execution order control and difficulty in perceiving thread execution status.
[0059] In order to better implement the multi-threaded collaborative execution method based on a bidirectional linked list in the embodiment of the present invention, on the basis of the multi-threaded collaborative execution method based on a bidirectional linked list, the embodiment of the present invention also provides a multi-threaded collaborative execution device based on a bidirectional linked list. Figure 3A schematic diagram of the structure of an embodiment of a multi-threaded collaborative execution device based on a bidirectional linked list provided by the present invention is shown in FIG. Figure 3 As shown, the multi-threaded collaborative execution device 300 based on a bidirectional linked list includes: A construction module 310 is used to construct a data structure based on business scenario requirements; the data structure is a bidirectional linked list; An execution module 320 is used to execute the thread corresponding to the second node and switch the node state of the third node to listening when the node state corresponding to the first node in the data structure is successful and the execution condition corresponding to the second node is satisfied; the second node is a subordinate node of the first node; the third node is a subordinate node of the second node; The stopping module 330 is used to stop executing the threads in the data structure when the node status of all nodes in the data structure is success.
[0060] The multi-threaded collaborative execution device 300 based on a bidirectional linked list provided in the above embodiment can implement the technical solution described in the above embodiment of the multi-threaded collaborative execution method based on a bidirectional linked list. The specific implementation principles of the above modules or units can refer to the corresponding contents in the embodiment of the multi-threaded collaborative execution method based on a bidirectional linked list, which will not be repeated here.
[0061] Optionally, the construction module 310 is specifically used to: Constructing the data structure based on the execution conditions and execution order of each thread in the business scenario requirements; The data structure includes a plurality of nodes, each of which corresponds to a different thread, an upper node, a lower node and an execution condition.
[0062] Optionally, the initial node in the data structure is a node whose execution condition is empty.
[0063] Optionally, the multi-threaded collaborative execution device 300 based on a bidirectional linked list further includes: The first switching module is used to switch the node status of all subordinate nodes of the first node to cancel when the node status corresponding to the first node is failed.
[0064] Optionally, the multi-threaded collaborative execution device 300 based on a bidirectional linked list further includes: The second switching module is used to switch the node status of the second node to cancel and switch the node status of all subordinate nodes of the second node to cancel when the node status corresponding to the first node is successful and the execution condition corresponding to the second node is not satisfied.
[0065] Optionally, the multi-threaded collaborative execution device 300 based on a bidirectional linked list further includes: A marking module is used to mark the node status of all nodes as initialized; When the threads corresponding to all nodes call the lock resource and enter the dormant state, the node status of all nodes is switched to ready.
[0066] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the steps or functions of the multi-threaded collaborative execution method based on a bidirectional linked list provided in the above-mentioned method embodiments.
[0067] Those skilled in the art will appreciate that all or part of the processes of the above-mentioned embodiments can be implemented by instructing related hardware (such as a processor, a controller, etc.) through a computer program, and the computer program can be stored in a computer-readable storage medium, wherein the computer-readable storage medium is a disk, an optical disk, a read-only storage memory, or a random access memory, etc.
[0068] The above is a detailed introduction to the multi-threaded collaborative execution method based on a bidirectional linked list provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A multi-threaded collaborative execution method based on a bidirectional linked list, characterized in that: include: Build data structures based on business scenario requirements; The data structure is a doubly linked list; When the node state corresponding to the first node in the data structure is successful and the execution condition corresponding to the second node is satisfied, the thread corresponding to the second node is executed, and the node state of the third node is switched to listening; the second node is a subordinate node of the first node; The third node is a subordinate node of the second node; When the node statuses of all nodes in the data structure are successful, the execution of threads in the data structure is stopped.
2. The multi-threaded collaborative execution method based on a bidirectional linked list according to claim 1, characterized in that: The data structure is constructed based on business scenario requirements, including: Constructing the data structure based on the execution conditions and execution order of each thread in the business scenario requirements; The data structure includes a plurality of nodes, each of which corresponds to a different thread, an upper node, a lower node and an execution condition.
3. The multi-threaded collaborative execution method based on a bidirectional linked list according to claim 2 is characterized in that: The initial node in the data structure is a node whose execution condition is empty.
4. The multi-threaded collaborative execution method based on a bidirectional linked list according to claim 1, characterized in that: Also includes: When the node status corresponding to the first node is failed, the node status of all subordinate nodes of the first node are switched to canceled.
5. The multi-threaded collaborative execution method based on a bidirectional linked list according to claim 1, characterized in that: Also includes: When the node status corresponding to the first node is successful and the execution condition corresponding to the second node is not satisfied, the node status of the second node is switched to canceled, and the node status of all subordinate nodes of the second node are switched to canceled.
6. The multi-threaded collaborative execution method based on a bidirectional linked list according to claim 1, characterized in that: After the data structure is constructed based on the business scenario requirements, it also includes: Mark the node status of all nodes as initialized; When the threads corresponding to all nodes call the lock resource and enter the dormant state, the node status of all nodes is switched to ready.
7. A multi-threaded collaborative execution device based on a bidirectional linked list, characterized in that: include: Building modules, used to build data structures based on business scenario requirements; The data structure is a doubly linked list; an execution module, configured to execute a thread corresponding to a first node in the data structure and switch a node state of a third node to listening when a node state corresponding to the first node is successful and an execution condition corresponding to the second node is satisfied; the second node is a subordinate node of the first node; The third node is a subordinate node of the second node; The stopping module is used to stop executing the threads in the data structure when the node status of all nodes in the data structure is success.
8. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the multi-threaded collaborative execution method based on a bidirectional linked list as described in any one of claims 1 to 6 is implemented.