A multi-threaded event production and consumption method, system, device and storage medium

By adopting a multi-threaded event production and consumption method in mobile program development, utilizing the isolation mechanism of the producer module and the consumer module, and combining with the Java observer mode, the problem of main thread blocking is solved, and business development efficiency and reliability are improved. The efficiency and thread safety of event processing are achieved.

CN114296963BActive Publication Date: 2024-09-10HUNAN HAPPLY SUNSHINE INTERACTIVE ENTERTAINMENT MEDIA CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111627551.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-28
Publication Date
2024-09-10
Estimated Expiration
2041-12-28

AI Technical Summary

Technical Problem

In mobile program development, burying, logging, and performance management operations are directly written to the main thread, causing the main thread to block and affecting business development efficiency.

Method used

Using a multi-threaded event production and consumption method, the producer module generates events to be consumed with priority in multiple event generation threads, and consumes them in a single event monitoring thread through the consumer module, using Java's own observer mode. Set up the observer module to consume the consumption events to avoid occupying the main thread.

Benefits of technology

It improves the efficiency of event processing, eliminates the coupling problem between parallel operations, ensures the reliability of business development and the efficiency of main thread operation, realizes the isolation of multi-thread production events and single-thread consumption events, and reduces main thread blocking.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114296963B
    Figure CN114296963B
Patent Text Reader

Abstract

The embodiment of the present invention provides a multi-threaded event production and consumption method, system, device and storage medium, wherein the method includes: the producer module generates a to-be-consumed event with a priority according to a preset event creation rule, and according to the preset event adding strategy, performs the operation of adding to the message queue one by one for the to-be-consumed event whose priority is not greater than the first preset threshold value, and when the consumer module detects that the operation of adding to the message queue is completed, it determines whether the number of to-be-consumed events in the message queue is equal to the first number, and if so, it traverses the first number of to-be-consumed events in the message queue, and performs consumption operations on the first number of to-be-consumed events one by one, and when the consumer module detects that the first number of to-be-consumed events in the message queue are all consumed, it clears the message queue, and monitors the cleared message queue to see whether the operation of adding to the message queue occurs. The present invention achieves the purpose of the invention of improving business development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of program development and design, and in particular to a multi-threaded event production and consumption method, system, device and storage medium. Background Art

[0002] In the process of mobile application development, it is inevitable to generate tracking points, log records, and performance tracking operations. The current way to achieve the above operations is to directly write the programs of the above operations into the main thread of the running business. Since there are running businesses in the main thread, when the above operations are performed, the main thread will be blocked, causing the running business to freeze, which in turn affects the development efficiency of normal business. Summary of the invention

[0003] The purpose of the embodiments of the present invention is to provide a multi-threaded event production and consumption method, system, device and storage medium to achieve the invention purpose of improving business development efficiency. The specific technical solution is as follows:

[0004] A multi-threaded event production and consumption method, the method comprising:

[0005] The producer module creates events with priorities according to preset event creation rules, and adds events to be consumed according to preset event adding strategies, and executes the operation of adding events to the message queue one by one for the events to be consumed whose priorities are not greater than the first preset threshold, wherein the producer module is deployed in multiple event generation threads.

[0006] When the consumer module detects that the operation of joining the message queue is completed, it determines whether the number of the events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue, and the consumer module is fixedly deployed in an event monitoring thread. The consumption operation specifically includes: sending an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the events to be consumed without occupying the event monitoring thread.

[0007] When the consumer module detects that the first number of events to be consumed in the message queue are all consumed, the consumer module clears the message queue and monitors the cleared message queue to see whether the operation of adding the message queue occurs.

[0008] Optionally, when the consumer module detects that the operation of adding the message queue is completed and determines that the number of the events to be consumed in the message queue is less than the first number, the method further includes:

[0009] The consumer module sends an add event instruction to the producer module, so that the producer module responds to the add event instruction and performs the join message queue operation.

[0010] Optionally, the method further includes:

