Thread execution method and device for electronic process
Patent Information
- Application Number
- CN202611169528.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-04
- Publication Date
- 2026-10-27
AI Technical Summary
对此,现有引擎缺乏高效的内建协调机制,往往需要借助外部消息队列或分布式锁实现,显著增加了系统复杂度与延迟
[0055]本申请提供的上述电子流程的线程执行方法,通过线程计数、第一数据队列、第二数据队列及同步节点的判断时机,在同一流程实例中内支持同步节点与异步节点的混合调度。异步节点通过多线程并行执行以最大化吞吐,同步节点通过无锁数据队列排队及最后存活线程单线程串行执行以保证数据一致性,无需依赖外部消息队列或分布式锁,实现同步节点和异步节点混合并发的高效流程执行。同时,上述方法还可实现动态线程扩展,显著提升了批量数据处理的吞吐量。
Smart Images

Figure CN122985246A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of digital workflow orchestration technology, and in particular to a method and apparatus for thread execution of electronic workflows. Background Technology
[0002] As enterprises deepen their digital transformation, Business Process Management (BPM) technology, through process modeling, automated execution, and continuous optimization, connects fragmented business processes into controllable, visible, and measurable end-to-end processes. It is increasingly being applied to complex business scenarios such as financial approvals, government inter-agency services, supply chain collaboration, and intelligent manufacturing scheduling. Among these, the electronic workflow orchestration engine is the technological heart of the BPM system, serving as the core software component that drives business processes to flow automatically according to predefined rules. Its functions include process definition, managing the lifecycle of process instances, and coordinating the allocation and execution of tasks among participants and system services.
[0003] However, existing workflow engines mostly employ a unified synchronous execution model or a unified asynchronous message-driven model, making it difficult to flexibly mix the two modes within the same process instance. In scenarios involving data consistency requirements, such as inventory deduction and fund settlement, nodes need to execute synchronously and serially. Conversely, in scenarios involving I / O-intensive operations, such as external interface calls, nodes need to execute asynchronously and in parallel. Existing engines lack efficient built-in coordination mechanisms for this, often relying on external message queues or distributed locks, significantly increasing system complexity and latency.
[0004] Therefore, how to provide a new method for thread execution of electronic processes, realize the mixed execution of nodes synchronously and asynchronously, and enhance thread concurrency capabilities has become an urgent problem to be solved in this field. Summary of the Invention
[0005] Firstly, addressing the problems existing in the prior art, this invention provides a thread execution method for electronic workflows to achieve hybrid synchronous and asynchronous execution of nodes, thereby enhancing thread concurrency capabilities. In this method, an electronic workflow includes at least one piece of data to be processed, multiple pieces of data to be processed are processed by one or more threads, and each thread includes at least one node to process the data to be processed. The method includes:
[0006] S10, Obtain the thread start instruction;
[0007] S11, Increment the thread count by one;
[0008] S12, determine whether there is any pending data for the current thread in the first data queue. If it exists, execute S13; otherwise, execute S17.
[0009] S13, find the node to be executed from the node context mapping table according to the execution start point identifier of the data to be processed;
[0010] S14, determine whether the current node to be executed is a synchronization node. If it is, execute S15; otherwise, execute S16.
[0011] S15, reset the execution start point identifier of the current data to be processed to the node code of the current node to be processed, put the current data to be processed into the second data queue, and return to S12;
[0012] S16, execute the current node to be executed, and determine whether the next node of the currently executed node is the end node. If it is, return to S12; otherwise, return to S14.
[0013] S17, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty. If yes, execute S18; otherwise, execute S19.
[0014] S18: Obtain the node to be executed from the second data queue and execute it. Then, put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue, and return to S11.
[0015] S19, terminate the current thread.
[0016] According to the thread execution method of an electronic process provided by the present invention, after S12, the method further includes:
[0017] S121, determine whether the data to be processed obtained from the first data queue is empty. If it is not empty, execute S13; if it is empty, execute S12.
[0018] According to the thread execution method of an electronic process provided by the present invention, step S16 includes:
[0019] S161, execute the current node to be executed, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node;
[0020] S162, determine if the current node has executed successfully. If successful, execute S163; otherwise, execute S164.
[0021] S163, after the first callback is processed, execute S165;
[0022] S164, after the second callback is processed, execute S12;
[0023] S165 determines whether the next node of the currently executed node is the end node. If it is, return to S12; otherwise, return to S14.
[0024] According to the thread execution method of an electronic process provided by the present invention, after S163, the method further includes:
[0025] S1631, Determine whether the current node is a judgment type node. If it is, execute S1632; otherwise, execute S1633.
[0026] S1632, determine the next node based on the execution result of the current node and execute it;
[0027] S1633: Directly obtain the next node and execute it.
[0028] According to the thread execution method of an electronic process provided by the present invention, step S18 includes:
[0029] S181, retrieve the node to be executed from the second data queue and execute it, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node;
[0030] S182, determine if the current node has executed successfully. If successful, execute S183; otherwise, execute S184.
[0031] S183, after the first callback processing, execute S185;
[0032] S184, after the second callback process, execute S185;
[0033] S185, after putting the data corresponding to the currently executing node back into the first data queue to become the data to be executed in the first data queue, return to S11.
[0034] According to the thread execution method of an electronic process provided by the present invention, step S181 includes:
[0035] S1811, Obtain the node to be executed from the second data queue;
[0036] S1812, Perform atomic comparison and swap operations on the data to be processed corresponding to the node to be executed;
[0037] S1813, in response to the successful atomic comparison exchange operation, the node to be executed is executed.
[0038] According to the thread execution method of an electronic process provided by the present invention, step S1812 includes:
[0039] The synchronization enable flag of the data to be processed is compared with a preset value. If the synchronization enable flag is equal to the preset value, the atomic comparison and exchange operation is successful.
[0040] According to the thread execution method of an electronic process provided by the present invention, both the first data queue and the second data queue are lock-free data queues.
[0041] According to the thread execution method of the electronic process provided by the present invention, in S11 and S17, the threads are counted using an atomic counter.
[0042] Secondly, the present invention also provides a thread execution device for an electronic workflow, wherein the electronic workflow includes at least one piece of data to be processed, and the plurality of pieces of data to be processed are processed by one or more threads, wherein one thread includes at least one node for processing the pieces of data to be processed, and the device includes:
[0043] The thread startup unit is configured to receive the thread start instruction;
[0044] The counting unit is configured to increment the thread count by one, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty;
[0045] The data confirmation unit is configured to determine whether there is any pending data for the current thread in the first data queue;
[0046] The node lookup unit is configured to, if there is pending data for the current thread in the first data queue, look up the node to be executed from the node context mapping table based on the execution start point identifier of the pending data;
[0047] If there is no pending data for the current thread in the first data queue, the counting unit is invoked to decrement the thread count by one, and it is determined whether the current thread count is equal to 0 and whether the second data queue is not empty.
[0048] The first node processing unit is configured to determine whether the currently executed node is a synchronous node;
[0049] If the previous node to be executed is a synchronous node, then the execution start point identifier of the current data to be processed is reset to the node code of the current node to be executed, and the current data to be processed is put into the second data queue. Then, the data confirmation unit is called to determine whether there is data to be processed for the current thread in the first data queue.
[0050] If the previous node to be executed is not a synchronous node, then the current node to be executed is executed, and it is determined whether the next node of the currently executed node is the end node;
[0051] If the next node after the currently executed node is the end node, then the data confirmation unit is invoked to determine whether there is any pending data for the current thread in the first data queue;
[0052] If the next node of the currently executed node is not the end node, then determine whether the currently to be executed node is a synchronization node;
[0053] The second node processing unit is configured to, if the current thread count is equal to 0 and the second data queue is not empty, retrieve the node to be executed from the second data queue and execute it, and put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue, and then call the counting unit to increment the thread count by one.
[0054] The thread termination unit is configured to terminate the current thread if the current thread count is not equal to 0 or the second data queue is empty.
[0055] The thread execution method for the electronic workflow provided in this application supports hybrid scheduling of synchronous and asynchronous nodes within the same workflow instance through thread counting, a first data queue, a second data queue, and the timing of synchronous node judgment. Asynchronous nodes maximize throughput through multi-threaded parallel execution, while synchronous nodes ensure data consistency through lock-free data queue queuing and single-threaded serial execution by the last surviving thread. This eliminates the need for external message queues or distributed locks, achieving efficient workflow execution with hybrid concurrent synchronous and asynchronous nodes. Furthermore, the method also enables dynamic thread expansion, significantly improving the throughput of batch data processing. Attached Figure Description
[0056] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0057] Figure 1 This is a flowchart illustrating the thread execution method of the electronic process provided by the present invention;
[0058] Figure 2 This is another schematic diagram of the thread execution method of the electronic process provided by the present invention;
[0059] Figure 3 This is a flowchart illustrating the synchronization node execution method provided by the present invention;
[0060] Figure 4 This is a schematic diagram of the structure of the thread execution device for electronic processes provided by the present invention.
[0061] Figure label:
[0062] 410: Thread start unit; 420: Counting unit; 430: Data confirmation unit; 440: Node lookup unit; 450: First node processing unit; 460: Second node processing unit; 470: Thread termination unit. Detailed Implementation
[0063] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0064] To better understand this application, the terms and related background information used in this application will be explained first.
[0065] Electronic processes are business processes defined, driven, and managed in a digital form, and implemented using a process engine as the technological carrier.
[0066] A node is a logical processing unit or execution step in an electronic workflow. Various data processing functions can be implemented within a node, such as cache retrieval, HTTP calls, and database data retrieval.
[0067] When a synchronization node is executed, all data awaiting execution must wait on the synchronization node for the current node to complete its execution.
[0068] An asynchronous node executes concurrently with the data processed on that node, without waiting for other nodes to complete their execution.
[0069] A thread is the smallest unit of computation that an operating system can schedule; it is the actual unit of operation in an electronic workflow. An electronic workflow can contain multiple threads that share process resources but execute independently. Threads are also the execution units of nodes. Nodes themselves do not own threads; instead, the workflow engine schedules threads to execute nodes. Synchronous nodes run continuously within the same thread, asynchronous nodes release their threads to await callbacks, and parallel nodes may be assigned to different threads for simultaneous execution.
[0070] A thread instance is an object instance that runs during the process engine's runtime. One thread instance corresponds to one thread.
[0071] The data to be processed is internal data of the process engine, used to encapsulate business data. Business data is data that needs to be processed, such as data to be queried or accessed. After being encapsulated by the engine adapter, business data forms the data to be processed.
[0072] The following is combined with Figures 1-4 This application describes the thread execution method and apparatus for electronic processes.
[0073] This application provides a thread execution method for an electronic workflow, targeting the execution of a specific thread within the workflow. In this method, an electronic workflow includes at least one set of data to be processed, and these data are processed by one or more threads. For example, an electronic workflow may require processing a batch of 20 data items. These 20 data items can be configured to be processed sequentially by a single thread, or concurrently by multiple threads, and each thread includes at least one node for processing the data to be processed. Figures 1-3 As shown, the method includes S10-S19:
[0074] S10, Obtain the thread start instruction;
[0075] Specifically, once the electronic process is started, each thread in the electronic process begins to start according to the thread start instruction.
[0076] S11, Increment the thread count by one;
[0077] Specifically, for the current thread, the thread count is incremented by one after it starts. The same applies to other threads in the electronic process.
[0078] In a preferred embodiment, a thread counter can be used to count all currently executing threads. This thread counter is an atomic counter. An atomic counter is a variable that implements thread-safe counting based on hardware atomic instructions, ensuring read-write consistency in a multi-threaded environment without the need for locking.
[0079] S12, determine whether there is any pending data for the current thread in the first data queue.
[0080] Specifically, in this application, the first data queue contains the data to be processed in the current electronic workflow, and the thread data mapping table contains the thread identifier corresponding to the data to be processed. Based on the thread identifier, the data to be processed corresponding to the current thread can be found in the first data queue. If the first data queue contains data to be processed for the current thread, then the data to be processed needs to be processed in the current thread, and step S13 is executed; if the first data queue does not contain data to be processed for the current thread, then it means there is no data to be processed in the current thread, and step S17 is executed.
[0081] In a preferred embodiment, the first data queue adopts a lock-free data structure. This structure does not rely on traditional locking mechanisms (such as mutexes and read-write locks) to achieve thread safety. Instead, it ensures correctness in a multi-threaded environment through atomic operations and fine-grained concurrency control. An atomic operation is the smallest uninterruptible unit of execution; it either completes entirely or does not execute at all, with no intermediate state of "halfway through execution." By queuing data in a lock-free queue and having the last surviving thread execute it serially in a single thread, data consistency can be guaranteed without relying on external message queues or distributed locks.
[0082] In one specific implementation, the structure of the first data queue is constructed using thread-safe collection classes from the Java concurrency package java.util.concurrent, such as ConcurrentLinkedQueue and ConcurrentHashMap.
[0083] In another preferred embodiment, in response to the presence of pending data for the current thread in the first data queue, S121 needs to be executed before S13:
[0084] S121, determine whether the data to be processed obtained in the first data queue is empty;
[0085] Specifically, since multiple threads may run concurrently in an electronic workflow, each thread may retrieve data from the first data queue. Therefore, even if there is data to be processed in the first data queue, the specific value of that data may be empty, indicating that the data has already been retrieved by another concurrent thread. In this case, the current thread does not need to process the data again. Therefore, before processing the data, it is necessary to determine whether the retrieved data is empty. If the data is empty, it means that the data has been retrieved by another concurrent thread, so S12 is executed to continue searching for the next data to be processed in the first data queue. Here, data that has been searched in a thread can be marked as searched to prevent circular searching for the same data. If the data is not empty, S13 is executed.
[0086] S13, find the node to be executed from the node context mapping table according to the execution start point identifier of the data to be processed;
[0087] Specifically, the data to be processed includes not only the thread identifier but also the execution start point identifier. This execution start point identifier can be understood as the location identifier of the node that should be executed. In a complete, unexecuted process, the execution start point identifier should correspond to the first specific node that needs to be executed. This can be a regular node or a decision-making node, but it should be an executable node that is neither the beginning nor the end. The node context mapping table stores the context correspondence between each node. After obtaining the data to be processed, the next node to be executed corresponding to the node to be executed can be found from the node context mapping table. Each node to be executed and the corresponding data to be processed are stored in the first data queue.
[0088] In this application, each node corresponds to a node code, a synchronization-type node identifier, and a decision-type node identifier. The node code serves as a unique identifier for the node. The synchronization-type node identifier marks whether the node is a synchronous or asynchronous node. The decision-type node identifier indicates whether the node is a node with a branching decision type.
[0089] S14, determine whether the node to be executed is a synchronization node;
[0090] Specifically, after obtaining the node to be executed, it is determined whether the node is a synchronous node. If the node to be executed is a synchronous node, it means that the current node needs to obtain the processing results of other nodes before it can be executed, so S15 is executed; if the node to be executed is not a synchronous node, S16 is executed.
[0091] S15, reset the execution start point identifier of the current data to be processed to the node code of the current node to be processed, and put the current data to be processed into the second data queue;
[0092] Specifically, in response to the node to be executed being a synchronization node, the execution start point identifier of the current data to be processed is reset to the node code of the current node to be executed. Then, the current data to be processed is placed into the second data queue, and the process returns to S12 to continue searching for the next data to be processed in the first data queue. As described above, the second data queue is a queue dedicated to storing synchronization nodes and data to be processed. In this application, the second data queue is a data queue dedicated to storing synchronization node information, and like the first data queue, it preferably adopts a lock-free data structure.
[0093] This process can be understood as follows: when the next node obtained is a synchronous node, the synchronous node identifier of the current synchronous node is obtained, the execution start point identifier of the current data to be processed is reset to the node code of the current synchronous node, and then the data to be processed, together with the synchronous node identifier, is stored in the second data queue. At the same time, the local cache of the current thread is removed, and the current thread continues to process the next data to be processed.
[0094] For example, suppose a piece of data S to be processed corresponds to executable nodes A, B, and C, with node codes "001", "002", and "003" respectively, and only node C has a synchronization node identifier of "Yes". The execution start point identifier of the data S to be processed is "001". According to the mapping table below, the next node after node A is node B, and the next node after node B is node C. Then, after executing nodes A and B in sequence and determining that the current node to be executed, C, is a synchronization node, the execution start point identifier of the current data S to be processed is reset to the node code "003" of the current synchronization node C. Then, the data S to be processed, along with the synchronization node identifier, is stored in the second data queue.
[0095] S16, execute the currently pending node and determine whether the next node of the currently executed node is the end node;
[0096] Specifically, after obtaining the node to be executed, the node is executed. The execution of the node produces a corresponding execution result. If the node to be executed is not a synchronous node, the current node to be executed is executed directly. After the node execution is complete, the execution start point identifier of the current data to be processed needs to be reset to the node code of the currently executed node. Furthermore, if the node execution is successful, it is determined whether the next node of the currently executed node is the end node; if the node execution fails, the failure reason is confirmed and saved, and the process returns to S12 to continue determining whether there is data to be processed in the first data queue. Here, the process after a node execution is successful includes two parts: one is determining the next node of the currently executed node, and the other is determining whether the next node is the end node. For the first part, regardless of whether the node execution is successful or unsuccessful, the execution start point identifier of the corresponding data to be executed needs to be reset to the node code of the currently executed node. When determining the next node of the currently executed node, the next node to be executed is found in the node context mapping table based on the execution start point identifier of the data to be processed. For the second part, it is determined whether the found next node is the end node based on the node type.
[0097] If the next node is the end node, return to S12 and continue searching for the next data to be processed in the first data queue; if the next node is not the end node, return to S14 and continue determining whether the next node is a synchronous node. In other words, regardless of whether the current node to be executed is a synchronous node or an asynchronous node, it will determine whether there is still data to be processed in the return to S12, thereby traversing the data to be processed.
[0098] In a preferred embodiment, the steps for executing the current node to be executed include S161-S165:
[0099] S161, execute the current node to be executed, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node;
[0100] Specifically, after determining the current node to be executed as a synchronization node in S14, the current node to be executed is executed. After the execution of this node is completed, the execution start point identifier of the current data to be processed needs to be reset to the node code of the currently executed node.
[0101] S162, determine whether the current node has executed successfully;
[0102] Specifically, if the current node executes successfully, then S163 is executed; if the current node fails to execute, then S164 is executed.
[0103] Understandably, if an exception occurs during node execution, even if no execution failure feedback is triggered, the node is considered to have failed. For example, if a node does not respond for an extended period, it is considered to have failed.
[0104] S163, perform the first callback processing;
[0105] Specifically, in response to the successful execution of the current node, the first callback process is performed, followed by step S165. Callback processing can be understood as the process engine proactively triggering pre-registered external business logic when a specific node or process instance reaches a predetermined state, enabling bidirectional interaction between the process and external systems. The callback processing in this application includes two types: one is the first callback processing for successful node execution in this step. In the first callback processing, information such as the node code, execution result, output variables, and context snapshot is sent to the external system. The other is the second callback processing in S164 for unsuccessful node execution. In the second callback processing, information such as the node code, exception object, failure context, and number of retries is sent to the external system.
[0106] S164, perform the second callback process;
[0107] Specifically, in response to the failure of the current node execution, after the second callback process is performed, the result of the execution failure and its reason are stored in the failure reason mapping table, and then S12 is executed to continue to determine whether there is any data to be processed in the first data queue.
[0108] S165, determine whether the next node of the currently executed node is the end node;
[0109] Specifically, the next node to be executed for the currently pending data is found in the node context mapping table, and it is confirmed whether the next node is the end node. If the next node of the currently successfully executed node is the end node, S12 is returned, and it is further determined whether there is pending data in the first data queue, that is, the next data is processed in the current thread; if the next node of the currently successfully executed node is not the end node, S14 is returned, and it is further determined whether the next node that is not the end node is a synchronization node.
[0110] In a preferred embodiment, after the first callback process is performed, S1631-S1633 needs to be executed to determine whether the current node is a judgment type node.
[0111] S1631, Determine whether the current node is a judgment type node;
[0112] Specifically, based on the node's judgment class node identifier, determine whether the current node is a judgment class node. If the current node is a judgment class node, execute S1632; if the current node is not a judgment class node, execute S1633.
[0113] S1632, determine the next node based on the execution result of the current node and execute it;
[0114] Specifically, based on the execution result of the current node, the branch that needs to be executed is determined, thereby determining the next node that needs to be executed.
[0115] S1633: Directly obtain the next node and execute it.
[0116] S17, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty;
[0117] Specifically, in response to determining in S12 that there is no more data to be processed by the current thread in the first data queue, indicating that all executable asynchronous nodes in the current thread have been completed, the thread count is decremented by one. Then, it is first determined whether the current thread count is equal to 0. If the current thread count is equal to 0, it means that no other threads are running in the current electronic process, and then it is determined whether the second data queue is empty. If the second data queue is not empty, it means that there are still synchronization nodes to be executed in the current thread, and S18 is executed. If the current thread count is greater than 0, it means that other threads are still running, and S19 is executed. If the second data queue is empty, it means that there are no synchronization nodes to be executed in the current thread, and S19 is also executed.
[0118] S18: Obtain the node to be executed from the second data queue and execute it, and put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue;
[0119] Specifically, in response to a thread count of 0 and a non-empty second data queue, the current node to be executed (synchronization node) is retrieved from the second data queue and executed. After successful execution of this node, the execution start point identifier of the corresponding data to be executed is reset to the node code of the currently executed node. Furthermore, the data corresponding to the executed node is placed back into the first data queue as data to be executed in the first data queue. Then, the process returns to S11, incrementing the thread count. Thus, when S12 is executed again, it will be determined that there is data to be processed in the first data queue, and this data will be processed in subsequent steps, thereby enabling the continuation of the execution of the synchronization node.
[0120] In a preferred embodiment, the execution steps of the synchronization node include S181-S185:
[0121] S181, retrieve the node to be executed from the second data queue and execute it, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node;
[0122] Specifically, after determining in S17 that the thread count is 0 and the second data queue is not empty, the node to be executed is retrieved from the second data queue and executed. After the node is executed, the execution start point identifier of the current data to be processed needs to be reset to the node code of the currently executed node. The storage logic of the second data queue is similar to that of the first data queue. The second data queue stores the synchronization nodes and their data to be processed.
[0123] In a preferred embodiment, when executing the synchronization node, an atomic comparison-and-swap (CAS) operation is used, including S1811-S1814:
[0124] S1811, retrieve the node to be executed from the second data queue;
[0125] Specifically, when placing the data to be processed corresponding to the synchronization node into the second data queue, a synchronization start flag is added to the data to be processed. The initial value of this flag is not started.
[0126] S1812, Perform atomic comparison and swap operations on the data to be processed corresponding to the node to be executed;
[0127] Specifically, when a thread executes a synchronization node, it attempts to acquire permission through an atomic comparison-swap operation. Only the first thread to successfully complete the atomic comparison-swap gains execution permission; other threads skip this step. This controls concurrent access to the synchronization node, ensuring that only one thread executes a node at any given time, achieving single-threaded execution control under multi-threaded contention. In the atomic comparison-swap operation, the expected value set in the atomic comparison is the initial value of the synchronization start flag. After a thread acquires execution permission for a node, it modifies the synchronization start flag from its initial value to another value. If the synchronization start flag equals the preset value, the atomic comparison-swap operation succeeds, and the thread gains execution permission for the node; otherwise, it fails to acquire permission.
[0128] For a thread that has obtained execution privileges, the synchronization nodes are executed in a single-threaded manner, depending on the execution context. That is, within the same thread, nodes that do not require waiting for external responses are executed sequentially, without creating new threads or switching contexts. This ensures the serial execution of multiple data pairs on the synchronization nodes, avoiding concurrency conflicts.
[0129] S1813, in response to the successful atomic comparison exchange operation, the node to be executed is then executed;
[0130] Specifically, if the synchronization start flag equals the preset value, it means that no thread has yet acquired execution permission for this node. The current thread then acquires execution permission for this node, changes the synchronization start flag from its initial value to another value, and executes the node to be executed. If the synchronization start flag does not equal the preset value, it means that another thread has already acquired execution permission for this node. Therefore, since the synchronization start flag does not equal the preset value, the execution result of the current node failing is output.
[0131] For example, multiple threads X, Y, and Z arrive at synchronization node C simultaneously. At this time, the synchronization start flag for the data to be processed corresponding to node C is "not started," and the expected value in the atomic comparison is also "not started." Thread X is the first thread to obtain execution permission from node C by atomically comparing "not started" and "not started." After obtaining permission, thread X updates the synchronization start flag to "started." Then, threads Y and Z, by atomically comparing "not started" and "started," determine that "not started" and "started" are not equal, and therefore will not obtain execution permission from node C.
[0132] S182, determine whether the current node has executed successfully;
[0133] Specifically, if the current node executes successfully, then S183 is executed; if the current node fails to execute, then S184 is executed.
[0134] S183, perform the first callback processing;
[0135] Specifically, in response to the successful execution of the current node, S185 is executed after the first callback is processed.
[0136] S184, perform the second callback process;
[0137] Specifically, in response to the failure of the current node execution, the second callback is executed and S11 is executed, incrementing the current thread count by one.
[0138] S185, the data corresponding to the executed node is returned to the first data queue, which is called the data to be executed in the first data queue;
[0139] Specifically, after the first callback process, the data corresponding to the executed node is put back into the first data queue, which is called the data to be executed in the first data queue. Then, S11 is returned, and the current thread count is incremented by one.
[0140] S19, terminate the current thread.
[0141] Specifically, if the current thread count is greater than 0, indicating that other threads are still running, the current thread is terminated to avoid duplicate data execution or deadlock, and resources are released. If the current thread count is greater than 0 and the second data queue is not empty, then there must be another thread that has determined that the current thread count is equal to 0 and the second data queue is not empty, and is processing the synchronization nodes in the second data queue; therefore, this application will not have any missed nodes. If the second data queue is empty, indicating that there are no synchronization nodes to be executed in the current thread, the current thread is also terminated.
[0142] In a preferred embodiment, this application also utilizes an execution result notifier to mark thread execution results and node asynchronous execution results.
[0143] The internal state machine of the execution result notifier preferably includes six states: New, In Progress, Normal Completion, Abnormal Completion, Cancelled, and Non-Cancellable. State transitions are performed on state fields using atomic comparison and swap operations via an AtomicField Updater, ensuring state transition safety in a multi-threaded environment. The AtomicField Updater is a utility class based on reflection that performs atomic operations on object fields; it is a lightweight atomicity solution in the Java concurrency package.
[0144] Meanwhile, the execution result notifier internally contains a synchronizer that inherits from the first and second data queue synchronizers. When the conditions are met, the synchronizers wake up multiple threads simultaneously in a shared mode to retrieve the data. When the execution result notifier's internal state machine is in the node completed state, it allows the retrieval of the next node or pending data from the data queue and wakes up all waiting threads; otherwise, it blocks and waits.
[0145] In one specific implementation, the synchronizer is an AbstractQueuedSynchronizer (AQS).
[0146] The core functionality provided by the execution result notifier includes: transforming a newly created or non-cancellable state into a "completion" state based on the atomic comparison result. Intermediate values can be filled in the "completion" state to avoid a partially visible state. Once the result value is visible, the state changes to the corresponding final state, finally waking up waiting threads. Similarly, the execution result notifier also supports abnormal completion paths and cancellation paths, blocking or timeout waiting, and non-blocking result queries.
[0147] The thread execution method of the above electronic process is explained below in a specific embodiment.
[0148] In this embodiment, the electronic workflow is a "approval application" workflow, and the data to be processed is the batch of data awaiting approval. For each piece of data awaiting approval, there are three nodes in sequence: "judgment," "approval," and "notification." Among them, the "approval" node is a synchronous node, which must wait for other nodes to "approve" before it can execute the next node. The "judgment" and "notification" nodes are asynchronous nodes, which do not need to wait for any operation from other nodes.
[0149] Suppose that in the above-mentioned electronic process for "approval application", there are three pending data items S1, S2, and S3. Pending data S1 and S2 are assigned to thread X1 for processing, and pending data S3 is assigned to thread X2 for concurrent processing. Specifically, the pending amount in pending data S1 is "1000 yuan", the pending amount in pending data S2 is "10 yuan", and the pending amount in pending data S3 is also "10 yuan".
[0150] For thread X1, when processing pending data S1, if the "judgment" node determines that its amount is large and the next synchronous "approval" node needs to be executed, then pending data S1 is placed in the second data queue, and thread X1 begins processing pending data S2. If the "judgment" node of pending data S2 determines that its amount is small, the "notification" node can be executed directly. After thread X1 finishes processing pending data S2, since thread X2 only has pending data S3, and the "judgment" node of pending data S3 also determines that its amount is small, the "notification" node can be executed directly. The execution process is short, so the "approval" node of pending data S1 may have already been concurrently processed by thread X2. Therefore, thread X1 can directly obtain the "approval result" of pending data S1 and execute the "notification" node. The "approval" node of pending data S1 does not need to be executed in thread X1. In this way, the concurrent processing of multi-threaded, synchronous, and asynchronous nodes can be realized in the "approval application" electronic workflow, which speeds up data processing.
[0151] The thread execution method for the electronic workflow provided in this application supports hybrid scheduling of synchronous and asynchronous nodes within the same workflow instance through thread counting, a first data queue, a second data queue, and the timing of synchronous node judgment. Asynchronous nodes maximize throughput through multi-threaded parallel execution, while synchronous nodes ensure data consistency through lock-free data queue queuing and single-threaded serial execution by the last surviving thread. This eliminates the need for external message queues or distributed locks, achieving efficient workflow execution with hybrid concurrent synchronous and asynchronous nodes. Furthermore, the method also enables dynamic thread expansion, significantly improving the throughput of batch data processing.
[0152] In a second aspect, this application also provides a thread execution apparatus for electronic processes. The thread execution apparatus for electronic processes provided by this invention will now be described, and the thread execution apparatus for electronic processes described below can be referred to in correspondence with the thread execution method for electronic processes described above.
[0153] In this device, an electronic process includes at least one piece of data to be processed, and multiple pieces of data to be processed are processed by one or more threads. Each thread includes at least one node to process the data to be processed. Figure 4 This is a schematic diagram of the structure of the thread execution device for electronic processes provided by the present invention, as shown below. Figure 4 As shown, the device includes:
[0154] The thread startup unit 410 is configured to acquire the thread start instruction.
[0155] The counting unit 420 is configured to increment the thread count by one, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty.
[0156] The data confirmation unit 430 is configured to determine whether there is pending data for the current thread in the first data queue.
[0157] The node lookup unit 440 is configured to look up the node to be executed from the node context mapping table based on the execution start point identifier of the data to be processed if there is data to be processed for the current thread in the first data queue.
[0158] If there is no pending data for the current thread in the first data queue, then count unit 420 is called to decrement the thread count by one, and it is determined whether the current thread count is equal to 0 and whether the second data queue is not empty.
[0159] The first node processing unit 450 is configured to determine whether the currently executed node is a synchronous node.
[0160] If the previous node to be executed is a synchronous node, the execution start point identifier of the current data to be processed is reset to the node code of the current node to be executed. After the current data to be processed is put into the second data queue, the data confirmation unit 430 is called to determine whether there is data to be processed by the current thread in the first data queue.
[0161] If the previous node to be executed is not a synchronous node, then the current node to be executed is executed, and it is determined whether the next node of the currently executed node is the end node.
[0162] If the next node after the currently executed node is the end node, then the data confirmation unit 430 is invoked to determine whether there is any pending data for the current thread in the first data queue.
[0163] If the next node of the currently executed node is not the end node, then determine whether the currently to-be-executed node is a synchronization node.
[0164] The second node processing unit 460 is configured to, if the current thread count is equal to 0 and the second data queue is not empty, retrieve the node to be executed from the second data queue and execute it, and put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue, and then call the counting unit 420 to increment the thread count by one.
[0165] Thread termination unit 470 is configured to terminate the current thread if the current thread count is not equal to 0 or the second data queue is empty.
[0166] Other preferred embodiments, specific implementations, technical problems that can be solved, and technical effects that can be achieved by the above-mentioned thread execution device for electronic processes all correspond to the thread execution method of electronic processes, and will not be repeated here.
[0167] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0168] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0169] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0170] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A thread execution method for electronic processes, characterized in that, The electronic workflow includes at least one piece of data to be processed, and multiple pieces of data to be processed are processed by one or more threads. Each thread includes at least one node to process the data to be processed. The method includes: S10, Obtain the thread start instruction; S11, Increment the thread count by one; S12, determine whether there is any pending data for the current thread in the first data queue. If it exists, execute S13; otherwise, execute S17. S13, find the node to be executed from the node context mapping table according to the execution start point identifier of the data to be processed; S14, determine whether the current node to be executed is a synchronization node. If it is, execute S15; otherwise, execute S16. S15, reset the execution start point identifier of the current data to be processed to the node code of the current node to be processed, put the current data to be processed into the second data queue, and return to S12; S16, execute the current node to be executed, and determine whether the next node of the currently executed node is the end node. If it is, return to S12; otherwise, return to S14. S17, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty. If yes, execute S18; otherwise, execute S19. S18: Obtain the node to be executed from the second data queue and execute it. Then, put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue, and return to S11. S19, terminate the current thread.
2. The method according to claim 1, characterized in that, Following S12, the method further includes: S121, determine whether the data to be processed obtained from the first data queue is empty. If it is not empty, execute S13; if it is empty, execute S12.
3. The method according to claim 1, characterized in that, Step S16 includes: S161, execute the current node to be executed, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node; S162, determine if the current node has executed successfully. If successful, execute S163; otherwise, execute S164. S163, after the first callback is processed, execute S165; S164, after the second callback is processed, execute S12; S165 determines whether the next node of the currently executed node is the end node. If it is, return to S12; otherwise, return to S14.
4. The method according to claim 3, characterized in that, Following S163, the method further includes: S1631, Determine whether the current node is a judgment type node. If it is, execute S1632; otherwise, execute S1633. S1632, determine the next node based on the execution result of the current node and execute it; S1633: Directly obtain the next node and execute it.
5. The method according to claim 1, characterized in that, Step S18 includes: S181, retrieve the node to be executed from the second data queue and execute it, and reset the execution start point identifier of the current data to be processed to the node code of the currently executed node; S182, determine if the current node has executed successfully. If successful, execute S183; otherwise, execute S184. S183, after the first callback processing, execute S185; S184, after the second callback process, execute S185; S185, after putting the data corresponding to the currently executing node back into the first data queue to become the data to be executed in the first data queue, return to S11.
6. The method according to claim 1, characterized in that, The steps in S181 include: S1811, Obtain the node to be executed from the second data queue; S1812, Perform atomic comparison and swap operations on the data to be processed corresponding to the node to be executed; S1813, in response to the successful atomic comparison exchange operation, the node to be executed is executed.
7. The method according to claim 1, characterized in that, The steps in S1812 include: The synchronization enable flag of the data to be processed is compared with a preset value. If the synchronization enable flag is equal to the preset value, the atomic comparison and exchange operation is successful.
8. The method according to claim 1, characterized in that, Both the first data queue and the second data queue are lock-free data queues.
9. The method according to claim 1, characterized in that, In S11 and S17, the threads are counted using an atomic counter.
10. A thread execution device for electronic processes, characterized in that, The electronic workflow includes at least one piece of data to be processed, and multiple pieces of data to be processed are processed by one or more threads. Each thread includes at least one node to process the data to be processed. The device includes: The thread initiation unit (410) is configured to acquire a thread start instruction; The counting unit (420) is configured to increment the thread count by one, decrement the thread count by one, and determine whether the current thread count is equal to 0 and whether the second data queue is not empty; The data confirmation unit (430) is configured to determine whether there is pending data for the current thread in the first data queue; The node lookup unit (440) is configured to look up the node to be executed from the node context mapping table according to the execution start point identifier of the data to be processed if there is data to be processed for the current thread in the first data queue; If there is no pending data for the current thread in the first data queue, the counting unit (420) is called to decrement the thread count by one, and it is determined whether the current thread count is equal to 0 and whether the second data queue is not empty. The first node processing unit (450) is configured to determine whether the currently executed node is a synchronous node; If the previous node to be executed is a synchronous node, then the execution start point identifier of the current data to be processed is reset to the node code of the current node to be executed, and the current data to be processed is put into the second data queue. Then, the data confirmation unit (430) is called to determine whether there is data to be processed for the current thread in the first data queue. If the previous node to be executed is not a synchronous node, then the current node to be executed is executed, and it is determined whether the next node of the currently executed node is the end node; If the next node after the currently executed node is the end node, then the data confirmation unit (430) is invoked to determine whether there is any pending data for the current thread in the first data queue; If the next node of the currently executed node is not the end node, then determine whether the currently to be executed node is a synchronization node; The second node processing unit (460) is configured to, if the current thread count is equal to 0 and the second data queue is not empty, retrieve the node to be executed from the second data queue and execute it, and put the data corresponding to the executed node back into the first data queue to become the data to be executed in the first data queue, and then call the counting unit (420) to increment the thread count by one. The thread termination unit (470) is configured to terminate the current thread if the current thread count is not equal to 0 or the second data queue is empty.