Method and device for preventing Java contract calling deadlock, equipment and storage medium
By obtaining thread pool information, separating synchronous and asynchronous threads, and using preset techniques to dynamically adjust the number of synchronous threads through a dynamic thread calculation formula, the thread deadlock problem in the Java contract call process is solved, improving the system's stability and efficiency.
Patent Information
- Application Number
- CN202410856772.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2025-12-30
AI Technical Summary
A thread deadlock issue exists during Java contract calls, causing contract calls to fail.
By obtaining thread pool information, separating synchronous and asynchronous threads, dynamically calculating the number of synchronous threads using a preset dynamic thread calculation formula, and adjusting thread pool parameters to execute contract call tasks, thread deadlock is avoided.
This effectively avoids thread deadlock, reduces the probability of Java contract call failures, and improves system stability and efficiency.
Smart Images

Figure CN121233352A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of contract invocation technology, and in particular to methods, apparatus, devices, and storage media for preventing deadlocks in Java contract invocations. Background Technology
[0002] A smart contract is an automatically executing computer program that runs in cyberspace. When its terms are met, a smart contract is executed automatically without third-party intervention. These contracts are typically stored on a blockchain to ensure transparency and security. During execution, the contract platform allows one contract to invoke other contracts to perform corresponding tasks.
[0003] Among them, Java contracts refer to smart contracts written on the Java platform. In Java contracts, there are situations where contracts call other contracts. Contract call tasks occupy message processing threads, which can cause system freezes in some scenarios, resulting in thread deadlock and ultimately causing Java contract call failures. Summary of the Invention
[0004] The purpose of this invention is to provide a method, apparatus, device, and storage medium for preventing deadlocks in Java contract calls, in order to solve the problem of thread deadlocks occurring when existing Java contracts call contracts, leading to Java contract call failures.
[0005] The specific technical solution of this invention is as follows: In a first aspect of the present invention, a method for preventing deadlock in Java contract calls is provided, comprising: Obtain thread pool information for handling Java contract call tasks; The threads in the thread pool are separated into synchronous threads and asynchronous threads; Based on the first number of asynchronous threads and the thread pool state information, the second number of synchronous threads for executing Java contract call contract tasks is determined by performing dynamic thread calculation according to a preset dynamic thread calculation method. The Java contract call task is assigned to a second number of synchronous threads for execution, and the contract call result is obtained.
[0006] Furthermore, obtaining the thread pool information for processing Java contract call tasks includes: The ThreadPoolExecutor class is called through the thread pool manager to obtain the thread pool information currently allocated to process Java contract call tasks. The thread pool information includes the limit on the number of concurrent contract transactions, the contract call level, and the number of threads.
[0007] Furthermore, the separation of threads in the thread pool into synchronous threads and asynchronous threads includes: Based on the task type processed by the thread, the threads in the thread pool are separated to obtain asynchronous threads for processing non-contract call messages and synchronous threads for processing contract container startup and Java contract call. The first number of asynchronous threads for processing non-contract call messages in the thread pool is obtained through the thread pool manager.
[0008] Further, the step of determining the second number of synchronous threads for executing the Java contract call task by dynamically calculating the number of threads based on the first number of asynchronous threads and the thread pool state information according to a preset thread dynamic calculation method includes: The first number of asynchronous threads is set to L1, the concurrency limit of contract transactions is set to M, and the contract call level is set to N. The second number of synchronous threads executing Java contract call tasks is dynamically calculated according to the preset dynamic thread calculation formula, which is: L2=M*N+L1.
[0009] Furthermore, the step of assigning the Java contract call task to a second number of synchronous threads for execution and obtaining the contract call result includes: Determine whether the current number of synchronization threads in the thread pool exceeds the second number. If the current number of synchronization threads exceeds the second number, set the thread pool parameters according to the second number, allocate the second number of synchronization threads from the current synchronization threads to execute the Java contract call contract task, and obtain the corresponding execution result after the second number of synchronization threads have completed the task. If the current number of synchronization threads does not exceed the second number, multiple synchronization threads are created to make the current number of synchronization threads exceed the second number. The thread pool parameters are set according to the second number, and the second number of synchronization threads are allocated to execute the Java contract call contract task. After the second number of synchronization threads have completed the task, the corresponding execution result is obtained.
[0010] In a second aspect of the present invention, an apparatus for preventing deadlock in Java contract calls is provided, comprising: The information acquisition module is used to obtain thread pool information for processing Java contract call tasks; The thread separation module is used to separate the threads in the thread pool into synchronous threads and asynchronous threads; The thread dynamic calculation module is used to perform thread dynamic calculation according to a preset thread dynamic calculation method based on the first number of asynchronous threads and the thread pool state information, and to determine the second number of synchronous threads that execute Java contract call contract tasks. The thread allocation module is used to assign Java contract call tasks to a second number of synchronous threads for execution and to obtain the contract call results.
[0011] In a third aspect of the present invention, the present invention provides an electronic device, comprising: One or more processors; A storage device for storing one or more programs that, when executed by one or more processors, cause the one or more processors to implement a method for preventing deadlock in Java contract calls, as described in the first aspect.
[0012] In a fourth aspect of the present invention, the present invention provides a computer-readable medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the method for preventing deadlock in Java contract calls as described in the first aspect.
[0013] The beneficial effects of this invention are as follows: The present invention provides a method for preventing deadlock in Java contract calls. This method obtains thread pool information for handling Java contract call tasks; separates the threads in the thread pool into synchronous and asynchronous threads; and then, based on the first number of asynchronous threads and the thread pool state information, dynamically calculates the number of threads according to a preset dynamic thread calculation method to determine the second number of synchronous threads executing Java contract call tasks. This achieves dynamic calculation and adjustment of the number of threads executing Java contract call tasks, avoiding deadlock caused by contract call tasks occupying message processing threads and reducing the probability of Java contract call failures. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in the embodiments of the present 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 in the following description are only some embodiments of the present invention, and those skilled in the art can obtain other drawings based on these drawings without creative effort.
[0015] Figure 1 This is a flowchart of a method for preventing deadlock in Java contract calls according to an embodiment of the present invention; Figure 2 This is a schematic diagram of the module structure of a device for preventing deadlock in Java contract calls according to an embodiment of the present invention. Detailed Implementation
[0016] The technical solutions of the embodiments of the present 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 the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative effort are within the scope of protection of the present invention.
[0017] Example 1: Figure 1 This is a flowchart of a method for preventing deadlock in Java contract calls according to an embodiment of the present invention. (See reference...) Figure 1 As shown, the method for preventing deadlock in Java contract calls according to an embodiment of the present invention mainly includes the following steps: S1: Obtain thread pool information for handling Java contract call tasks; S2: Separate the threads in the thread pool to obtain synchronous threads and asynchronous threads; S3: Based on the first number of asynchronous threads and the thread pool state information, perform dynamic thread calculation according to the preset thread dynamic calculation method to determine the second number of synchronous threads that execute Java contract call contract tasks; S4: Assign the Java contract call task to a second number of synchronous threads for execution and obtain the contract call result.
[0018] Furthermore, in this embodiment of the invention, obtaining the thread pool information for processing Java contract call tasks in step S1 specifically includes the following steps: By calling the ThreadPoolExecutor class through the thread pool manager, you can obtain information about the thread pool currently allocated to handle Java contract call tasks. This thread pool information includes the limit on the number of concurrent contract transactions, the contract call level, and the number of threads.
[0019] Using thread pools avoids the performance overhead of frequently creating and destroying threads. Thread pool management typically relies on specific programming frameworks or libraries. The thread pool manager is responsible for the creation, destruction, and management of the entire thread pool, and dynamically adjusts the number of threads based on task submissions. Therefore, this embodiment of the invention uses a thread pool manager to obtain relevant information about the thread pool. The ThreadPoolExecutor class is a Java class for managing thread pools, providing task management, thread scheduling, and related hook methods to control the state of the thread pool. The ThreadPoolExecutor class can be used to obtain thread pool parameters, including the core thread count, the maximum allowed thread count, thread idle timeout, time units, task queue, thread factory, rejection policy, and other parameters. Based on these parameters, thread pool information can be obtained.
[0020] Furthermore, in this embodiment of the invention, step S2 separates the threads in the thread pool to obtain synchronous threads and asynchronous threads, specifically including the following process: Based on the task type processed by the thread, the threads in the thread pool are separated to obtain asynchronous threads for processing non-contract call messages and synchronous threads for processing contract container startup and Java contract call. The first number of asynchronous threads for processing non-contract call messages in the thread pool is obtained through the thread pool manager.
[0021] Specifically, threads in a thread pool are typically assigned to execute tasks of different types, such as contract calls, contract container startup, data updates, and message pushes. These tasks include both synchronous and asynchronous execution. Therefore, this embodiment of the invention divides threads according to their assigned task types into asynchronous threads used for non-contract call contract messages and synchronous threads used for container startup and contract call contract calls. This ensures that when a Java contract calls a contract, the thread used for non-contract call contract messages will not be occupied, thereby avoiding deadlock problems caused by contract call tasks occupying message processing threads.
[0022] Furthermore, in this embodiment of the invention, step S3, based on the first number of asynchronous threads and the thread pool state information, dynamically calculates the number of threads according to a preset dynamic thread calculation method to determine the second number of synchronous threads executing the Java contract call contract task. This specifically includes the following process: First, set the initial number of asynchronous threads to L1, limit the concurrency of contract transactions to M, and set the contract call level to N.
[0023] Then, the second number L2 of synchronous threads executing Java contract call tasks is dynamically calculated according to the preset dynamic thread calculation formula. The preset dynamic thread calculation formula in this invention is specifically: L2 = M * N + L1.
[0024] Specifically, the thread dynamic calculation method of this invention can dynamically adjust the number of synchronous threads executing Java contract call tasks based on the number of asynchronous threads in the thread pool and the level of the contract call, ensuring that the contract call task does not occupy the asynchronous thread for processing other messages and avoids system freeze.
[0025] Furthermore, in this embodiment of the invention, step S4, which assigns the Java contract call task to a second number of synchronous threads for execution and obtains the contract call result, specifically includes the following process: Determine whether the current number of synchronization threads in the thread pool exceeds the second number. If the current number of synchronization threads exceeds the second number, set the thread pool parameters according to the second number, allocate the second number of synchronization threads from the current synchronization threads to execute the Java contract call contract task, and obtain the corresponding execution result after the second number of synchronization threads have completed the task. If the current number of synchronization threads does not exceed the second number, multiple synchronization threads are created to make the current number of synchronization threads exceed the second number. The thread pool parameters are set according to the second number, and the second number of synchronization threads are allocated to execute the Java contract call contract task. After the second number of synchronization threads have completed the task, the corresponding execution result is obtained.
[0026] Specifically, in this embodiment of the invention, after calculating the number of synchronous threads that execute Java contract call contract tasks, the number of threads in the thread pool can be dynamically adjusted so that the number of threads meets the number of threads required to execute the task.
[0027] This invention obtains thread pool information for processing Java contract call tasks; separates the threads in the thread pool into synchronous threads and asynchronous threads; and then, based on the first number of asynchronous threads in the thread pool and the thread pool state information, dynamically calculates the number of threads according to a preset dynamic thread calculation method to determine the second number of synchronous threads executing Java contract call tasks. This achieves dynamic calculation and adjustment of threads executing Java contract call tasks, avoids deadlock of processing threads caused by contract call tasks occupying message processing threads, and reduces the probability of Java contract call failure.
[0028] Example 2: Refer to Figure 2 As shown, embodiments of the present invention also provide a device for preventing deadlock in Java contract calls, comprising: The information acquisition module is used to obtain thread pool information for processing Java contract call tasks; The thread separation module is used to separate the threads in the thread pool into synchronous threads and asynchronous threads; The thread dynamic calculation module is used to perform thread dynamic calculation according to a preset thread dynamic calculation method based on the first number of asynchronous threads and the thread pool state information, and to determine the second number of synchronous threads that execute Java contract call contract tasks. The thread allocation module is used to assign Java contract call tasks to a second number of synchronous threads for execution and to obtain the contract call results.
[0029] Specifically, in this embodiment of the invention, the information acquisition module calls the ThreadPoolExecutor class through the thread pool manager to obtain the thread pool information currently allocated to process Java contract call tasks. This thread pool information includes the limit on the number of concurrent contract transactions, the contract call level, and the number of threads.
[0030] Specifically, in this embodiment of the invention, the thread separation module separates the threads in the thread pool according to the type of task processed by the thread, to obtain asynchronous threads for processing non-contract call messages and synchronous threads for processing contract container startup and Java contract call contracts, and obtains the first number of asynchronous threads for processing non-contract call messages in the thread pool through the thread pool manager.
[0031] Specifically, threads in a thread pool are typically assigned to execute tasks of different types, such as contract calls, contract container startup, data updates, and message pushes. These tasks include both synchronous and asynchronous execution. Therefore, this embodiment of the invention divides threads according to their assigned task types into asynchronous threads used for non-contract call contract messages and synchronous threads used for container startup and contract call contract calls. This ensures that when a Java contract calls a contract, the thread used for non-contract call contract messages will not be occupied, thereby avoiding deadlock problems caused by contract call tasks occupying message processing threads. Specifically, in this embodiment of the invention, the thread dynamic calculation module first sets the first number of asynchronous threads to L1, limits the concurrency of contract transactions to M, and sets the contract call contract level to N.
[0032] Then, the second number L2 of synchronous threads executing Java contract call tasks is dynamically calculated according to the preset dynamic thread calculation formula. The preset dynamic thread calculation formula in this invention is specifically: L2 = M * N + L1.
[0033] Specifically, the thread dynamic calculation method of this invention can dynamically adjust the number of synchronous threads executing Java contract call tasks based on the number of asynchronous threads in the thread pool and the level of the contract call, ensuring that the contract call task does not occupy the asynchronous thread for processing other messages and avoids system freeze.
[0034] Specifically, in this embodiment of the invention, after obtaining the second number L2 of synchronous threads executing the Java contract call task, the thread allocation module first determines whether the current number of synchronous threads in the thread pool exceeds the second number. If the current number of synchronous threads exceeds the second number, the thread pool parameters are set according to the second number, and the second number of synchronous threads are allocated from the current synchronous threads to execute the Java contract call task. After the second number of synchronous threads completes the task, the corresponding execution result is obtained. If the current number of synchronous threads does not exceed the second number, multiple synchronous threads are created to make the current number of synchronous threads exceed the second number, and the thread pool parameters are set according to the second number. The second number of synchronous threads are allocated to execute the Java contract call task, and the corresponding execution result is obtained after the second number of synchronous threads completes the task.
[0035] Specifically, in this embodiment of the invention, after calculating the number of synchronous threads that execute Java contract call contract tasks, the number of threads in the thread pool can be dynamically adjusted so that the number of threads meets the number of threads required to execute the task.
[0036] Furthermore, embodiments of the present invention also provide an electronic device, comprising: One or more processors; A storage device for storing one or more programs that, when executed by one or more processors, cause the one or more processors to implement the method for preventing Java contract call deadlock in Embodiment 1.
[0037] Furthermore, embodiments of the present invention also provide a computer-readable medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements a method for preventing deadlock in Java contract calls as described in Embodiment 1.
[0038] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state disk (SSD)).
[0039] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, 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 a process, method, article, or apparatus. Without further limitations, 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 said element.
[0040] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0041] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.
Claims
1. A method for preventing a Java contract call deadlock, characterized by, The method comprises the following steps: acquiring thread pool information for processing Java contract invocation contract tasks; separating threads in the thread pool to obtain synchronous threads and asynchronous threads; based on a first number of the asynchronous threads and thread pool state information, performing thread dynamic calculation according to a preset thread dynamic calculation method to determine a second number of synchronous threads for executing Java contract invocation contract tasks; allocating the Java contract invocation contract tasks to the second number of synchronous threads for execution and acquiring contract invocation results.
2. The method for preventing Java contract call deadlock according to claim 1, characterized in that, The step of acquiring the thread pool information for processing the Java contract invocation contract tasks comprises the following steps: acquiring, by a thread pool manager, ThreadPoolExecutor class, thread pool information for currently processing Java contract invocation contract tasks, wherein the thread pool information comprises a contract transaction concurrency limit, a contract invocation contract level and a thread number.
3. The method for preventing Java contract call deadlock according to claim 1, wherein, The step of separating the threads in the thread pool to obtain the synchronous threads and the asynchronous threads comprises the following steps: separating the threads in the thread pool according to task types processed by the threads to obtain asynchronous threads for processing non-contract invocation messages and synchronous threads for processing contract container startup and Java contract invocation contracts, and acquiring, by the thread pool manager, a first number of the asynchronous threads in the thread pool for processing the non-contract invocation messages.
4. The method for preventing Java contract call deadlock according to claim 1, wherein, The step of performing thread dynamic calculation based on the first number of the asynchronous threads and the thread pool state information according to the preset thread dynamic calculation method to determine the second number of the synchronous threads for executing the Java contract invocation contract tasks comprises the following steps: setting the first number of the asynchronous threads as L1, setting the contract transaction concurrency limit as M and setting the contract invocation contract level as N, and dynamically calculating a second number L2 of the synchronous threads for executing the Java contract invocation contract tasks according to a preset thread dynamic calculation formula, wherein the preset thread dynamic calculation formula is L2 = M*N+L1.
5. The method for preventing Java contract call deadlock according to claim 1, wherein, The step of allocating the Java contract invocation contract tasks to the second number of synchronous threads for execution and acquiring contract invocation results comprises the following steps: determining whether the current number of synchronous threads in the thread pool exceeds the second number, if the current number of synchronous threads exceeds the second number, setting thread pool parameters according to the second number, allocating the second number of synchronous threads from the current synchronous threads to execute the Java contract invocation contract tasks, and acquiring corresponding execution results after the second number of synchronous threads complete the tasks; if the current number of synchronous threads does not exceed the second number, creating a plurality of synchronous threads to make the current number of synchronous threads exceed the second number, setting thread pool parameters according to the second number, allocating the second number of synchronous threads to execute the Java contract invocation contract tasks, and acquiring corresponding execution results after the second number of synchronous threads complete the tasks.
6. An apparatus for preventing a Java contract call deadlock, characterized by, The method comprises the following steps: an information acquisition module for acquiring thread pool information for processing Java contract invocation contract tasks; a thread separation module for separating threads in the thread pool to obtain synchronous threads and asynchronous threads; The thread dynamic calculation module is configured to perform thread dynamic calculation according to a preset thread dynamic calculation method based on the first number of asynchronous threads and thread pool state information, and determine a second number of synchronous threads for executing the Java contract call contract task. The thread allocation module is configured to allocate the Java contract call contract task to the second number of synchronous threads for execution, and obtain a contract call result.
7. An electronic device, comprising: The method comprises the following steps: one or more processors; a storage device configured to store one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement the method for preventing Java contract call deadlock according to any one of claims 1 to 5.
8. A computer readable medium having stored thereon a computer program, characterized in that, The computer program, when executed by a processor, implements the method for preventing Java contract call deadlock according to any one of claims 1 to 5.