[0011] After the consumer module sends the add event instruction to the producer module, it determines whether the monitoring time of the message queue is greater than a second preset threshold. If so, it traverses the second number of events to be consumed in the message queue, and performs the consumption operation on the second number of events to be consumed one by one, wherein the second number is less than the first number.

[0012] Optionally, the producer module generates a to-be-consumed event with a priority according to a preset event creation rule, and according to a preset event adding strategy, performs a message queue addition operation one by one for the to-be-consumed events whose priority is not greater than a first preset threshold, including:

[0013] The producer module generates a plurality of events to be consumed with priorities according to a preset event creation rule.

[0014] Determine whether the number of the events to be consumed in the message queue is less than the first number. If so, add the events to be consumed whose priorities are not greater than a first preset threshold to the message queue one by one.

[0015] Optionally, before determining whether the number of the events to be consumed in the message queue is less than the first number, the method further includes:

[0016] The producer module traverses the events to be consumed, determines the events to be consumed whose priority is greater than the first preset threshold as priority consumption events, and performs an immediate consumption operation on the priority consumption events, wherein the immediate consumption operation specifically includes: the producer module sends an immediate consumption request to the consumer module, so that the consumer module performs the consumption operation on the priority consumption event in the immediate consumption request.

[0017] Optionally, the determining whether the number of the events to be consumed in the message queue is less than the first number further includes:

[0018] When the producer module determines that the number of the events to be consumed in the message queue is not less than the first number, the immediate consumption operation is performed on the events to be consumed whose priority is not greater than a first preset threshold.

[0019] Optionally, the consumer module executes the consumption operation and sends an event consumption instruction to the observer module, so that the observer module consumes the event to be consumed in response to the event consumption instruction, including:

[0020] The consumer module executes the consumption operation and sends an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction, controls multiple observers in multiple threads in the observer module, and consumes the events to be consumed according to preset consumption rules, wherein each of the events to be consumed corresponds to multiple observers.

[0021] A multi-threaded event production and consumption system, the system comprising: a consumer module, a generator module and an observer module,

[0022] The producer module creates events with priorities according to preset event creation rules, and adds events to be consumed according to preset event adding strategies, and executes the operation of adding events to the message queue one by one for the events to be consumed whose priorities are not greater than the first preset threshold, wherein the producer module is deployed in multiple event generation threads.

[0023] When the consumer module detects that the operation of joining the message queue is completed, it determines whether the number of the events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue, and the consumer module is fixedly deployed in an event monitoring thread. The consumption operation specifically includes: sending an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the events to be consumed without occupying the event monitoring thread.

[0024] When the consumer module detects that the first number of events to be consumed in the message queue are all consumed, the consumer module clears the message queue and monitors the cleared message queue to see whether the operation of adding the message queue occurs.

[0025] Optionally, the consumer module is further configured to:

[0026] An add event instruction is sent to the producer module, so that the producer module performs the adding message queue operation in response to the add event instruction.

[0027] Optionally, the consumer module is further configured to:

[0028] After the consumer module sends the add event instruction to the producer module, it determines whether the monitoring time of the message queue is greater than a second preset threshold. If so, it traverses the second number of events to be consumed in the message queue, and performs the consumption operation on the second number of events to be consumed one by one, wherein the second number is less than the first number.

[0029] Optionally, the producer module is specifically configured as follows:

[0030] According to the preset event creation rule, a plurality of events to be consumed with priorities are generated.

[0031] Determine whether the number of the events to be consumed in the message queue is less than the first number. If so, add the events to be consumed whose priorities are not greater than a first preset threshold to the message queue one by one.

[0032] Optionally, the producer module is further configured to:

[0033] Traverse the events to be consumed, determine the events to be consumed whose priority is greater than the first preset threshold as priority consumption events, and perform immediate consumption operations on the priority consumption events, wherein the immediate consumption operation specifically includes: the producer module sends an immediate consumption request to the consumer module, so that the consumer module performs the consumption operation on the priority consumption event in the immediate consumption request.

