An asynchronous-to-synchronous conversion method, apparatus, device, storage medium, and product.
By using a buffer repository and a second thread mechanism during the asynchronous-to-synchronous conversion process, the problems of low efficiency and excessive resource consumption in the existing technology are solved, achieving efficient asynchronous-to-synchronous processing and improving the stability and performance of the system.
Patent Information
- Application Number
- CN202411352808.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2044-09-26
AI Technical Summary
Existing asynchronous-to-synchronous conversion technologies suffer from problems such as low efficiency, complex implementation, or excessive consumption of thread resources, making it difficult to maintain system stability and performance in high-concurrency scenarios.
By generating a call identifier and storing the asynchronous processing result in a buffer repository, a second thread is used to retrieve and send the asynchronous processing result to wake up the blocked first thread. The buffer repository and the second thread are combined for mediating processing to achieve caching and orderly distribution of asynchronous processing results, thus avoiding excessive consumption of system resources in high-concurrency scenarios.
It improves system efficiency and performance, balances the acquisition and sending of asynchronous processing results, avoids excessive consumption of system resources and the risk of crash in high-concurrency scenarios, and enhances system stability.
Smart Images

Figure CN119690694B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of communication, in particular to an asynchronous-to-synchronous method, device, equipment, storage medium and product. BACKGROUND
[0002] With the rapid development of computer technology, asynchronous processing mode is widely used due to its efficient resource utilization and excellent response capability. However, in some scenarios, synchronous processing is still necessary, such as in scenarios where the order of operations needs to be ensured or there is a dependency on processing results, asynchronous operations need to be converted into synchronous operations to ensure the smooth execution of tasks. Therefore, asynchronous-to-synchronous technology emerges as the times require to meet these specific needs.
[0003] However, the existing asynchronous-to-synchronous technology can achieve the demand of asynchronous-to-synchronous to some extent, but has some limitations, such as low efficiency, complex implementation or excessive consumption of thread resources. SUMMARY
[0004] Embodiments of the present application are dedicated to providing an asynchronous-to-synchronous method, device, equipment, storage medium and product.
[0005] According to a first aspect of the embodiments of the present application, an asynchronous-to-synchronous method is provided, comprising:
[0006] 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, the first thread being used to execute synchronous processing logic;
[0007] When the asynchronous interface stores a first asynchronous processing result to a buffer storage, a second thread is triggered to obtain the first asynchronous processing result in the buffer storage according to the first call identifier, the first asynchronous processing result including an asynchronous processing result of the asynchronous interface to the first call, the buffer storage being used to store the first asynchronous processing result and the first call identifier in a mapping manner 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, the response to the successful call of the first call includes:
[0010] In response to the successful call of the first call, a first result object corresponding to the first call is generated, and the first result object is stored 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, comprising:
[0013] the second thread determines the first result object in the global cache according to the first call identification, and stores the first asynchronous processing result into the first result object;
[0014] waking up the first thread to continue execution.
[0015] Optionally, the method is implemented based on a Spring framework, and the first result object is a DeferredResult object;
[0016] the first call corresponds to a first result object, comprising:
[0017] generating an empty DeferredResult object corresponding to the first call;
[0018] the blocking of the first thread, comprising:
[0019] in response to generating the empty DeferredResult object, the first thread enters a blocked state;
[0020] the waking up of the first thread to continue execution, comprising:
[0021] in response to storing the first call result into the DeferredResult object, the first thread exits the blocked state and continues execution.
[0022] Optionally, the method further comprises:
[0023] when generating the empty 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 a timeout monitoring method of the DeferredResult object, and storing a preset first result into the DeferredResult object when a waiting time is greater than the first time threshold, the first result comprising information representing that the waiting time is greater than the first time threshold;
[0025] in response to storing the first result into the DeferredResult object, the first thread exits the blocked state and continues execution.
[0026] Optionally, after the second thread acquires the first asynchronous processing result in the buffer storage according to the first call identifier, the method further comprises:
[0027] The second thread post-processes 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, comprising:
[0029] The second thread sends the final asynchronous processing result to the first thread.
[0030] Optionally, the buffer storage is a message queue, and 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 to the buffer storage, the second thread acquires the first asynchronous processing result in the buffer storage according to the first call identifier, comprising:
[0032] The second thread acquires the asynchronous processing result and the call identifier corresponding to the asynchronous processing result published to the message queue by the asynchronous interface through 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 the embodiments of the present application, an asynchronous-to-synchronous conversion device is provided, comprising:
[0035] A first unit is configured to generate a first call identifier corresponding to a first call of an asynchronous interface in response to the first call of the asynchronous interface by a first thread, and 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 configured to acquire a first asynchronous processing result in a buffer storage according to a first call identifier when the asynchronous interface stores the first asynchronous processing result to the buffer storage, wherein the first asynchronous processing result comprises an asynchronous processing result of the asynchronous interface on the first call, and the buffer storage is configured to store the first asynchronous processing result and the first call identifier in a mapping manner after the asynchronous interface generates the first asynchronous processing result.
[0037] A third unit is configured 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 the embodiments of the present application, an electronic device is provided, comprising a memory and a processor;
[0039] The memory is connected with the processor, and is configured to store a program;
[0040] The processor is configured to realize the asynchronous-to-synchronous method according to any one of the first aspect of the embodiments of the present application by running the program in the memory.
[0041] According to a fourth aspect of the embodiments of the present application, a storage medium is provided, and the storage medium stores a computer program. When the computer program is run by a processor, the asynchronous-to-synchronous method according to any one of the first aspect of the embodiments of the present application is realized.
[0042] According to a fifth aspect of the embodiments of the present application, a computer program product is provided, comprising computer program instructions. When the computer program instructions are run by a processor, the processor realizes the asynchronous-to-synchronous method according to any one of the first aspect of the embodiments of the present application.
[0043] The asynchronous-to-synchronous method provided in the present application, on the one hand, after the asynchronous interface generates an asynchronous processing result of a first call and stores the asynchronous processing result in a buffer storage, a second thread quickly acquires the asynchronous processing result and sends the asynchronous processing result 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 result generated by the asynchronous interface in the buffer storage, and the second thread acquires the asynchronous processing result in the buffer storage according to the call identifier and sends the asynchronous processing result to the first thread. In this way, the acquisition and sending of the asynchronous processing result no longer depend on a large number of real-time requests, but are mediated by the buffer storage and the second thread. By setting the buffer database and the second thread, the caching and orderly distribution of the asynchronous processing result are realized, the acquisition and sending of the asynchronous processing result are balanced, and the burst peak of the processing traffic is smoothed. When the system is in a traffic peak period, the asynchronous processing result can be immediately published to the buffer database, thereby quickly releasing system resources. The second thread can subscribe and send the asynchronous processing result according to the system resource situation, avoiding the situation that a large number of asynchronous processing results simultaneously flow into the system thread, thereby avoiding the risk of excessive consumption of system resources and system crash in a high-concurrency scenario, and improving the stability of the system. BRIEF DESCRIPTION OF DRAWINGS
[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or prior art description will be briefly introduced. Obviously, the drawings in the following description are only embodiments of the present application, and for those skilled in the art, other drawings can be obtained without creative labor on the basis of the provided drawings.
[0046] FIG. 1 A flowchart of an asynchronous-to-synchronous method provided by an embodiment of the present application.
[0047] FIG. 2 A structural diagram of an asynchronous-to-synchronous device provided by an embodiment of the present application.
[0048] FIG. 3 A structural diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0049] The technical solutions of the embodiments of the present application are applicable to various scenarios requiring asynchronous processing to be converted to synchronous processing, and are particularly applicable to long communication application scenarios, such as big data analysis (e.g., user shopping behavior analysis), image processing (e.g., printing services requiring preprocessing of uploaded pictures), remote service invocation (e.g., requesting a vehicle digital key), and the like. In these scenarios, the system often needs to wait for the return result of an asynchronous interface to perform subsequent synchronous processing.
[0050] The asynchronous processing can be understood as a non-blocking execution mode, which allows other tasks to continue to be executed without waiting for a certain operation to be completed. The synchronous processing refers to starting to execute a next task only after a previous task is executed. The asynchronous-to-synchronous conversion can be understood as a process of converting asynchronous processing to synchronous processing.
[0051] The technical solutions provided by 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 devices execute the processing process of the technical solutions of the present application, or the above software programs are run, the automatic splitting of target tasks and the automatic calling of application program interfaces required by the tasks can be realized, and the purpose of completing the target tasks can be achieved. The embodiments of the present application only exemplarily introduce the specific processing process of the technical solutions of the present application, and do not limit the specific implementation forms of the technical solutions of the present application. Any technical implementation form that can execute the processing process of the technical solutions of the present application can be adopted by the embodiments of the present application.
[0052] With reference to the drawings of the embodiments of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described. Obviously, the described embodiments are only a part of the embodiments of the present application, but not all the embodiments of the present application. Based on the embodiments of the present application, all the other embodiments obtained by a person of ordinary skill in the art without creative effort are within the scope of protection of the present application.
[0053] Before introducing the technical solutions of the present application, the related art will be introduced first.
[0054] With the rapid development of computer technology, the asynchronous processing mode is widely used due to its efficient resource utilization and excellent response capability. However, in some scenarios, synchronous processing is still necessary, such as in scenarios where the order of operations needs to be ensured or there is a dependency on processing results, asynchronous operations need to be converted into synchronous operations to ensure the smooth execution of tasks, and thus, the asynchronous-to-synchronous technology is born to meet these specific needs.
[0055] However, the existing asynchronous-to-synchronous technology can achieve the asynchronous-to-synchronous requirement to some extent, but has some limitations, such as low efficiency, complex implementation, or excessive consumption of thread resources. For example, in the timing sleep polling method, a certain time interval is set to let the thread sleep and wake up continuously in the polling process to check whether the asynchronous operation is completed. The timeliness of the response depends entirely on the query frequency, which makes it difficult to ensure that the system gets timely and effective response, and the efficiency is low. In the traditional wait / notify method, complex thread coordination and synchronization mechanisms are often needed, and the developer needs to accurately control the synchronization and communication between threads, including when and how to call wait(), notify(), so as to correctly control the blocking and waking up of the current thread, and the lock operation between threads needs to be implemented by using synchronized. If the operation is not proper, it is easy to cause problems such as deadlock or livelock. In some asynchronous-to-synchronous methods, there is a case where the asynchronous interface directly returns the asynchronous processing result to the current thread. Although this method is direct, when facing a large number of concurrent requests, it may lead to excessive consumption of thread resources, thereby affecting the overall performance and stability of the system.
[0056] Therefore, the asynchronous-to-synchronous method, device, equipment, storage medium, and product provided by the embodiments of the present application will be described one by one in the following embodiments.
[0057] Exemplary method
[0058] FIG. 1 A flowchart of an asynchronous-to-synchronous method provided by the embodiments of the present application is shown in FIG. 1. As shown in FIG. 1, the method comprises the following steps. FIG. 1As shown, the asynchronous-to-synchronous conversion method provided by the embodiment includes steps S101-S103:
[0059] S101, in response to a first call of an asynchronous interface by a first thread, 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, the first thread being used to execute synchronous processing logic.
[0060] The first thread can be understood as a worker thread executing a specific task, and the processing logic of the task is synchronous processing logic, meaning that each step in the task needs to be executed in a specific order. During the execution of this task, since some operations may involve time-consuming data processing, resource acquisition or external service calls, etc., 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 that executes asynchronous operations, and is usually used to process tasks that may take a long time to complete, such as complex data processing, external service calls or file operations, etc. By placing these time-consuming operations in the asynchronous interface for processing, the system can maintain responsiveness while processing multiple tasks. As for the implementation of the asynchronous interface and the specific method of calling the asynchronous interface, it can be implemented according to related prior art, and this application does not make detailed description.
[0062] The call identifier can be understood as a unique identifier generated during the call of the asynchronous interface, which is usually a string or a sequence of numbers, and has global uniqueness, which is used to distinguish multiple asynchronous calls occurring at the same time, and then to track, identify and manage a specific asynchronous call. The first call identifier is an identifier for uniquely identifying the first call of the asynchronous interface.
[0063] Optionally, a pre-set algorithm can be used to generate the call identifier, such as UUID (Universally Unique Identifier), timestamp combined with random number or other algorithms that can guarantee global uniqueness. These algorithms can dynamically generate call identifiers when the asynchronous interface is called, without the need for central coordination or pre-allocation.
[0064] The successful call 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 processing.
[0065] Specifically, in the process of the first thread making the first call to the asynchronous interface, a first call identifier uniquely identifying the first call is automatically generated, and the first call identifier is transmitted to the asynchronous interface together with a call request or command corresponding to the first call when the call request or command is sent to the asynchronous interface, so as to track the state of the first call and match the result of the first call in the future.
[0066] When the asynchronous interface receives the call request or command corresponding to the first call, the asynchronous interface returns 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, meaning that the first call is successfully called. Because the first thread needs to continue the subsequent synchronous processing logic using the asynchronous processing result corresponding to the first call, in response to the successful call of the first call, the first thread needs to enter a blocked state to 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 storage, a second thread is triggered to obtain the first asynchronous processing result in the buffer storage according to the first call identifier, the first asynchronous processing result includes the asynchronous processing result of the asynchronous interface on the first call, and the buffer storage is used to 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 the request, reflecting the final effect or output of the asynchronous operation.
[0069] Optionally, the first asynchronous processing result includes the processing response or data of the asynchronous interface on the first call. Optionally, the first asynchronous processing result includes one or more combinations of data results, state information (such as whether the asynchronous operation is successful, whether an error is encountered, etc.), or other related information (such as detailed logs, related resource links, etc.).
[0070] When the asynchronous interface completes the processing of the first call and generates the 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 together,
[0071] The buffer database can be understood as a data storage system for temporarily storing asynchronous processing results generated by an asynchronous interface and their corresponding call identifiers. The specific implementation of the buffer storage repository varies depending on the application scenario and technology stack. The buffer database can use a lightweight database within the application, such as a relational database, a NoSQL database, etc., or a mature third-party middleware, such as an in-memory database (e.g., Redis database), a message queue system (Redis queue, Kafka, RabbitMQ), etc. The specific implementation of the buffer database is determined based on specific business requirements, performance requirements, and cost considerations, which are not limited in this application.
[0072] The second thread can be understood as a task execution unit running independently in the background, used to obtain asynchronous processing results from the buffer storage repository and send the obtained asynchronous processing results to the corresponding asynchronous interface caller, i.e., the corresponding first thread.
[0073] Optionally, in actual implementation, the second thread can be a real operating system thread, an asynchronous task based on an event loop, or a background task managed by a task scheduler, depending on the system architecture and design requirements, which are not limited in this application.
[0074] Optionally, the number, execution frequency, and execution priority of the second thread can be dynamically adjusted according to the system load to better adapt to different system environments and actual application scenarios.
[0075] Optionally, the running of the second thread can be event-driven, and the second thread will be triggered to execute when new asynchronous processing results are stored in the buffer storage repository.
[0076] Specifically, after generating the first asynchronous processing result, the asynchronous interface sends the first asynchronous processing result and the first call identifier to the buffer database, which stores the received first asynchronous processing result and the first call identifier, establishing 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 repository, it triggers the second thread, which retrieves and obtains the first asynchronous processing result from the buffer storage repository according to the first call identifier.
[0077] As an optional implementation, the buffer storage repository is a message queue, and 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 the implementation, the step S102 of triggering the second thread to obtain 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 into the buffer repository comprises steps A1-A2:
[0079] A1, the second thread obtains the asynchronous processing result published by the asynchronous interface into 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 is a redis queue or other message queue providing a publish / subscribe mechanism, such as RabbitMQ, Apache Kafka, ActiveMQ, etc., which is not limited in the application.
[0082] In the implementation, the asynchronous interface is configured to publish the generated asynchronous processing result and the call identifier corresponding to the asynchronous processing result into the message queue after generating the asynchronous processing result, and the message queue stores the information in a mapping manner, for example, when the message queue is a redis queue, the redis queue stores the call identifier corresponding to the asynchronous processing result as a key and stores the asynchronous call result as a value.
[0083] The second thread is configured to subscribe to the message queue, which means that a listening relationship is established between the second thread and the message queue, and once new messages (i.e., the asynchronous processing result generated by the asynchronous interface and the call identifier corresponding thereto) are published into the queue, the second thread can immediately receive the messages.
[0084] In step A1, the second thread can obtain the messages published by the asynchronous interface into the message queue, i.e., the asynchronous processing result generated by the asynchronous interface and the call identifier corresponding thereto, in time by subscribing to the message queue, so as to ensure that the second thread can process the latest asynchronous processing result in time.
[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, and if the call identifier corresponding to the asynchronous processing result is the first call identifier, 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 acquires the first asynchronous processing result in the buffer storage, the second thread sends the acquired first asynchronous processing result to the first thread, the first thread acquires the first asynchronous processing result corresponding to the first call, the condition that the first thread executes subsequent synchronous processing logic is satisfied, the first thread is woken up, the first thread changes from the waiting state to the running state, and the first thread continues to execute the subsequent processing step according to the first asynchronous processing result.
[0088] As for the specific implementation of blocking the first thread and waking up the first thread, refer to the related prior art implementation, and the application does not limit this. Optionally, the first thread can be woken up in any one or a combination of multiple ways such as a condition variable, a semaphore, a blocking queue, an event or a flag, wait() / notify(), and the use of tools in the java.util.concurrent package. The specific implementation is determined according to the actual application scenario, programming language and framework, and the application does not limit this.
[0089] On the one hand, the asynchronous-to-synchronous method provided by the application can quickly acquire the asynchronous processing result and send it to the first thread after the asynchronous interface generates the asynchronous processing result of the first call and stores it in the buffer storage, 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 application stores the asynchronous processing result generated by the asynchronous interface in the buffer storage, and the second thread acquires the asynchronous processing result in the buffer storage according to the call identifier and sends the asynchronous processing result to the first thread. In this way, the acquisition and sending of the asynchronous processing result no longer depend on a large number of real-time requests, but are mediated by the buffer storage and the second thread. By setting the buffer database and the second thread, the application realizes the caching and orderly distribution of the asynchronous processing result, balances the acquisition and sending of the asynchronous processing result, and smooths the burst peak of the processing flow. When the system is in a peak flow period, the asynchronous processing result can be immediately published to the buffer database, the asynchronous processing result is 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 result according to the system resource situation, avoiding the situation that a large number of asynchronous processing results enter the system thread at the same time. Through the above mechanism, the application can avoid the risk of excessive consumption of system resources and system crash in a high-concurrency scenario, and improve the stability of the system.
[0091] As an optional implementation, the step S101 of "blocking the first thread in response to the successful call of the first call" comprises:
[0092] In response to the successful invocation of the first call, a first result object corresponding to the first call is generated, and the first result object is stored in a global cache according to the first call identifier;
[0093] The first thread is blocked;
[0094] The first result object can be understood as a data structure for storing and transmitting 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 can be understood as a shared storage space that can be accessed by multiple threads, used for storing the first result object and the first call identifier corresponding thereto.
[0096] The global cache can be implemented in various ways, such as a simple hash table, dictionary, or 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 make detailed description. 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, the global cache is pre-set and the global cache object and its read-write method are initialized when the program starts, and then it is used globally.
[0098] Specifically, in response to the successful invocation of the first call of the asynchronous interface by the first thread, a first result object corresponding to the first call is generated, the first call identifier corresponding to the first call is used as the key, and the first result object corresponding to the first call is used as the value. In the form of key-value pair, it is stored in the global object, the first thread is blocked, and the result of asynchronous processing is waited.
[0099] Optionally, when ConcurrentHashMap in Java is used as the global cache, the storage of the key-value pair is implemented by calling the put method of the cache implementation class (such as ConcurrentHashMap).
[0100] In this implementation, the 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, when the second thread obtains the first asynchronous processing result, the first call identifier is used as an index to retrieve the global cache. Through the retrieval, the second thread can find the first result object matching 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 can need to perform error processing, which can include operations such as logging, sending a warning, 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 can also update the state flag of the first result object to indicate that the object now contains valid asynchronous processing results.
[0105] Optionally, when using ConcurrentHashMap in Java as the global cache, the key-value pair is obtained by calling the get method of the cache implementation class (such as ConcurrentHashMap).
[0106] In this implementation mode, the global cache is introduced to store the result object, so that the system can more flexibly handle multiple concurrent requests, improve the throughput of the system, and also provide space for subsequent expansion and optimization. In addition, through the explicit result object and the call identifier, the complexity and error possibility of inter-thread communication are reduced, and the stability and efficiency of the system are improved.
[0107] As an optional implementation mode, the method is implemented based on the Spring framework, and the first result object is a DeferredResult object.
[0108] The method further includes: generating the first result object corresponding to the first call, including generating the empty-loaded DeferredResult object corresponding to the first call.
[0109] The method further includes: blocking the first thread, including: in response to generating the empty-loaded DeferredResult object, the first thread enters a blocked state.
[0110] The method further includes: waking up the first thread to continue execution, including: in response to storing the first call result into the DeferredResult object, the first thread exits the blocked state to continue 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, meaning that it has not been assigned any result data yet. The DeferredResult object acts as a placeholder, indicating that the result of the asynchronous processing will be set at a future point in time.
[0112] When an empty DeferredResult object is created and returned, Spring knows that there is an asynchronous request waiting for a result, and the processing of the task request corresponding to the first thread is suspended. The first thread enters a blocked state. In this state, the first thread suspends execution and waits 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 (i.e., the DeferredResult object is no longer empty), Spring triggers a callback that notifies the waiting first thread to exit the blocked state and continue processing.
[0114] In this implementation, by tightly integrating the DeferredResult mechanism with the asynchronous request processing function of Spring and relying on the event-driven and callback mechanisms of Spring, the blocking and awakening of the first thread can be automatically implemented. Compared with other existing technologies, this implementation does not need to encapsulate communication between threads itself, does not need to control the blocking and awakening of threads itself, and developers do not need to manually manage complex operations such as thread creation, blocking, and awakening, greatly simplifying the programming model of asynchronous processing, reducing development difficulty, improving development efficiency, and eliminating the need for developers to explicitly use lock operations such as synchronized, thereby avoiding the risk of deadlocks. In addition, the event-driven and callback mechanisms of the Spring framework are stable and reliable after rigorous testing and verification. 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, simple, and reliable asynchronous processing solution by combining DeferredResult and the asynchronous request processing function of Spring.
[0115] As an optional implementation, the method further includes:
[0116] When generating the empty 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 a timeout listening method of the DeferredResult object, and storing a preset first result into the DeferredResult object when the waiting time is greater than the first time threshold, the first result including information representing that the waiting time is greater than the first time threshold;
[0118] In response to storing the first result into the DeferredResult object, the first thread exits the blocking 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] Firstly, by setting the first time threshold, this 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 due to some reason, the first thread will continue execution after reaching the set time threshold, thereby avoiding potential deadlocks or long-term resource occupation problems.
[0121] Secondly, when the waiting time exceeds the set first time threshold, the preset first result is stored into the DeferredResult object through the timeout listening method, and this first result contains information representing that the waiting time has timed out, so that the first thread can perceive the occurrence of the timeout event when continuing execution and take corresponding processing measures accordingly.
[0122] Thirdly, the first thread can perform flexible error handling after timeout according to the preset first result, including but not limited to printing error logs, email alerts, SMS alerts, etc. For example, the first thread can return an error message to the client or try other alternative solutions to complete the processing of the request.
[0123] This implementation effectively prevents infinite waiting by introducing timeout listening 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 handling asynchronous requests that may be affected by external factors.
[0124] Compared with the wait / notify scheme, the present application can directly implement the processing of timeout exceptions. The wait / notify scheme uses the parametric wait(timeout) method to directly wake up the thread after timeout, and cannot perceive whether it is woken up by calling the notify method or automatically woken up due to timeout. The present application listens to the timeout state and can handle timeout exceptions once timeout occurs, such as
[0125] As an optional implementation, after the second thread obtains the first asynchronous processing result in the buffer repository according to the first call identifier, the method further includes:
[0126] The second thread post-processes 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 on the first asynchronous processing result. Specifically, after the second thread obtains the preliminary first processing result, additional operations can be performed on these preliminary results to meet specific business requirements, data format requirements or to improve data quality. Post-processing can include but is not limited to the following operations: data cleaning, data conversion, data aggregation, business logic application, security or compliance check, result sorting or screening, result packaging, etc.
[0130] In this implementation, by setting post-processing, the accuracy and usability of data can be improved, and it is ensured that the final asynchronous processing result sent to the first thread is verified and optimized, which helps to reduce errors and improve the stability and reliability of the system. At the same time, it can also help to decouple and modularize the system, so that the code is clearer and easier to maintain.
[0131] Exemplary apparatus
[0132] Corresponding to the above-mentioned asynchronous-to-synchronous method, the present application embodiment also provides an asynchronous-to-synchronous device. FIG. 2 is a structural schematic diagram of an asynchronous-to-synchronous device provided by the present application embodiment, as FIG. 2 shown, the asynchronous-to-synchronous device provided by the present application embodiment includes:
[0133] The first unit 201 is configured to, in response to a first call of an asynchronous interface by a first thread, generate a first call identifier corresponding to the first call, and in response to a successful call of the first call, block the first thread, the first thread being configured to execute a synchronous processing logic;
[0134] The second unit 202 is configured to acquire the first asynchronous processing result in the buffer storage according to the first call identifier when the asynchronous interface stores the first asynchronous processing result into the buffer storage, the first asynchronous processing result comprising an asynchronous processing result of the first call by the asynchronous interface, and the buffer storage being configured to store the first asynchronous processing result and the first call identifier in a mapping manner after the asynchronous interface generates the first asynchronous processing result.
[0135] The third unit 203 is configured 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 can be specifically configured to:
[0137] in response to successful calling of the first call, generate a first result object corresponding to the first call, and store the first result object into a global cache according to the first call identifier;
[0138] block 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 into the first result object;
[0141] wake up the first thread to continue execution.
[0142] Optionally, the method is implemented based on a Spring framework, and the first result object is a DeferredResult object.
[0143] The generating of the first result object corresponding to the first call comprises:
[0144] generating an empty DeferredResult object corresponding to the first call;
[0145] The blocking of the first thread comprises:
[0146] in response to generating the empty DeferredResult object, the first thread enters a blocked state;
[0147] The waking up of the first thread to continue execution comprises:
[0148] in response to storing the first call result into the DeferredResult object, the first thread exits the blocked state to continue execution.
[0149] Optionally, the apparatus further comprises:
[0150] a fourth unit configured to set a first time threshold for waiting for the second thread to send the first asynchronous processing result when generating the idle DeferredResult object corresponding to the first call;
[0151] a fifth unit configured to invoke a timeout monitoring method of the DeferredResult object, and store a preset first result into the DeferredResult object when the waiting time is greater than the first time threshold, the first result including information representing that the waiting time is greater than the first time threshold;
[0152] a sixth unit configured to cause the first thread to exit the blocking state and continue execution in response to storing the first result into the DeferredResult object.
[0153] Optionally, the apparatus further comprises:
[0154] a seventh unit configured to post-process the first asynchronous processing result to obtain a final asynchronous processing result;
[0155] The third unit 203 can be specifically configured to:
[0156] send the final asynchronous processing result to the first thread.
[0157] Optionally, the buffer storage 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 configured to:
[0159] the second thread acquires the asynchronous processing result and the call identifier corresponding to the asynchronous processing result published by the asynchronous interface to the redis queue through 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 conversion device provided by the embodiment belongs to the same application concept as the asynchronous-to-synchronous conversion method provided by the above-mentioned embodiments of the application, can execute the asynchronous-to-synchronous conversion method provided by any of the above-mentioned embodiments of the application, and has the corresponding function modules and beneficial effects of executing the asynchronous-to-synchronous conversion method. The technical details not described in detail in the embodiment can be found in the specific processing content of the asynchronous-to-synchronous conversion method provided by the above-mentioned embodiments of the application, and will not be described here.
[0162] Exemplary electronic device
[0163] Another embodiment of the application also provides an electronic device, as shown in FIG. 3 The device comprises:
[0164] a memory 200 and a processor 210;
[0165] The memory 200 is connected with the processor 210, and is configured to store a program.
[0166] The processor 210 is configured to execute the asynchronous-to-synchronous conversion method disclosed in any of the above-mentioned embodiments by running the program stored in the memory 200.
[0167] Specifically, the electronic device can further comprise 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 with each other through the bus.
[0169] The bus can comprise a path for transmitting information between various components of the computer system.
[0170] The processor 210 can be a general-purpose processor, such as a general-purpose central processing unit (CPU), a microprocessor, etc., or can be an application-specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of programs of the application scheme. It can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a ready-to-use programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component.
[0171] The processor 210 can include a main processor, and can also include a baseband chip, a modem, etc.
[0172] The memory 200 stores programs for implementing the technical solutions of the present application, and can also store an operating system and other key services. Specifically, the programs can include program codes, which include computer operation instructions. More specifically, the memory 200 can 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 memory, a flash, and the like.
[0173] The input device 230 can include devices that receive 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, and the like.
[0174] The output device 240 can include devices that allow information to be output to a user, such as a display screen, a printer, a speaker, and the like.
[0175] The communication interface 220 can include devices using any transceiver to communicate with other devices or communication networks, such as an Ethernet, a radio access network (RAN), a wireless local area network (WLAN), and the like.
[0176] The processor 210 executes programs stored in the memory 200 and calls other devices, which can be used to implement each step of any of the asynchronous-to-synchronous conversion methods provided by the above-described embodiments of the present application.
[0177] Exemplary computer program product and storage medium
[0178] In addition to the above methods and devices, the embodiments of the present application can also be computer program products that include computer program instructions that, when executed by a processor, cause the processor to perform the steps of the asynchronous-to-synchronous conversion methods according to various embodiments of the present application described in the above "Exemplary Methods" section of the present specification.
[0179] The computer program product can be written in any combination of one or more programming languages to perform the operations of the embodiments of the present application, including an object-oriented programming language, such as Java, C++, and the like, and a conventional procedural programming language, such as the "C" language or a similar programming language. The program code can be executed entirely on a user computing device, partially on a user device, as an independent software package, partially on a user computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0180] In addition, an embodiment of the present application can also be a storage medium having a computer program stored thereon, and the computer program is executed by a processor to implement the steps of the asynchronous-to-synchronous conversion method according to various embodiments of the present application described in the above “Exemplary Method” section of the specification. Specifically, the following steps can be implemented:
[0181] S101, in response to a first call of an asynchronous interface by a first thread, 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, the first thread being configured to execute a synchronous processing logic;
[0182] S102, when the asynchronous interface stores a first asynchronous processing result in a buffer storage, triggering a second thread to obtain the first asynchronous processing result in the buffer storage according to the first call identifier, the first asynchronous processing result including an asynchronous processing result of the asynchronous interface for the first call, the buffer storage being configured to store the first asynchronous processing result and the first call identifier in a mapping manner 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 each of the foregoing method embodiments, in order to simply describe, it is expressed as a combination of a series of actions, but those skilled in the art should know that the present application is not limited to the order of the actions described, because according to the present application, certain steps can be performed in other order or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification all belong to preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0185] It should be noted that each of the embodiments in the specification is described in a progressive manner, and each embodiment focuses on the difference from other embodiments, and the same and similar parts between the embodiments can be referred to each other. For device embodiments, since they are basically similar to method embodiments, they are described more simply, and the relevant parts are referred to the part of the method embodiment.
[0186] The steps in the method of each embodiment of the present application can be adjusted, combined and reduced in sequence 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 each embodiment of the present application can be combined, divided and reduced according to actual needs.
[0188] It should be understood that the disclosed terminal, device and method can be implemented in other ways. For example, the terminal embodiments described above are merely illustrative. For example, the division of modules or sub-modules is merely a logical function division. In actual implementation, another division manner can be used. For example, a plurality of sub-modules or modules can be combined or integrated into another module, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed modules can be indirect coupling or communication connection through some interfaces, devices or modules, and can be electrical, mechanical or other forms.
[0189] The modules or sub-modules described as separate components can or can not be physically separate, and the components of the modules or sub-modules can or can not be physical modules or sub-modules, i.e. can be located in one place or distributed on a plurality of network modules or sub-modules. Some or all of the modules or sub-modules can be selected according to actual needs to achieve the purpose of the embodiment.
[0190] In addition, the functional modules or sub-modules in each embodiment of the present application can be integrated into a processing module, or each module or sub-module can exist physically, or two or more modules or sub-modules can be integrated into one module. The integrated module or sub-module can be realized in the form of hardware or software functional module or sub-module.
[0191] The skilled person can further realize that the units and algorithm steps of the examples described in conjunction with the embodiments disclosed herein can be realized in electronic hardware, computer software or a combination of both. In order to clearly illustrate the interchangeability of hardware and software, the components and steps of the examples have been described in general terms in the above description. Whether the functions are realized in hardware or software depends on the specific application and design constraints of the technical solution. The skilled person can use different methods to realize the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.
[0192] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein can be directly implemented by hardware, software units executed by a processor, or a combination of both. The software units can 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 document, the term "only" is used simply to set off from one entity or action to another in order to avoid the use of the term "and / or" or the like for the sake of clarity. In no way should the term "only" be interpreted as implying that there is an implied exclusion of any referenced entity or action. Moreover, the terms "comprising", "including", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or apparatus. An element proceeded by "comprises... a" does not, without more constraints, exclude the existence of additional identical elements in the process, method, article, or apparatus that comprises the recited element.
[0194] The above description of disclosed embodiments provides enabling teaching for making or using the application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein can be applied to other embodiments without departing from the spirit or scope of the application. Thus, the present application is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. An asynchronous-to-synchronous method, characterized by, The method comprises: in response to a first call of an asynchronous interface by a first thread, 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, the first thread being used to execute synchronous processing logic; when the asynchronous interface stores a first asynchronous processing result to a buffer repository, triggering a second thread to obtain the first asynchronous processing result in the buffer repository according to the first call identifier, the first asynchronous processing result comprising an asynchronous processing result of the asynchronous interface for the first call, and the buffer repository being used to store the first asynchronous processing result and the first call identifier in a mapping manner 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 of claim 1, wherein, The method comprises: in response to the 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, which comprises: 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; waking up the first thread to continue execution.
3. The method of claim 2, wherein, The method is based on a Spring framework, and the first result object is a DeferredResult object; The method comprises: generating an empty DeferredResult object corresponding to the first call; The method comprises: in response to generating the empty DeferredResult object, the first thread enters a blocked state; The method comprises: in response to storing the first call result in the DeferredResult object, the first thread exits the blocked state to continue execution.
4. The method of claim 3, wherein, The method further comprises: when generating the empty 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 a timeout monitoring method of the DeferredResult object, and storing a preset first result in the DeferredResult object when a waiting time is greater than the first time threshold, the first result comprising 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 of claim 1, wherein, After the second thread obtains the first asynchronous processing result in the buffer repository according to the first call identifier, the method further comprises: The second thread post-processes 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 of claim 1, wherein, The buffer storage is a message queue, the asynchronous interface is configured to publish the generated asynchronous processing result and the calling identifier corresponding to the asynchronous processing result to the message queue, and the second thread is configured to subscribe to the message queue; The second thread acquires the first asynchronous processing result in the buffer storage according to the first calling identifier when the asynchronous interface stores the first asynchronous processing result in the buffer storage, including: The second thread acquires the asynchronous processing result and the calling identifier corresponding to the asynchronous processing result published by the asynchronous interface to the message queue by subscribing to the message queue; When it is determined that the calling identifier corresponding to the asynchronous processing result is the first calling identifier, the asynchronous processing result is determined as the first asynchronous processing result.
7. An asynchronous-to- synchronous device, comprising: including: The first unit is configured to generate a first calling identifier corresponding to a first call of an asynchronous interface in response to the first call, and block a first thread in response to a successful call of the first call, the first thread being configured to execute synchronous processing logic; The second unit is configured to acquire a first asynchronous processing result in a buffer storage according to the first calling identifier when the asynchronous interface stores the first asynchronous processing result in the buffer storage, the first asynchronous processing result including an asynchronous processing result of the asynchronous interface on the first call, and the buffer storage being configured to store the first asynchronous processing result and the first calling identifier in a mapping manner after the asynchronous interface generates the first asynchronous processing result; The third unit is configured to send the first asynchronous processing result to the first thread to wake up the first thread to continue execution.
8. An electronic device, comprising: including a memory and a processor; The memory is connected with the processor and is configured to store a program; The processor is configured to realize the asynchronous-to-synchronous method according to any one of claims 1-6 by running the program in the memory.
9. A storage medium, characterized by The storage medium has a computer program stored thereon, and the computer program realizes the asynchronous-to-synchronous method according to any one of claims 1-6 when being run by a processor.
10. A computer program product, characterised in that, The computer program instructions make the processor realize the asynchronous-to-synchronous method according to any one of claims 1-6 when being run by the processor.
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