Asynchronous-to-synchronous method and device, equipment, storage medium and product
By generating asynchronous processing results on the asynchronous interface, storing them in the buffer repository, and obtaining and sending the results by the second thread, the existing asynchronous to-sync technology is solved, and efficient asynchronous to-sync processing is achieved, improving the stability and performance of the system.
Patent Information
- Application Number
- CN202411352808.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2044-09-26
AI Technical Summary
The existing asynchronous to-synchronization technology has problems such as inefficiency, complex implementation, or excessive thread resource consumption, and it is difficult to meet the needs in scenarios where the operation sequence or processing result dependency is required.
After the asynchronous processing result is generated by the asynchronous interface, it is stored in the buffer repository, and the second thread obtains the asynchronous processing result according to the call identification, and sends it to the first thread to wake it up to continue execution.
It realizes the rapid acquisition and transmission of asynchronous processing results, improves the efficiency and performance of the system, avoids the risk of excessive consumption of system resources and crashes in high concurrency scenarios, and improves the stability of the system.
Smart Images

Figure CN119690694A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to an asynchronous-to-synchronous conversion method, device, equipment, storage medium, and product. Background Art
[0002] With the rapid development of computer technology, asynchronous processing mode has been widely used due to its efficient resource utilization and excellent responsiveness. However, in some scenarios, synchronous processing is still necessary. For example, in scenarios where the order of operations needs to be ensured or there is a dependency on processing results, it is necessary to convert asynchronous operations into synchronous operations to ensure the smooth execution of tasks. Therefore, asynchronous-to-synchronous technology has emerged to meet these specific needs.
[0003] However, although the existing asynchronous-to-synchronous technologies can meet the needs of asynchronous-to-synchronous conversion to a certain extent, they all have some limitations, such as low efficiency, complex implementation, or excessive consumption of thread resources. Summary of the invention
[0004] The embodiments of the present application are intended to provide an asynchronous-to-synchronous conversion method, apparatus, device, storage medium, and product.
[0005] According to a first aspect of an embodiment of the present application, a method for converting asynchronous to synchronous is provided, including:
[0006] In response to a first call of a first thread to an asynchronous interface, generating a first call identifier corresponding to the first call, and in response to a successful call of the first call, blocking the first thread, wherein the first thread is used to execute synchronous processing logic;
[0007] When the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used to map and store the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result;
[0008] The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
[0009] Optionally, in response to a successful call of the first call, blocking the first thread includes:
[0010] In response to a successful call of the first call, generating a first result object corresponding to the first call, and storing the first result object in a global cache according to the first call identifier;
[0011] Blocking the first thread;
[0012] The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution, including:
[0013] The second thread determines the first result object in the global cache according to the first call identifier, and stores the first asynchronous processing result in the first result object;
[0014] The first thread is woken up to continue execution.
[0015] Optionally, the method is implemented based on the Spring framework, and the first result object is a DeferredResult object;
[0016] The generating a first result object corresponding to the first call includes:
[0017] Generate an empty DeferredResult object corresponding to the first call;
[0018] The blocking of the first thread comprises:
[0019] In response to generating an empty DeferredResult object, the first thread enters a blocked state;
[0020] The waking up the first thread to continue execution includes:
[0021] In response to storing the first call result in the DeferredResult object, the first thread exits the blocking state and continues execution.
[0022] Optionally, the method further includes:
[0023] When generating an unloaded DeferredResult object corresponding to the first call, setting a first time threshold for waiting for the second thread to send the first asynchronous processing result;
[0024] Calling the timeout monitoring method of the DeferredResult object, and when the waiting time is greater than the first time threshold, storing a preset first result in the DeferredResult object, wherein the first result includes information indicating that the waiting time is greater than the first time threshold;
[0025] In response to storing the first result in the DeferredResult object, the first thread exits the blocked state and continues execution.
[0026] Optionally, after the second thread obtains the first asynchronous processing result in the buffer storage according to the first call identifier, the method further includes:
[0027] The second thread performs post-processing on the first asynchronous processing result to obtain a final asynchronous processing result;
[0028] The second thread sends the first asynchronous processing result to the first thread, including:
[0029] The second thread sends the final asynchronous processing result to the first thread.
[0030] Optionally, the buffer repository is a message queue, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result to the message queue, and the second thread is configured to subscribe to the message queue;
[0031] When the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier includes:
[0032] The second thread obtains the asynchronous processing result published by the asynchronous interface to the message queue and the call identifier corresponding to the asynchronous processing result by subscribing to the message queue;
[0033] When it is determined that the call identifier corresponding to the asynchronous processing result is the first call identifier, the asynchronous processing result is determined as the first asynchronous processing result.
[0034] According to a second aspect of an embodiment of the present application, there is provided an asynchronous-to-synchronous conversion device, including:
[0035] A first unit is configured to generate a first call identifier corresponding to a first call to an asynchronous interface in response to a first call to the asynchronous interface by a first thread, and to block the first thread in response to a successful call of the first call, wherein the first thread is configured to execute a synchronous processing logic;
[0036] A second unit is used for obtaining the first asynchronous processing result in the buffer repository according to the first call identifier when the asynchronous interface stores the first asynchronous processing result in the buffer repository, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used for mapping and storing the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result;
[0037] The third unit is used to send the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
[0038] According to a third aspect of an embodiment of the present application, there is provided an electronic device, including a memory and a processor;
[0039] The memory is connected to the processor and is used to store programs;
[0040] The processor is used to implement the asynchronous-to-synchronous method as described in any one of the first aspects of the embodiments of the present application by running the program in the memory.
[0041] According to a fourth aspect of an embodiment of the present application, a storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the asynchronous-to-synchronous method as described in any one of the first aspects of the embodiments of the present application is implemented.
[0042] According to a fifth aspect of an embodiment of the present application, a computer program product is provided, comprising computer program instructions, which, when executed by a processor, enable the processor to implement the asynchronous-to-synchronous method as described in any one of the first aspects of the embodiments of the present application.
[0043] The asynchronous-to-synchronous method proposed in the present application, on the one hand, after the asynchronous interface generates the asynchronous processing result of the first call and stores it in the buffer storage repository, the second thread will quickly obtain the asynchronous processing result and send it to the first thread, thereby waking up the blocked first thread. This precise wake-up mechanism significantly improves the efficiency and performance of the system.
[0044] On the other hand, the present application stores the asynchronous processing results generated by the asynchronous interface in a buffer repository, and the second thread obtains the asynchronous processing results in the buffer repository according to the call identifier, and sends the asynchronous processing results to the first thread. This setting makes the acquisition and sending of the asynchronous processing results no longer rely on a large number of real-time requests, but is mediated by the buffer repository and the second thread. By setting up the buffer database and the second thread, the caching and orderly distribution of the asynchronous processing results are realized, which can balance the acquisition and sending of the asynchronous processing results and smooth the sudden peak of the processing traffic. When the system is in the peak traffic stage, the asynchronous processing results can be immediately published to the buffer database, thereby quickly releasing system resources, and the second thread can subscribe and send the asynchronous processing results according to the system resource situation, avoiding the situation where a large number of asynchronous processing results are simultaneously poured into the system thread, thereby avoiding excessive consumption of system resources and the risk of system crash in high concurrency scenarios, and improving the stability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0046] Figure 1 A flowchart of an asynchronous-to-synchronous method provided in an embodiment of the present application.
[0047] Figure 2 A schematic diagram of the structure of an asynchronous-to-synchronous device provided in an embodiment of the present application.
[0048] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0049] The technical solution of the embodiment of the present application is applicable to various scenarios that require conversion of asynchronous processing to synchronous processing, and is particularly suitable for long communication application scenarios, for example, big data analysis (such as user shopping behavior analysis, etc.), image processing (such as printing services that require pre-processing of uploaded images, etc.), remote service calls (such as requesting vehicle digital keys, etc.), and other application scenarios. In these scenarios, the system often needs to wait for the return result of the asynchronous interface for subsequent synchronous processing.
[0050] The asynchronous processing can be understood as a non-blocking execution mode, which allows other tasks to be continued without waiting for a certain operation to complete. The synchronous processing means that the next task will be started after the execution of one task. The asynchronous-to-synchronous conversion can be understood as the process of converting asynchronous processing to synchronous processing.
[0051] The technical solution provided in the embodiments of the present application can be exemplarily applied to hardware devices such as processors, electronic devices, servers (including cloud servers), or packaged into software programs to be run. When the hardware device executes the processing of the technical solution of the embodiments of the present application, or the above-mentioned software program is run, the automatic splitting of the target task and the automatic calling of the application program interface required for the task can be achieved to complete the purpose of the target task. The embodiments of the present application only exemplarily introduce the specific processing of the technical solution of the present application, and do not limit the specific implementation form of the technical solution of the present application. Any technical implementation form that can execute the processing of the technical solution of the present application can be adopted by the embodiments of the present application.
[0052] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0053] Before introducing this application solution, the relevant technologies are first introduced:
[0054] With the rapid development of computer technology, asynchronous processing mode has been widely used due to its efficient resource utilization and excellent responsiveness. However, in some scenarios, synchronous processing is still necessary. For example, in scenarios where the order of operations needs to be ensured or there is a dependency on processing results, it is necessary to convert asynchronous operations into synchronous operations to ensure the smooth execution of tasks. Therefore, asynchronous-to-synchronous technology has emerged to meet these specific needs.
[0055] However, although the existing asynchronous-to-synchronous technologies can meet the needs of asynchronous-to-synchronous conversion to a certain extent, they all have some limitations, such as low efficiency, complex implementation, or excessive thread resource consumption. For example, in the timed sleep polling method, by setting a certain time interval, the thread is allowed to sleep and wake up continuously during the polling process to check whether the asynchronous operation is completed. Whether the method responds in a timely manner depends entirely on the frequency of the query, and it is difficult to ensure that the system gets a timely and effective response, which is inefficient; the traditional wait / notify method often requires a complex coordination and synchronization mechanism between threads, and requires developers to accurately control the synchronization and communication between threads, including when and how to call wait() and notify(), so as to correctly control the blocking and waking of the current thread, and it is necessary to use synchronized to implement the lock operation between threads. Once the operation is improper, it is easy to cause deadlock or livelock and other problems; in some asynchronous-to-synchronous conversion methods, there is a situation where the asynchronous interface directly returns the asynchronous processing result to the current thread. Although this method is direct, it may cause excessive consumption of thread resources when facing a large number of concurrent requests, thereby affecting the overall performance and stability of the system.
[0056] In view of this, an asynchronous-to-synchronous conversion method, apparatus, device, storage medium, and product according to an embodiment of the present application will be described in detail one by one in the following embodiments.
[0057] Exemplary Methods
[0058] Figure 1 A flowchart of an asynchronous to synchronous method provided in an embodiment of the present application. Figure 1As shown, the asynchronous to synchronous method provided in this embodiment includes steps S101-S103:
[0059] S101. In response to a first call of a first thread to an asynchronous interface, a first call identifier corresponding to the first call is generated, and in response to a successful call of the first call, the first thread is blocked, where the first thread is used to execute synchronous processing logic.
[0060] The first thread can be understood as a working thread that performs a specific task. The processing logic of the task is a synchronous processing logic, which means that the various steps in the task need to be executed in a specific order. In the process of executing this task, since some operations may involve time-consuming data processing, resource acquisition, or external service calls, the first thread needs to call the asynchronous interface and wait for the result of the asynchronous processing after the call is successful, so as to continue the subsequent synchronous processing logic using the result of the asynchronous processing. Therefore, in order to ensure the correct execution of the synchronous processing logic, after the first thread successfully calls the asynchronous interface, it usually needs to wait for the asynchronous interface to complete the processing and return the asynchronous processing result before continuing to execute the subsequent steps.
[0061] The asynchronous interface can be understood as an interface for performing asynchronous operations, which is usually used to handle tasks that may take a long time to complete, such as complex data processing, calls to external services, or file operations. By processing these time-consuming operations in the asynchronous interface, the system can maintain responsiveness and handle multiple tasks at the same time. As for the implementation of the asynchronous interface and the specific method of calling the asynchronous interface, they can be implemented by referring to the relevant prior art, and this application will not elaborate on this.
[0062] The call identifier can be understood as a unique identifier generated during the asynchronous interface call process, which is usually a string or a sequence of numbers with global uniqueness and is used to distinguish multiple asynchronous calls occurring simultaneously, thereby achieving tracking, identification and management of specific asynchronous calls. The first call identifier is an identifier used to uniquely identify the first call to the asynchronous interface.
[0063] Optionally, a call identifier can be generated using a preset algorithm, such as a UUID (Universally Unique Identifier), a timestamp combined with a random number, or other algorithms that can ensure global uniqueness. These algorithms can dynamically generate a call identifier when calling an asynchronous interface without the need for central coordination or pre-allocation.
[0064] The successful calling of the first call can be understood as a state in which the first call initiated by the first thread to the asynchronous interface has been successfully received and started to be processed.
[0065] Specifically, during the process of the first thread making the first call to the asynchronous interface, a first call identifier that uniquely identifies the first call will be automatically generated. When a call request or command corresponding to the first call is sent to the asynchronous interface, the first call identifier will be passed to the asynchronous interface to facilitate subsequent status tracking and result matching of the first call.
[0066] When the asynchronous interface receives the call request or command corresponding to the first call, it will return confirmation information to the first thread, indicating that the call request or command corresponding to the first call has been received and arranged for processing, which means that the first call is successfully called. Because the first thread needs to use the asynchronous processing result corresponding to the first call to continue the subsequent synchronous processing logic, in response to the successful call of the first call, the first thread needs to enter a blocked state, suspend execution, and wait for the asynchronous interface to complete the processing of the first call and return the asynchronous processing result corresponding to the first call.
[0067] S102. When the asynchronous interface stores the first asynchronous processing result in the buffer repository, the second thread is triggered to obtain the first asynchronous processing result in the buffer repository according to the first call identifier. The first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call. The buffer repository is used to map and store the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result.
[0068] The first asynchronous processing result can be understood as the asynchronous processing result obtained after the asynchronous interface receives the request corresponding to the first call and performs asynchronous processing on it, reflecting the final effect or output of the asynchronous operation.
[0069] Optionally, the first asynchronous processing result includes a processing response or data of the asynchronous interface to the first call. Optionally, the first asynchronous processing result includes a combination of one or more of data results, status information (such as whether the asynchronous operation is successful, whether an error is encountered, etc.), or other relevant information (such as detailed logs, related resource links, etc.).
[0070] When the asynchronous interface completes processing of the first call and generates a first asynchronous processing result, the first asynchronous processing result corresponding to the first call and the first call identifier are stored in the buffer storage repository.
[0071] The buffer database can be understood as a data storage system for temporarily storing the asynchronous processing results generated by the asynchronous interface and their corresponding call identifiers. The specific implementation of the buffer repository varies due to different application scenarios and technology stacks. The buffer database can use a lightweight database inside the application, such as a relational database, NoSQL database, etc., or a mature third-party middleware, such as an in-memory database (such as a Redis database), a message queue system (Redis queue, Kafka, RabbitMQ), etc. The specific implementation of the buffer database is determined according to specific business needs, performance requirements and cost considerations, and this application does not limit this.
[0072] The second thread can be understood as a task execution unit running independently in the background, which is used to obtain the asynchronous processing result from the buffer storage repository and send the obtained asynchronous processing result to the corresponding asynchronous interface caller, that is, the corresponding first thread.
[0073] Optionally, in actual implementation, the second thread may be a real operating system thread, or it may be an asynchronous task based on an event loop, or it may be a background task managed by a task scheduler, depending on the system architecture and design requirements, which is not limited in this application.
[0074] Optionally, the number, execution frequency, execution priority and other settings of the second threads can be dynamically adjusted according to the system load to better adapt to different system environments and actual application scenarios.
[0075] Optionally, the operation of the second thread may be event-driven, and when a new asynchronous processing result is stored in the buffer repository, the second thread will be triggered to execute.
[0076] Specifically, after the asynchronous interface generates the first asynchronous processing result, it will send the first asynchronous processing result and the first call identifier to the buffer database, and the buffer database will map and store the received first asynchronous processing result and the first call identifier, and establish a mapping relationship between the first asynchronous processing result and the first call identifier. When the asynchronous interface completes the asynchronous processing of the first call and stores the first asynchronous processing result in the buffer storage, the second thread will be triggered, and the second thread will retrieve and obtain the first asynchronous processing result from the buffer storage according to the first call identifier.
[0077] As an optional implementation, the buffer storage repository is a message queue, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result to the message queue, and the second thread is configured to subscribe to the message queue.
[0078] In this implementation, step S102 "when the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier" includes steps A1-A2:
[0079] A1. The second thread obtains the asynchronous processing result published by the asynchronous interface to the message queue and the call identifier corresponding to the asynchronous processing result by subscribing to the message queue;
[0080] A2. When it is determined that the call identifier corresponding to the asynchronous processing result is the first call identifier, the asynchronous processing result is determined as the first asynchronous processing result.
[0081] Optionally, the message queue uses a redis queue or other message queue that provides a publish / subscribe mechanism, such as RabbitMQ, Apache Kafka, ActiveMQ, etc., which is not limited in this application.
[0082] In this implementation, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result to the message queue after generating the asynchronous processing result. The message queue maps and stores this information. For example, when the message queue is a redis queue, the redis queue uses the call identifier corresponding to the asynchronous processing result as the key and stores the asynchronous call result as the value.
[0083] The second thread is configured to subscribe to the message queue, which means that a monitoring relationship is established between the second thread and the message queue. Once new messages (i.e., the asynchronous processing results generated by the asynchronous interface and their corresponding call identifiers) are published to the queue, the second thread can immediately receive these messages.
[0084] In step A1, the second thread can timely obtain the messages published by the asynchronous interface to the message queue by subscribing to the message queue, that is, the asynchronous processing results generated by the asynchronous interface and their corresponding call identifiers, to ensure that the second thread can timely process the latest asynchronous processing results.
[0085] In step A2, the second thread verifies the asynchronous processing result obtained from the message queue to determine whether the call identifier corresponding to the asynchronous processing result is the first call identifier. If the call identifier corresponding to the asynchronous processing result is the first call identifier, then the asynchronous processing result is the first asynchronous processing result.
[0086] S103: The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
[0087] After the second thread obtains the first asynchronous processing result in the buffer storage repository, the second thread sends the obtained first asynchronous processing result to the first thread, the first thread obtains the first asynchronous processing result corresponding to the first call, the condition for the first thread to execute subsequent synchronous processing logic is met, the first thread is awakened, the first thread changes from the waiting state to the running state, and continues to execute subsequent processing steps according to the first asynchronous processing result.
[0088] As for the specific implementation of blocking the first thread and waking up the first thread, it can be implemented by referring to the relevant prior art, and this application does not limit it. Optionally, the first thread can be awakened by using any one of a variety of methods such as condition variables, semaphores, blocking queues, events or flags, wait() / notify(), using tools in the java.util.concurrent package, or a combination of multiple methods. The specific implementation method is determined according to the actual application scenario, programming language and framework, and this application does not limit it.
[0089] On the one hand, in the asynchronous-to-synchronous method proposed in the present application, after the asynchronous interface generates the asynchronous processing result of the first call and stores it in the buffer storage library, the second thread will quickly obtain the asynchronous processing result and send it to the first thread, thereby waking up the blocked first thread. This precise wake-up mechanism significantly improves the efficiency and performance of the system.
[0090] On the other hand, the present application stores the asynchronous processing results generated by the asynchronous interface in a buffer repository, and the second thread obtains the asynchronous processing results in the buffer repository according to the call identifier, and sends the asynchronous processing results to the first thread. Such a setting makes the acquisition and sending of the asynchronous processing results no longer rely on a large number of real-time requests, but is mediated by the buffer repository and the second thread. By setting the buffer database and the second thread, the caching and orderly distribution of the asynchronous processing results are realized, and the acquisition and sending of the asynchronous processing results can be balanced, and the sudden peak of the processing traffic can be smoothly processed. When the system is in the peak traffic stage, the asynchronous processing results can be immediately published to the buffer database, and the asynchronous processing results can be quickly removed from the corresponding thread, thereby quickly releasing system resources, allowing the thread to continue to process other call requests, and the second thread can subscribe and send the asynchronous processing results according to the system resource situation, avoiding the situation where a large number of asynchronous processing results are simultaneously poured into the system thread. Through the above mechanism, the present application can avoid excessive consumption of system resources and system crash risks in high concurrency scenarios, and improve the stability of the system.
[0091] As an optional implementation, in step S101, "blocking the first thread in response to a successful call of the first call" includes:
[0092] In response to a successful call of the first call, generating a first result object corresponding to the first call, and storing the first result object in a global cache according to the first call identifier;
[0093] Blocking the first thread;
[0094] The first result object can be understood as a data structure used to store and transmit the first asynchronous processing result, ensuring that the first thread can still receive the first asynchronous processing result after being blocked.
[0095] The global cache may be understood as a shared storage space that can be accessed by multiple threads and is used to store the first result object and its corresponding first call identifier.
[0096] The global cache can be implemented in a variety of ways, such as a simple hash table, dictionary, or a more complex cache system. The specific implementation method is determined according to the specific needs and performance considerations of the system, and this application does not provide detailed descriptions. Optionally, in order to avoid thread safety issues, ConcurrentHashMap in Java is used as the global cache, which provides efficient concurrent read and write operations and can ensure the safe storage and retrieval of the first result object in an asynchronous environment.
[0097] Optionally, a global cache is pre-set, and the global cache object and its read and write methods are initialized when the program starts, and then used globally.
[0098] Specifically, in response to the successful call of the first call of the asynchronous interface by the first thread, a first result object corresponding to the first call will be generated, and the first call identifier corresponding to the first call will be used as the key, and the first result object corresponding to the first call will be used as the value, and the data will be stored in the global object in the form of a key-value pair, blocking the first thread and waiting for the result of the asynchronous processing.
[0099] Optionally, when using ConcurrentHashMap in Java as a global cache, the storage of key-value pairs is implemented by calling the put method of the cache implementation class (such as ConcurrentHashMap).
[0100] In this implementation, in step S103, "the second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution" includes:
[0101] The second thread determines the first result object in the global cache according to the first call identifier, and stores the first asynchronous processing result in the first result object; and wakes up the first thread to continue execution.
[0102] Specifically, after the second thread obtains the first asynchronous processing result, it uses the first call identifier as an index to search the global cache. Through the search, the second thread can find the first result object that matches the first call identifier in the global cache. Once the first result object is confirmed, the second thread fills the first asynchronous processing result into the corresponding field of the first result object.
[0103] Optionally, if the first result object matching the first call identifier cannot be found in the global cache, the second thread may need to perform error handling, which may include operations such as logging, sending warnings, or notifying a system administrator.
[0104] Optionally, in addition to filling the first asynchronous processing result into the corresponding field of the first result object, the second thread may also update the status flag of the first result object to indicate that the object now contains a valid asynchronous processing result.
[0105] Optionally, when using ConcurrentHashMap in Java as a global cache, the key-value pairs are obtained by calling the get method of the cache implementation class (such as ConcurrentHashMap).
[0106] In this implementation, by introducing a global cache to store result objects, the system can handle multiple concurrent requests more flexibly, improving the system throughput, while also providing space for subsequent expansion and optimization; in addition, by clear result objects and call identifiers, the complexity and error possibility of inter-thread communication are reduced, improving the stability and efficiency of the system.
[0107] As an optional implementation, the method is implemented based on the Spring framework, and the first result object is a DeferredResult object;
[0108] The generating a first result object corresponding to the first call includes: generating an empty DeferredResult object corresponding to the first call;
[0109] The blocking of the first thread includes: in response to generating an unloaded DeferredResult object, the first thread entering a blocked state;
[0110] The waking up the first thread to continue execution includes: in response to storing the first call result in the DeferredResult object, the first thread exits the blocking state and continues execution.
[0111] Specifically, when the first thread makes the first call to the asynchronous interface, a new DeferredResult object is created and returned immediately. At this time, the DeferredResult object is empty, which means that it has not been assigned any result data. The DeferredResult object acts as a placeholder, indicating that the result of the asynchronous processing will be set at some point in the future.
[0112] When an empty DeferredResult object is created and returned, Spring will know that there is an asynchronous request waiting for the result, and will suspend the processing of the task request corresponding to the first thread, and the first thread will enter a blocked state. In this state, the first thread will suspend execution and wait for the result of the asynchronous operation.
[0113] When the asynchronous operation is completed and the first asynchronous processing result is set to the DeferredResult object (that is, the DeferredResult object is no longer empty), Spring will trigger a callback, which will notify the waiting first thread to exit the blocking state and continue processing.
[0114] In this implementation, by closely combining the working mechanism of DeferredResult with the asynchronous request processing function of Spring, relying on the event-driven and callback mechanism of Spring, the blocking and awakening of the first thread can be automatically realized. Compared with other existing technologies, this implementation does not need to encapsulate the communication between threads by itself, does not need to control the blocking and awakening of threads by itself, and developers do not need to manually manage complex operations such as thread creation, blocking, and awakening, which greatly simplifies the programming model of asynchronous processing, reduces the difficulty of development, improves development efficiency, and does not require developers to explicitly use lock operations such as synchronized, thereby avoiding the risk of deadlock. In addition, the Spring framework has been strictly tested and verified, and its event-driven and callback mechanisms are stable and reliable. By utilizing these mechanisms, this implementation can enhance the stability of the system and reduce problems such as system crashes or performance degradation caused by improper thread management. This implementation provides an efficient, concise and reliable asynchronous processing solution by combining DeferredResult and Spring's asynchronous request processing function.
[0115] As an optional implementation, the method further includes:
[0116] When generating an unloaded DeferredResult object corresponding to the first call, setting a first time threshold for waiting for the second thread to send the first asynchronous processing result;
[0117] Calling the timeout monitoring method of the DeferredResult object, and when the waiting time is greater than the first time threshold, storing a preset first result in the DeferredResult object, wherein the first result includes information indicating that the waiting time is greater than the first time threshold;
[0118] In response to storing the first result in the DeferredResult object, the first thread exits the blocked state and continues execution.
[0119] This implementation adds additional flexibility and robustness to DeferredResult-based asynchronous processing by introducing a timeout mechanism and a timeout listening method.
[0120] First, by setting the first time threshold, the implementation ensures that the first thread will not wait indefinitely for the asynchronous processing result of the second thread. If the second thread cannot complete the task in time for some reason, the first thread will continue to execute after reaching the set time threshold, thereby avoiding potential deadlock or long-term resource occupation problems.
[0121] Secondly, when the waiting time exceeds the set first time threshold, the preset first result is stored in the DeferredResult object through the timeout monitoring method. This first result contains information representing the waiting time timeout, so that the first thread can perceive the occurrence of the timeout event when continuing to execute, and take corresponding processing measures accordingly.
[0122] Again, after the timeout, the first thread can perform flexible error processing according to the preset first result, including but not limited to printing an error log, email alert, SMS alert, etc. For example, the first thread can return an error message to the client, or try other alternatives to complete the request processing.
[0123] This implementation effectively prevents infinite waiting by introducing timeout monitoring and first time threshold setting, provides a timeout feedback mechanism, enhances the robustness of the system, and allows flexible error handling and better user experience. These features make this implementation more reliable and practical when processing asynchronous requests that may be affected by external factors.
[0124] Compared with the wait / notify solution, the present invention can directly implement the processing of timeout exceptions. The wait / notify solution uses the wait(timeout) method with parameters to directly wake up the thread after timeout, and cannot perceive whether it is because the notify method is called or it is automatically woken up due to timeout. However, the present invention monitors the timeout status and can handle the timeout exception once the timeout occurs, such as
[0125] As an optional implementation manner, after the second thread obtains the first asynchronous processing result in the buffer storage according to the first call identifier, the method further includes:
[0126] The second thread performs post-processing on the first asynchronous processing result to obtain a final asynchronous processing result;
[0127] The second thread sends the first asynchronous processing result to the first thread, including:
[0128] The second thread sends the final asynchronous processing result to the first thread.
[0129] The post-processing can be understood as further processing, conversion, verification or other forms of processing of the first asynchronous processing result. Specifically, after the second thread obtains the preliminary first processing results, additional operations can be performed on these preliminary results to meet specific business needs, data format requirements or to improve data quality. Post-processing may include but is not limited to the following operations: data cleaning, data conversion, data aggregation, business logic application, security or compliance checks, result sorting or screening, result packaging, etc.
[0130] In this implementation, by setting up post-processing, the accuracy and availability of data can be improved, ensuring that the final asynchronous processing result sent to the first thread is verified and optimized, which helps reduce errors and improves the stability and reliability of the system. It can also help decouple and modularize the system, making the code clearer and easier to maintain.
[0131] Exemplary Devices
[0132] Corresponding to the above-mentioned asynchronous-to-synchronous method, an embodiment of the present application also provides an asynchronous-to-synchronous device. Figure 2 is a schematic diagram of the structure of an asynchronous-to-synchronous device provided in an embodiment of the present application. Figure 2 As shown, the asynchronous-to-synchronous device provided in the embodiment of the present application includes:
[0133] The first unit 201 is used to generate a first call identifier corresponding to the first call in response to a first call of the first thread to the asynchronous interface, and block the first thread in response to a successful call of the first call, wherein the first thread is used to execute a synchronous processing logic;
[0134] The second unit 202 is used for obtaining the first asynchronous processing result in the buffer repository according to the first call identifier when the asynchronous interface stores the first asynchronous processing result in the buffer repository, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used for mapping and storing the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result;
[0135] The third unit 203 is used to send the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
[0136] Optionally, the first unit 201 may be specifically used for:
[0137] In response to a successful call of the first call, generating a first result object corresponding to the first call, and storing the first result object in a global cache according to the first call identifier;
[0138] Blocking the first thread;
[0139] The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution, including:
[0140] The second thread determines the first result object in the global cache according to the first call identifier, and stores the first asynchronous processing result in the first result object;
[0141] The first thread is woken up to continue execution.
[0142] Optionally, the method is implemented based on the Spring framework, and the first result object is a DeferredResult object;
[0143] The generating a first result object corresponding to the first call includes:
[0144] Generate an empty DeferredResult object corresponding to the first call;
[0145] The blocking of the first thread comprises:
[0146] In response to generating an empty DeferredResult object, the first thread enters a blocked state;
[0147] The waking up the first thread to continue execution includes:
[0148] In response to storing the first call result in the DeferredResult object, the first thread exits the blocking state and continues execution.
[0149] Optionally, the device further comprises:
[0150] A fourth unit is used to set a first time threshold for waiting for the second thread to send the first asynchronous processing result when generating an unloaded DeferredResult object corresponding to the first call;
[0151] A fifth unit is used to call the timeout monitoring method of the DeferredResult object, and when the waiting time is greater than the first time threshold, store a preset first result in the DeferredResult object, where the first result includes information indicating that the waiting time is greater than the first time threshold;
[0152] The sixth unit is configured to, in response to storing the first result in the DeferredResult object, cause the first thread to exit the blocking state and continue execution.
[0153] Optionally, the device further comprises:
[0154] A seventh unit is used to post-process the first asynchronous processing result to obtain a final asynchronous processing result;
[0155] The third unit 203 can be specifically used for:
[0156] The final asynchronous processing result is sent to the first thread.
[0157] Optionally, the buffer storage repository is a redis queue, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result to the redis queue, and the second thread is configured to subscribe to the redis queue;
[0158] The second unit 201 can be specifically used for:
[0159] The second thread obtains the asynchronous processing result published by the asynchronous interface to the redis queue and the call identifier corresponding to the asynchronous processing result by subscribing to the redis queue;
[0160] When it is determined that the call identifier corresponding to the asynchronous processing result is the first call identifier, the asynchronous processing result is determined as the first asynchronous processing result.
[0161] The asynchronous-to-synchronous device provided in this embodiment belongs to the same application concept as the asynchronous-to-synchronous method provided in the above embodiments of this application, and can execute the asynchronous-to-synchronous method provided in any of the above embodiments of this application, and has the corresponding functional modules and beneficial effects of executing the asynchronous-to-synchronous method. For technical details not fully described in this embodiment, please refer to the specific processing content of the asynchronous-to-synchronous method provided in the above embodiments of this application, and will not be repeated here.
[0162] Exemplary Electronic Devices
[0163] Another embodiment of the present application also provides an electronic device, see Figure 3 As shown, the device includes:
[0164] Memory 200 and processor 210;
[0165] The memory 200 is connected to the processor 210 and is used to store programs;
[0166] The processor 210 is used to implement the asynchronous-to-synchronous conversion method disclosed in any of the above embodiments by running the program stored in the memory 200.
[0167] Specifically, the electronic device may further include: a bus, a communication interface 220 , an input device 230 and an output device 240 .
[0168] The processor 210, the memory 200, the communication interface 220, the input device 230 and the output device 240 are connected to each other via a bus.
[0169] A bus may include a pathway that transfers information between components of a computer system.
[0170] The processor 210 may be a general-purpose processor, such as a general-purpose central processing unit (CPU), a microprocessor, etc., or an application-specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of the program of the scheme of the present invention. It may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0171] The processor 210 may include a main processor, and may also include a baseband chip, a modem, and the like.
[0172] The memory 200 stores a program for executing the technical solution of the present invention, and may also store an operating system and other key services. Specifically, the program may include a program code, and the program code includes a computer operation instruction. More specifically, the memory 200 may include a read-only memory (ROM), other types of static storage devices that can store static information and instructions, a random access memory (RAM), other types of dynamic storage devices that can store information and instructions, a disk storage, a flash, and the like.
[0173] The input device 230 may include a device for receiving data and information input by a user, such as a keyboard, a mouse, a camera, a scanner, a light pen, a voice input device, a touch screen, a pedometer, or a gravity sensor.
[0174] Output device 240 may include devices that allow information to be output to a user, such as a display screen, printer, speaker, etc.
[0175] The communication interface 220 may include any transceiver or the like to communicate with other devices or communication networks, such as Ethernet, Radio Access Network (RAN), Wireless Local Area Network (WLAN), etc.
[0176] The processor 210 executes the program stored in the memory 200 and calls other devices, which can be used to implement each step of any asynchronous-to-synchronous method provided in the above embodiments of the present application.
[0177] Exemplary computer program products and storage media
[0178] In addition to the above-mentioned methods and devices, an embodiment of the present application may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to execute the steps of the asynchronous-to-synchronous method according to various embodiments of the present application described in the above-mentioned "Exemplary Method" section of this specification.
[0179] The computer program product may be written in any combination of one or more programming languages to write program codes for performing the operations of the embodiments of the present application, including object-oriented programming languages, such as Java, C++, etc., and conventional procedural programming languages, such as "C" language or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as an independent software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0180] In addition, the embodiment of the present application may also be a storage medium on which a computer program is stored. The computer program is executed by a processor to execute the steps of the asynchronous-to-synchronous method according to various embodiments of the present application described in the above “Exemplary Method” section of this specification, and specifically the following steps may be implemented:
[0181] S101, in response to a first call of a first thread to an asynchronous interface, generating a first call identifier corresponding to the first call, and in response to a successful call of the first call, blocking the first thread, wherein the first thread is used to execute a synchronous processing logic;
[0182] S102: When the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used to map and store the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result;
[0183] S103: The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
[0184] For the aforementioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0185] It should be noted that each embodiment in this specification is described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same or similar parts between the embodiments can be referred to each other. For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0186] The steps in the methods of each embodiment of the present application can be adjusted in order, combined and deleted according to actual needs, and the technical features recorded in each embodiment can be replaced or combined.
[0187] The modules and sub-modules in the devices and terminals in the various embodiments of the present application can be combined, divided and deleted according to actual needs.
[0188] In the several embodiments provided in the present application, it should be understood that the disclosed terminals, devices and methods can be implemented in other ways. For example, the terminal embodiments described above are only schematic, for example, the division of modules or submodules is only a logical function division, and there may be other division methods in actual implementation, for example, multiple submodules or modules can be combined or integrated into another module, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0189] The modules or submodules described as separate components may or may not be physically separated, and the components of the modules or submodules may or may not be physical modules or submodules, that is, they may be located in one place, or they may be distributed on multiple network modules or submodules. Some or all of the modules or submodules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0190] In addition, each functional module or submodule in each embodiment of the present application may be integrated into one processing module, or each module or submodule may exist physically separately, or two or more modules or submodules may be integrated into one module. The above-mentioned integrated modules or submodules may be implemented in the form of hardware or in the form of software functional modules or submodules.
[0191] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0192] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly by hardware, software units executed by a processor, or a combination of the two. The software units may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0193] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprises" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "including a..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0194] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. An asynchronous to synchronous method, characterized in that: include: In response to a first call of a first thread to an asynchronous interface, generating a first call identifier corresponding to the first call, and in response to a successful call of the first call, blocking the first thread, wherein the first thread is used to execute synchronous processing logic; When the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used to map and store the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result; The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
2. The method according to claim 1, characterized in that In response to the successful calling of the first call, blocking the first thread includes: In response to a successful call of the first call, generating a first result object corresponding to the first call, and storing the first result object in a global cache according to the first call identifier; Blocking the first thread; The second thread sends the first asynchronous processing result to the first thread to wake up the first thread to continue execution, including: The second thread determines the first result object in the global cache according to the first call identifier, and stores the first asynchronous processing result in the first result object; The first thread is woken up to continue execution.
3. The method according to claim 2, characterized in that The method is implemented based on the Spring framework, and the first result object is a DeferredResult object; The generating a first result object corresponding to the first call includes: Generate an empty DeferredResult object corresponding to the first call; The blocking of the first thread comprises: In response to generating an empty DeferredResult object, the first thread enters a blocked state; The waking up the first thread to continue execution includes: In response to storing the first call result in the DeferredResult object, the first thread exits the blocking state and continues execution.
4. The method according to claim 3, characterized in that The method further comprises: When generating an unloaded DeferredResult object corresponding to the first call, setting a first time threshold for waiting for the second thread to send the first asynchronous processing result; Calling the timeout monitoring method of the DeferredResult object, and when the waiting time is greater than the first time threshold, storing a preset first result in the DeferredResult object, wherein the first result includes information indicating that the waiting time is greater than the first time threshold; In response to storing the first result in the DeferredResult object, the first thread exits the blocked state and continues execution.
5. The method according to claim 1, characterized in that After the second thread obtains the first asynchronous processing result in the buffer repository according to the first call identifier, the method further includes: The second thread performs post-processing on the first asynchronous processing result to obtain a final asynchronous processing result; The second thread sends the first asynchronous processing result to the first thread, including: The second thread sends the final asynchronous processing result to the first thread.
6. The method according to claim 1, characterized in that The buffer repository is a message queue, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result to the message queue, and the second thread is configured to subscribe to the message queue; When the asynchronous interface stores the first asynchronous processing result in the buffer repository, triggering the second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier includes: The second thread obtains the asynchronous processing result published by the asynchronous interface to the message queue and the call identifier corresponding to the asynchronous processing result by subscribing to the message queue; When it is determined that the call identifier corresponding to the asynchronous processing result is the first call identifier, the asynchronous processing result is determined as the first asynchronous processing result.
7. An asynchronous-to-synchronous device, characterized in that: include: A first unit is configured to generate a first call identifier corresponding to a first call to an asynchronous interface in response to a first call to the asynchronous interface by a first thread, and to block the first thread in response to a successful call of the first call, wherein the first thread is configured to execute a synchronous processing logic; A second unit is used for obtaining the first asynchronous processing result in the buffer repository according to the first call identifier when the asynchronous interface stores the first asynchronous processing result in the buffer repository, wherein the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer repository is used for mapping and storing the first asynchronous processing result and the first call identifier after the asynchronous interface generates the first asynchronous processing result; The third unit is used to send the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
8. An electronic device, characterized in that: including memory and processor; The memory is connected to the processor and is used to store programs; The processor is used to implement the asynchronous-to-synchronous method according to any one of claims 1 to 6 by running the program in the memory.
9. A storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by the processor, the asynchronous-to-synchronous conversion method according to any one of claims 1 to 6 is implemented.
10. A computer program product, characterized in that The method comprises computer program instructions, which, when executed by a processor, enable the processor to implement the asynchronous-to-synchronous conversion method as claimed in any one of claims 1 to 6.
Citation Information
Patent Citations
Method and device for realizing different-to-same conversion of distributed application
CN113641472A
Method and device for realizing service calling
CN114090210A
Asynchronous-to-synchronous conversion method and device, equipment and storage medium
CN117009107A
Automatic return to synchronization context for asynchronous computations
US20110276983A1
Asynchronous pattern
US7248603B1
Cited By
Asynchronous test method of photoetching machine based on conditional variable synchronization
CN119960272A
Synchronous conversion method and system of CTP asynchronous interface, medium and product
CN121786108A
Method and device for realizing asynchronization of synchronous network model, and distributed system
CN121907852A
Interaction method and device for graphics processor, processor, equipment, medium and program product
CN122312364A