[0034] Optionally, the producer module is further configured to:

[0035] When it is determined that the number of the events to be consumed in the message queue is not less than the first number, the immediate consumption operation is performed on the events to be consumed whose priority is not greater than a first preset threshold.

[0036] Optionally, the observer module is specifically configured as follows:

[0037] In response to the event consumption instruction, multiple observers in multiple threads in the observer module are controlled to consume the events to be consumed according to preset consumption rules, wherein each of the events to be consumed corresponds to multiple observers.

[0038] A multi-threaded event production and consumption device, the device comprising:

[0039] processor;

[0040] A memory for storing instructions executable by the processor.

[0041] The processor is configured to execute the instructions to implement the multi-threaded event production and consumption method as described in any one of the above items.

[0042] A computer storage medium, when the instructions in the computer-readable storage medium are executed by a processor of an electronic device, enables the device to execute any of the multi-threaded event production and consumption methods described above.

[0043] The embodiment of the present invention provides a multi-threaded event production and consumption method, system, device and storage medium. By introducing the observer mode that comes with Java, the observer module is set to consume the consumption event. While improving the processing efficiency of the event, the coupling problem between parallel operations is eliminated, so that the present invention improves the efficiency of business development and ensures the reliability of business development compared to the prior art in which a single consumer uniformly processes the event in the business development process. At the same time, the present invention isolates the event production and consumption process by setting the producer module and the consumer module, and realizes multi-threaded production of events and single-threaded consumption of events. While improving the event processing efficiency, the security of threads and data is guaranteed. Finally, the present invention deploys the producer module, the consumer module and the observer module in multiple sub-threads respectively, while improving the event generation efficiency, no longer occupying the main thread, reducing the degree of blocking the main thread, thereby ensuring the operation efficiency of the main thread while improving the efficiency of business development. It can be seen that the present invention achieves the invention purpose of improving the efficiency of business development.

[0044] Of course, it is not necessary to achieve all of the advantages described above at the same time to implement any product or method of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] In order to more clearly illustrate the embodiments of the present invention 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 some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0046] Figure 1 A flowchart of a multi-threaded event production and consumption method provided by an embodiment of the present invention;

[0047] Figure 2 A flowchart of a consumer module performing consumption operations provided by an optional embodiment of the present invention;

[0048] Figure 3 A flowchart of a producer module performing a message queue joining operation and an immediate consumption operation provided by an optional embodiment of the present invention;

[0049] Figure 4 A flowchart of an observer module consuming an event to be consumed provided by an optional embodiment of the present invention;

[0050] Figure 5 A block diagram of a multi-threaded event production and consumption system provided for an optional embodiment of the present invention;

[0051] Figure 6 A block diagram of a multi-threaded event production and consumption device provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0052] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0053] The embodiment of the present invention provides a multi-threaded event production and consumption method, such as Figure 1 As shown, the method includes:

[0054] S101. The producer module generates events to be consumed with priorities according to preset event creation rules, and adds strategies to the preset events to add to the message queue one by one for the events to be consumed whose priorities are not greater than the first preset threshold, wherein the producer module is deployed in multiple event generation threads.

[0055] Optionally, in an optional embodiment of the present invention, the above-mentioned producer module can be a module constructed based on the producer-consumer model, which generates events containing multiple data according to preset event creation rules. Among them, the preset event creation rules can be a collection of data added to the event by the developer according to the needs of the actual application scenario. The types of data include but are not limited to: event type and event priority. The present invention uses preset event creation rules to add event classification and event priority to the generated events, thereby achieving priority processing of important types of events or high-priority events, thereby improving the processing efficiency of important events.

[0056] Optionally, in another optional embodiment of the present invention, the producer module can be deployed in multiple event generation threads. The event generation thread can be a sub-thread different from the main thread. The present invention deploys the producer module in multiple event generation threads, while improving the event generation efficiency, no longer occupying the main thread, and reducing the degree of blocking the main thread. Thereby, while ensuring the operating efficiency of the main thread, the efficiency of business development is improved.

[0057] Optionally, in another optional embodiment of the present invention, the above-mentioned preset addition strategy can be a data synchronization strategy. The types of the above-mentioned synchronization strategies include, but are not limited to, full synchronization strategy, incremental synchronization strategy, new addition and change strategy, and special strategy, and are not limited in too much, subject to the realization of the technical effect of the present invention. The present invention ensures data consistency in the business development process and improves the reliability of business development by using data synchronization strategy.

[0058] S102. When the consumer module detects that the operation of joining the message queue is completed, it determines whether the number of events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue. The consumer module is fixedly deployed in an event monitoring thread, and the consumption operation specifically includes: sending an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the events to be consumed without occupying the event monitoring thread.

[0059] Optionally, in an optional embodiment of the present invention, the above-mentioned consumer module can be a module constructed based on the producer-consumer model and used to process the events to be consumed in the message queue. Because in the producer-consumer model, the producer module and the consumer module cannot communicate directly, and data interaction must be achieved through a buffer. Therefore, the present invention isolates the event production and consumption processes by setting up producer modules and consumer modules, and realizes multi-threaded production of events and single-threaded consumption of events. While improving the efficiency of event processing, the security of threads and data is guaranteed.

[0060] The present invention monitors the number of events to be consumed in the message queue by setting a consumer module, and when the number of events in the message queue reaches the maximum capacity, performs a one-time batch consumption operation on all the events to be consumed in the message queue, thereby realizing batch processing of concurrent events. Compared with the real-time input and output disk writing operation method of the prior art, the present invention improves the efficiency of events, thereby improving the efficiency of business development.

[0061] Optionally, in another optional embodiment of the present invention, the above-mentioned observer module can be a functional module built based on the observer pattern, and multiple observers set by the developer can be stored in the module. The above-mentioned multiple observers can be set in different threads and set by the developer to consume the events to be consumed of the target type or target priority. For an event to be consumed, multiple observers can be set for it, and each observer is set to perform only a single operation on the event to be consumed, implement the single responsibility principle, and decouple multiple observers that match the same event to be consumed. Among them, the construction method of the above-mentioned observer module includes but is not limited to: interface method, Java Development Kit (JDK) method and container (spring) method. Through the above-mentioned construction method, it is possible to implement the addition or deletion operations on the observers in the observer module.

[0062] The present invention consumes consumption events by setting an observer module, thereby improving the efficiency of event processing and eliminating the coupling problem between parallel operations. Compared with the prior art, in which a single consumer uniformly processes events during the business development process, the present invention improves the efficiency of business development and ensures the reliability of business development.

[0063] S103. When the consumer module detects that the first number of events to be consumed in the message queue are all consumed, the consumer module clears the message queue and monitors the cleared message queue to see whether an operation of adding to the message queue occurs.

[0064] The present invention introduces the observer mode that comes with Java, sets the observer module to consume the consumption events, and improves the processing efficiency of the events while eliminating the coupling problem between parallel operations, so that the present invention improves the efficiency of business development and ensures the reliability of business development compared to the prior art in which a single consumer uniformly processes the events in the business development process. At the same time, the present invention isolates the event production and consumption processes by setting the producer module and the consumer module, and realizes multi-threaded production events and single-threaded consumption events. While improving the event processing efficiency, the security of threads and data is guaranteed. Finally, the present invention deploys the producer module, the consumer module and the observer module in multiple sub-threads respectively, while improving the event generation efficiency, no longer occupies the main thread, reduces the degree of blocking the main thread, thereby ensuring the operation efficiency of the main thread and improving the efficiency of business development. It can be seen that the present invention achieves the invention purpose of improving the efficiency of business development.

[0065] Optionally, when the consumer module detects that the operation of adding to the message queue is completed, and determines that the number of events to be consumed in the message queue is less than the first number, the method further includes:

[0066] The consumer module sends an add event instruction to the producer module, so that the producer module responds to the add event instruction and performs an operation of adding to the message queue.

[0067] Optionally, in an optional embodiment of the present invention, the implementation method of the above-mentioned add event instruction can be based on a blocking queue. Specifically, the above-mentioned message queue can be set as a blocking queue. When the consumer model determines that the number of events to be consumed in the message queue is equal to the first number, the message queue will block the producer model from executing the operation of adding to the message queue. When the consumer model determines that the number of events to be consumed in the message queue is less than the first number, the message queue will block the consumer model's operation of traversing the message queue, and allow the producer model to execute the operation of adding to the message queue. By setting the above-mentioned message queue, the present invention blocks the data coupling between the producer module and the consumer module, thereby improving the reliability of event processing.

[0068] Optionally, the method further includes:

[0069] After the consumer module sends an add event instruction to the producer module, it determines whether the monitoring time of the message queue is greater than a second preset threshold. If so, it traverses the second number of events to be consumed in the message queue, and performs consumption operations on the second number of events to be consumed one by one, where the second number is less than the first number.

[0070] Optionally, in an optional embodiment of the present invention, the process of judging whether the monitoring duration of the information message queue is greater than the second preset threshold value can be implemented through a wait-notify mechanism. Specifically, in an optional embodiment of the present invention, by setting the maximum waiting time, that is, the second preset threshold value, when the monitoring duration is greater than the second preset threshold value, the consumer module is terminated from waiting for the producer module to complete the operation of joining the message queue, and the consumption operation is directly performed on the second number of events to be consumed in the current message queue. This avoids the occurrence of an infinite loop and improves the flexibility and reliability of the event processing process.

[0071] Optionally, in another optional embodiment of the present invention, when the consumer module determines that the monitoring time of the message queue is not greater than the second preset threshold, the consumer module may continue to monitor the message queue to see whether any operation of joining the message queue has been completed.

[0072] Optional, please combine Figure 2 An optional embodiment of the present invention is shown to understand the process of the consumer module performing the consumption operation:

[0073] Step S201: The consumer module detects that the operation of adding to the message queue is completed, triggering step S202.

[0074] Step S202, the consumer module determines whether the number of events to be consumed in the message queue is equal to the first number. If yes, step S203 is triggered, if no, step S204 is triggered.

[0075] Step S203: the consumer module performs consumption operations on the events to be consumed in the message queue one by one.

[0076] Step S204: The consumer module sends an add event instruction to the producer module, and triggers step S205.

[0077] Step S205, the consumer module determines whether the monitoring duration of the message queue is greater than a second preset threshold, if so, step S203 is triggered, if not, step S206 is triggered.

[0078] Step S206: continuously monitor the message queue to see if any message queue adding operation has been completed, and restart step S201.

[0079] It should be noted that this optional embodiment is only used to explain the process of the consumer module performing consumption operations. The specific implementation sequence should be determined according to the specific application scenario and is not subject to excessive restrictions.

[0080] Optionally, the producer module generates to-be-consumed events with priorities according to preset event creation rules, and adds strategies according to preset events to add to the message queue one by one for to-be-consumed events whose priorities are not greater than the first preset threshold, including:

[0081] The producer module creates event rules according to the presets and generates multiple events to be consumed with priorities.

[0082] Determine whether the number of events to be consumed in the message queue is less than the first number, and if so, add the events to be consumed whose priority is not greater than the first preset threshold to the message queue one by one. Optionally, before determining whether the number of events to be consumed in the message queue is less than the first number, the method further includes:

[0083] The producer module traverses the events to be consumed, determines the events to be consumed whose priority is greater than a first preset threshold as priority consumption events, and performs immediate consumption operations on the priority consumption events, wherein the immediate consumption operations specifically include: the producer module sends an immediate consumption request to the consumer module, so that the consumer module performs a consumption operation on the priority consumption event in the immediate consumption request.

[0084] Optionally, in an optional embodiment of the present invention, the present invention can avoid the risk of important events being overwritten or delayed due to message queue blocking by performing immediate consumption operations on priority consumption events having a priority level greater than a first preset threshold, thereby improving the reliability of data transmission during the event consumption process.

[0085] Optionally, determining whether the number of events to be consumed in the message queue is less than the first number further includes:

[0086] When the producer module determines that the number of events to be consumed in the message queue is not less than the first number, an immediate consumption operation is performed on the events to be consumed whose priority is not greater than the first preset threshold.

[0087] Optional, please combine Figure 3 An optional embodiment of the present invention shown in FIG. 1 is to understand the process of the producer module performing the message queue joining operation and the instant consumption operation:

[0088] The current producer module receives three pieces of data from three event generation threads. It now needs to generate three events to be consumed based on the three pieces of data and process the events to be consumed.

[0089] Step S301, according to the preset event creation rule, three events to be consumed with priorities are generated, and step S302 is triggered.

[0090] Optionally, for the convenience of description, the three events to be consumed are set as event A, event B and event C, and the priority of event A is greater than the first preset threshold, and the priorities of event B and event C are the same and both are less than the first preset threshold.

[0091] Step S302, traverse three events to be consumed, and determine one by one whether the priority of the event to be consumed is greater than the first preset threshold. If so, trigger step S303, otherwise trigger step S304.

[0092] Step S303: determine event A that is greater than the first preset threshold as a priority consumption event, and perform an immediate consumption operation.

[0093] Step S304, when the priorities of event B and event C are not greater than the first preset threshold, determine whether the number of events to be consumed in the current message queue is less than the first number, if so, trigger step S305, if not, trigger step S306.

[0094] Step S305: adding event B and event C to the message queue.

[0095] Step S306: Perform an immediate consumption operation on event B and event C.

[0096] It should be noted that this optional embodiment is only used to explain the process of the producer module executing the message queue joining operation and the instant consumption operation. The specific implementation sequence should be determined according to the specific application scenario and is not subject to excessive restrictions.

[0097] Optionally, the consumer module performs a consumption operation and sends an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the consumption event, including:

[0098] The consumer module executes the consumption operation and sends an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and controls multiple observers in multiple threads in the observer module to consume the consumption events according to the preset consumption rules, where each event to be consumed corresponds to multiple observers. Figure 4 An optional embodiment of the present invention is shown, and the process of consuming the consumption event by the above-mentioned observer module is understood:

[0099] Now we need to consume event A. It is known that in the observer module, observer 1 and observer 2 are observers corresponding to event A. The content of event A is to generate a notification file based on the data of event A.

[0100] Step S401: The observer module responds to the event consumption instruction and traverses the observers in the observer module, and triggers step S402.

[0101] In step S402, the observer module sends the event A to be consumed to observers 1 and 2 in the observer module, and triggers steps S403 and S404.

[0102] Step S403: Observer 1 generates a notification file according to the content of the event A to be consumed, and sends the notification file.

[0103] Step S404: Observer 2 generates a consumption log according to the content of the event A to be consumed, and sends the consumption log.

[0104] It should be noted that this optional embodiment is only used to explain the process of the observer module consuming the consumption event. The specific implementation sequence should be determined according to the specific application scenario and is not subject to excessive restrictions.

[0105] The present invention introduces the observer mode that comes with Java, sets the observer module to consume the consumption events, and improves the processing efficiency of the events while eliminating the coupling problem between parallel operations, so that the present invention improves the efficiency of business development and ensures the reliability of business development compared to the prior art in which a single consumer uniformly processes the events in the business development process. At the same time, the present invention isolates the event production and consumption processes by setting the producer module and the consumer module, and realizes multi-threaded production events and single-threaded consumption events. While improving the event processing efficiency, the security of threads and data is guaranteed. Finally, the present invention deploys the producer module, the consumer module and the observer module in multiple sub-threads respectively, while improving the event generation efficiency, no longer occupies the main thread, reduces the degree of blocking the main thread, thereby ensuring the operation efficiency of the main thread and improving the efficiency of business development. It can be seen that the present invention achieves the invention purpose of improving the efficiency of business development.

[0106] Corresponding to the embodiment of the multi-threaded event production and consumption method described above, the present invention also provides a multi-threaded event production and consumption system, such as Figure 5 The system includes: a consumer module 501, a generator module 502 and an observer module 503.

[0107] The producer module 502 generates events to be consumed with priorities according to preset event creation rules, and adds strategies to preset events to add to the message queue one by one for events to be consumed whose priorities are not greater than the first preset threshold, wherein the producer module 502 is deployed in multiple event generation threads.

[0108] When the consumer module 501 detects that the operation of joining the message queue is completed, it determines whether the number of events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue. The consumer module 502 is fixedly deployed in an event monitoring thread, and the consumption operation specifically includes: sending an event consumption instruction to the observer module 503, so that the observer module 503 responds to the event consumption instruction and consumes the events to be consumed without occupying the event monitoring thread.

[0109] When the consumer module 501 detects that the first number of events to be consumed in the message queue are all consumed, the consumer module 501 clears the message queue and monitors the cleared message queue to see whether an operation of adding to the message queue occurs.

[0110] Optionally, the consumer module 501 is further configured to:

[0111] An add event instruction is sent to the producer module 502, so that the producer module 502 performs an add message queue operation in response to the add event instruction.

[0112] Optionally, the consumer module 501 is further configured to:

[0113] After the consumer module sends an add event instruction to the producer module, it determines whether the monitoring time of the message queue is greater than a second preset threshold. If so, it traverses the second number of events to be consumed in the message queue, and performs consumption operations on the second number of events to be consumed one by one, where the second number is less than the first number.

[0114] Optionally, the producer module 502 is specifically configured as follows:

[0115] Create event rules according to the preset settings and generate multiple events with priorities to be consumed;

[0116] It is determined whether the number of events to be consumed in the message queue is less than the first number. If so, the events to be consumed whose priority is not greater than the first preset threshold are added to the message queue one by one.

[0117] Optionally, the producer module 502 is further configured to:

[0118] Traverse the events to be consumed, determine the events to be consumed whose priority is greater than a first preset threshold as priority consumption events, and perform immediate consumption operations on the priority consumption events, wherein the immediate consumption operation specifically includes: the producer module sends an immediate consumption request to the consumer module, so that the consumer module performs a consumption operation on the priority consumption event in the immediate consumption request.

[0119] Optionally, the producer module 502 is further configured to:

[0120] When it is determined that the number of events to be consumed in the message queue is not less than the first number, an immediate consumption operation is performed on the events to be consumed whose priorities are not greater than the first preset threshold.

[0121] Optionally, the observer module is specifically configured as follows:

[0122] In response to the event consumption instruction, multiple observers in multiple threads in the observer module are controlled to consume the consumption events according to preset consumption rules, wherein each event to be consumed corresponds to multiple observers.

[0123] The embodiment of the present invention provides a multi-threaded event production and consumption device, such as Figure 6 As shown, the device comprises:

[0124] Processor 601;

[0125] The memory 602 is used to store instructions executable by the processor 601 .

[0126] The processor 601 is configured to execute instructions to implement any of the multi-threaded event production and consumption methods described above.

[0127] An embodiment of the present invention provides a computer storage medium. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the device is enabled to execute any of the multi-threaded event production and consumption methods described above.

[0128] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip. The memory is an example of a computer-readable medium.

[0129] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0130] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0131] 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. It should also be noted that the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity 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, commodity or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0132] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system 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.

[0133] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.

Claims

1. A multi-threaded event production and consumption method, characterized in that: The method comprises: The producer module generates a to-be-consumed event with a priority according to a preset event creation rule, and according to a preset event adding strategy, performs a message queue addition operation one by one for the to-be-consumed event whose priority is not greater than a first preset threshold, wherein the producer module is deployed in multiple event generation threads; When the consumer module detects that the operation of adding the message queue is completed, it determines whether the number of the events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue, and the consumer module is fixedly deployed in an event monitoring thread. The consumption operation specifically includes: sending an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the event to be consumed without occupying the event monitoring thread; When the consumer module detects that the first number of events to be consumed in the message queue are all consumed, the consumer module clears the message queue and monitors the cleared message queue to see whether the operation of adding the message queue occurs.

2. The method according to claim 1, characterized in that: When the consumer module detects that the operation of adding the message queue is completed, and determines that the number of the events to be consumed in the message queue is less than the first number, the method further includes: The consumer module sends an add event instruction to the producer module, so that the producer module responds to the add event instruction and performs the join message queue operation.

3. The method according to claim 2, characterized in that The method further comprises: After the consumer module sends the add event instruction to the producer module, it determines whether the monitoring time of the message queue is greater than a second preset threshold. If so, it traverses the second number of events to be consumed in the message queue, and performs the consumption operation on the second number of events to be consumed one by one, wherein the second number is less than the first number.

4. The method according to claim 1, characterized in that The producer module generates a to-be-consumed event with a priority according to a preset event creation rule, and according to a preset event adding strategy, performs a message queue addition operation one by one for the to-be-consumed event whose priority is not greater than a first preset threshold, including: The producer module generates a plurality of events to be consumed with priorities according to a preset event creation rule; Determine whether the number of the events to be consumed in the message queue is less than the first number. If so, add the events to be consumed whose priorities are not greater than a first preset threshold to the message queue one by one.

5. The method according to claim 4, characterized in that Before determining whether the number of the events to be consumed in the message queue is less than the first number, the method further includes: The producer module traverses the events to be consumed, determines the events to be consumed whose priority is greater than the first preset threshold as priority consumption events, and performs an immediate consumption operation on the priority consumption events. The instant consumption operation specifically includes: the producer module sends an instant consumption request to the consumer module, so that the consumer module executes the consumption operation for the priority consumption event in the instant consumption request.

6. The method according to claim 5, characterized in that The determining whether the number of the events to be consumed in the message queue is less than the first number further includes: When the producer module determines that the number of the events to be consumed in the message queue is not less than the first number, the immediate consumption operation is performed on the events to be consumed whose priority is not greater than a first preset threshold.

7. The method according to claim 1, characterized in that The consumer module executes the consumption operation and sends an event consumption instruction to the observer module, so that the observer module consumes the event to be consumed in response to the event consumption instruction, including: The consumer module executes the consumption operation and sends an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction, controls multiple observers in multiple threads in the observer module, and consumes the events to be consumed according to preset consumption rules, wherein each of the events to be consumed corresponds to multiple observers.

8. A multi-threaded event production and consumption system, characterized in that: The system includes: a consumer module, a generator module and an observer module. The producer module generates a to-be-consumed event with a priority according to a preset event creation rule, and according to a preset event adding strategy, performs a message queue addition operation one by one for the to-be-consumed event whose priority is not greater than a first preset threshold, wherein the producer module is deployed in multiple event generation threads; When the consumer module detects that the operation of adding the message queue is completed, it determines whether the number of the events to be consumed in the message queue is equal to the first number. If so, it traverses the first number of events to be consumed in the message queue, and performs consumption operations on the first number of events to be consumed one by one, wherein the first number is the maximum capacity of the message queue, and the consumer module is fixedly deployed in an event monitoring thread. The consumption operation specifically includes: sending an event consumption instruction to the observer module, so that the observer module responds to the event consumption instruction and consumes the event to be consumed without occupying the event monitoring thread; When the consumer module detects that the first number of events to be consumed in the message queue are all consumed, the consumer module clears the message queue and monitors the cleared message queue to see whether the operation of adding the message queue occurs.

9. A multi-threaded event production and consumption device, characterized in that: The device comprises: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the multi-threaded event production and consumption method as described in any one of claims 1 to 7.

10. A computer storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the device is enabled to execute the multi-threaded event production and consumption method as described in any one of claims 1 to 7.