Event processing method and device
By introducing proxy threads into the application process to detect the main thread lag and send completion instructions, the problem of unresponsiveness of the application is solved, which significantly improves the user experience.
Patent Information
- Application Number
- CN202311376519.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-23
- Publication Date
- 2025-04-25
- Estimated Expiration
- 2043-10-23
AI Technical Summary
Applications may experience lag when processing input events, resulting in the application being unresponsive (ANR), affecting the user experience.
By introducing a proxy thread into the application process, it detects whether the main thread is stuck. If it is stuck, the proxy thread obtains the input event sent by the system process to the main thread and sends completion instructions to the system process to avoid ANR.
It effectively reduces the occurrence of application unresponsiveness, improves user experience, and avoids the system process controlling the application to generate ANR due to waiting for completion instructions.
Smart Images

Figure CN119883667A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to a field, and in particular to an event processing method and device. Background Art
[0002] With the development of computer technology, more and more applications have emerged. At present, when users use applications, there may be a phenomenon that the application is unresponsive. As an example, when a user uses an application installed on a terminal device, the user can trigger an operation through the terminal device, and accordingly, the terminal device can generate an input event in response to the user's triggering operation. If the application does not process the input event within a certain period of time, the application will be unresponsive (Application Not Responding, ANR).
[0003] The unresponsiveness of an application will affect the user experience of using the application. Therefore, a solution is urgently needed to solve this problem. Summary of the invention
[0004] In order to solve or at least partially solve the above technical problems, an embodiment of the present application provides an event processing method and device.
[0005] In a first aspect, an embodiment of the present application provides an event processing method, which is applied to an application process, wherein the application process includes a main thread and a proxy thread, and the method includes:
[0006] The proxy thread detects whether the main thread is stuck;
[0007] In response to detecting that the main thread is stuck, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
[0008] Optionally, in response to detecting that the main thread is stuck, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process, including:
[0009] In response to detecting that the main thread is stuck and the duration of the main thread being stuck is less than or equal to a preset duration, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process.
[0010] Optionally, the proxy thread detects whether the main thread is stuck, including:
[0011] The proxy thread sends a target task to the main thread;
[0012] The proxy thread periodically polls the completion status of the target task to determine whether the main thread is stuck.
[0013] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0014] For the i-th polling, if the completion status of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.
[0015] Optionally, the method further includes:
[0016] The time difference between the time corresponding to the i-th polling and the time when the target task is sent to the main thread is determined as the freeze duration of the main thread.
[0017] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0018] For the i-th polling, if the completion status of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.
[0019] Optionally, the period of polling the completion status of the target task is less than a timeout response threshold at which the system process generates no response from the application, and the preset duration is greater than the timeout response threshold.
[0020] In a second aspect, an embodiment of the present application provides an event processing device, which is applied to a proxy thread of an application process, and the device includes:
[0021] A detection unit, used to detect whether the main thread is stuck;
[0022] an acquisition unit, configured to acquire an input event sent by a system process to the main thread in response to detecting that the main thread is stuck;
[0023] The first sending unit is used to send completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
[0024] Optionally, the acquiring unit is used to:
[0025] In response to detecting that the main thread is stuck, and the duration of the main thread being stuck is less than or equal to a preset duration, obtaining an input event sent by a system process to the main thread;
[0026] The first sending unit is used to send completion indication information for the input event to the system process.
[0027] Optionally, the detection unit is used to:
[0028] Sending a target task to the main thread;
[0029] The completion status of the target task is polled periodically to determine whether the main thread is stuck.
[0030] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0031] For the i-th polling, if the completion status of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.
[0032] Optionally, the device further comprises:
[0033] The second sending unit is used to determine the time difference between the time corresponding to the i-th polling and the time of sending the target task to the main thread as the freeze duration of the main thread.
[0034] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0035] For the i-th polling, if the completion status of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.
[0036] Optionally, the period of polling the completion status of the target task is less than a timeout response threshold at which the system process generates no response from the application, and the preset duration is greater than the timeout response threshold.
[0037] In a third aspect, an embodiment of the present application provides an electronic device, the electronic device comprising a processor and a memory;
[0038] The processor is used to execute instructions stored in the memory so that the electronic device executes the method described in any one of the first aspects above.
[0039] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, comprising instructions, wherein the instructions instruct a device to execute any of the methods described in the first aspect above.
[0040] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when executed on a computer, enables the computer to execute any of the methods described in the first aspect above.
[0041] Compared with the prior art, the embodiments of the present application have the following advantages:
[0042] The embodiment of the present application provides an event processing method, which can be applied to an application process. The application process may include a main thread and a proxy thread. In the embodiment of the present application, considering that when the main thread is stuck, the input event sent by the system process to the main thread may not be processed in time, accordingly, the system process cannot receive the completion indication information of the input event when the response waiting time recorded for the input event exceeds the timeout response threshold, thereby causing the system process to control the application process to generate an ANR. Among them, the completion indication information is used to indicate that the input event has been processed. Therefore, in the embodiment of the present application, the proxy thread can detect whether the main thread is stuck, and when the main thread is detected to be stuck, the input event sent by the system process to the main thread is obtained, and the completion indication information for the input event is sent to the system process. That is: when the main thread cannot send the completion indication information of the input event to the system process in time, the proxy thread sends the completion indication information of the input event to the system process in advance, so that the system process can receive the completion indication information of the input event in time, thereby avoiding the system process controlling the application process to generate an ANR. Therefore, using this solution, ANR can be effectively reduced. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0044] Figure 1 A schematic diagram of a processing flow of an input event provided in an embodiment of the present application;
[0045] Figure 2 A flowchart of an event processing method provided in an embodiment of the present application;
[0046] Figure 3 A schematic diagram of a processing flow of an input event provided in an embodiment of the present application;
[0047] Figure 4 A flowchart of another event processing method provided in an embodiment of the present application;
[0048] Figure 5 A schematic diagram of the structure of an event processing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0049] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0050] The inventor of this application has found through research that when a user is using an application, the application may become unresponsive. As an example, when a user is using an application installed on a terminal device, the user may trigger an operation through the terminal device, and accordingly, the terminal device may generate an input event in response to the user's triggering operation. If the application does not process the input event within a certain period of time, an ANR will occur. Figure 1 To understand, Figure 1 A schematic diagram of a processing flow of an input event provided in an embodiment of the present application.
[0051] like Figure 1 As shown, the system service (system_server) process can generate an input event in response to a user's trigger operation, and send the input event to the main thread of the application process through the input event distribution (InputDispatcher) thread. The main thread of the application process processes the input event, and after the processing is completed, sends a completion indication message for the input event to the input event distribution thread. Among them, after the system service process sends the input event to the main thread, it will start the timing to record the response waiting time for the input event. If the timing reaches the timeout response threshold, and the input event distribution thread has not received the completion indication message for the above input event, the system service process can control the application process to generate an ANR. Among them, the system service process controls the application process to generate an ANR, which can be to control the application process to display a prompt message that the application cannot respond in the form of a pop-up window, or to kill the application process, which is not specifically limited in the embodiments of the present application.
[0052] The unresponsiveness of an application will affect the user experience of using the application. For example, displaying a prompt message that the application cannot respond in the form of a pop-up window will cause interference to the user. Killing the application process will prevent the user from continuing to use the application.
[0053] In order to solve the above problems, an embodiment of the present application provides an event processing method and device.
[0054] Various non-limiting implementations of the present application are described in detail below in conjunction with the accompanying drawings.
[0055] Exemplary Methods
[0056] See also Figure 2 , which is a flow chart of an event processing method provided in an embodiment of the present application. Figure 2 The method shown can be applied to an application process, and the application process can include a main thread and a proxy thread. In this embodiment, the method can include the following steps: S101-S102.
[0057] S101: The proxy thread detects whether the main thread is stuck.
[0058] In the embodiment of the present application, it is considered that when the main thread is stuck, the input event sent by the system process to the main thread may not be processed in time. Accordingly, when the response waiting time recorded by the system process for the input event exceeds the timeout response threshold, the completion indication information of the input event cannot be received, thereby causing the system process to control the application process to generate ANR. Therefore, the proxy thread can detect whether the main thread is stuck, and accordingly, determine further execution measures based on the detection result. Regarding the response waiting time, please refer to the above description of Figure 1 The relevant contents in the description will not be repeated here.
[0059] There are many ways to implement the proxy thread to detect whether the main thread is stuck. Next, two possible implementation methods are introduced.
[0060] In one example, the proxy thread may query the message cache queue of the main thread and determine whether the main thread is stuck according to the state of the message cache queue. For example, if the usage rate of the message cache queue is higher than a certain threshold, it is determined that the main thread is stuck; if the usage rate of the message cache queue is lower than or equal to the aforementioned threshold, it is determined that the main thread is not stuck. The message cache queue of the main thread is used to cache messages to be processed, and the aforementioned input event may be one of the message types to be processed by the main thread.
[0061] In another example, the proxy thread can send a target task to the main thread and periodically poll the completion status of the target task to determine whether the main thread is stuck. Specifically, the proxy thread can send the target task to the main thread at a certain period (for example, 2 seconds), and for each target task, the proxy thread can periodically poll the completion status of the target task to determine whether the main thread is stuck. In a specific example, the completion status of the target task can be periodically polled within a preset time period to determine whether the main thread is stuck.
[0062] The embodiment of the present application does not specifically limit the target task. The target task may be, for example, a state setting task of the main thread. The initial state of the main thread may be, for example, an error (false). If the target task is processed by the main thread, the state of the main thread is set to true (true).
[0063] The embodiments of the present application do not specifically limit the period for polling the completion status of the target task, which can be determined, for example, according to actual conditions. In one example, the period can be any value that is less than the timeout response threshold for the aforementioned system process to generate an unresponsive application, so that the main thread freeze can be determined in advance in a timely manner without generating ANR according to the traditional process. For example, if the aforementioned timeout response threshold is 5s, the period for polling the completion status of the target task can be 2s.
[0064] In one example, if the main thread is stuck, the main thread will not process the target task in time. Therefore, for the i-th polling of the completion status of the target task, if the completion status of the target thread indicates that the target task has not been completed, the proxy thread can determine that the main thread is stuck. Correspondingly, if the completion status of the target thread indicates that the target task has been completed, it can be determined that the main thread is not stuck.
[0065] S102: In response to detecting that the main thread is stuck, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
[0066] In the embodiment of the present application, since the main thread freeze may cause ANR, once the proxy thread detects that the main thread freezes, it can respond to the detection of the main thread freeze by obtaining the input event sent by the system process to the main thread, and further send completion indication information for the input event to the system process. After the system process receives the completion indication information for the input event, it can change the completion status of the input event without controlling the application process to generate ANR.
[0067] That is to say, in an embodiment of the present application, the proxy thread can obtain the input event sent by the system process to the main thread in advance when the main thread is stuck, and send the completion indication information of the input event to the system process in advance, so that the system process can receive the completion indication information of the input event in time, thereby avoiding the system process controlling the application process to generate ANR.
[0068] In one example, the proxy thread may obtain a socket file descriptor (socket fd) for communication between the main thread and the system process, and obtain an input event sent by the system process to the main thread through the socket file descriptor. The proxy thread may obtain the socket fd for communication between the main thread and the system process by hooking a function of the application process for receiving data.
[0069] In one example, after the proxy thread obtains the input event, it can extract the identifier of the input event. Accordingly, when sending the completion indication information of the input event to the system process, it can send the identifier of the input event and the completion indication information to the system process. In a specific example, the proxy thread can send the identifier of the input event and the completion indication information to the system process via the socket fd.
[0070] The system process mentioned here refers to the process of the operating system. The system process can correspond to Figure 1 In the system service process shown, the system process can send the input event to the main thread of the application process through the aforementioned input event distribution thread. Correspondingly, the proxy thread can send the completion indication information of the input event to the input event distribution thread of the system process.
[0071] In one example, considering that the main thread is stuck for too long, it means that the event processing capability of the main thread is low, and accordingly, the possibility that the application can run normally is also low. In this case, it is reasonable to generate ANR, and in this case, the traditional ANR generation mechanism should not be blocked. When the main thread is not stuck for a long time, it means that the event processing capability of the main thread may be restored. In this case, if ANR is generated, it will be detrimental to the user's continued use of the application. Therefore, in one implementation, the proxy thread can obtain the input event sent by the system process to the main thread when it detects that the main thread is stuck and the duration of the main thread being stuck is less than or equal to the preset duration, and send completion indication information for the input event to the system process. In this way, ANR can be avoided when the main thread is not stuck for a long time, which is conducive to the user continuing to use the application.
[0072] In one example, if the proxy thread determines whether the main thread is stuck by periodically polling the completion status of the target task, then if the main thread is stuck as determined by the i-th polling, the proxy thread may also determine the time difference between the time corresponding to the i-th polling and the time when the target task is sent to the main thread as the stuck duration of the main thread. Furthermore, the proxy thread may determine whether the stuck duration is less than a preset duration.
[0073] The embodiments of the present application do not specifically limit the preset polling duration, and the preset duration can be determined according to actual conditions. In one example, the preset duration can be any value greater than the timeout response threshold of the aforementioned system process that generates an unresponsive application, thereby allowing the main thread to freeze for a preset duration without generating an ANR. For example, if the aforementioned timeout response threshold is 5s, the preset duration can be 8s.
[0074] The above introduces the event processing method provided in the embodiment of the present application. Next, the event processing method is introduced in combination with specific examples.
[0075] See also Figure 3 , which is a schematic diagram of a processing flow of an input event provided by an embodiment of the present application. Figure 3 As shown, the application process includes a main thread and a proxy thread, and the interaction process between the main thread and the system process is the same as Figure 1 The proxy thread is used to send completion indication information of the input event to the system process in advance when detecting that the main thread is stuck.
[0076] In a specific example, the steps performed by the proxy thread can be as follows Figure 4 As shown, Figure 4 A flowchart of another event processing method provided in an embodiment of the present application.
[0077] like Figure 4 As shown, the proxy thread may execute the following S201-S205.
[0078] S201: Sending a target task to a main thread, recording a time at which the target task is sent to the main thread.
[0079] S202: Delay the period of polling the completion status of the target task.
[0080] In a specific implementation, S202 may be delayed for 2 seconds, for example.
[0081] S203: Query whether the completion status of the target task is completed.
[0082] If the completion status of the target task is completed, then continue to execute S201 ; if the completion status of the target task is not completed, then continue to execute S204 .
[0083] S204: Determine whether the difference between the current time and the aforementioned sending time is greater than a preset duration.
[0084] If the difference is greater than the preset time length, execute S202; if the difference is less than or equal to the preset time length, execute S205.
[0085] The difference mentioned here can correspond to the freeze duration mentioned above.
[0086] S205: Acquire an input event sent by the system process to the main thread, and send completion indication information for the input event to the system process.
[0087] After executing S205, continue to execute S202.
[0088] Exemplary Devices
[0089] Based on the method provided in the above embodiment, the embodiment of the present application further provides a device, which is described below in conjunction with the accompanying drawings.
[0090] See also Figure 5 , which is a structural diagram of an event processing device provided in an embodiment of the present application. Figure 5 The event processing device 500 shown can be applied to an application process, and the application process includes a main thread and a proxy thread. Specifically, the device 500 can be applied to the proxy thread, and the device 500 includes: a detection unit 501, an acquisition unit 502 and a first sending unit 503.
[0091] A detection unit 501 is used to detect whether the main thread is stuck;
[0092] An acquisition unit 502 is configured to acquire an input event sent by a system process to the main thread in response to detecting that the main thread is stuck;
[0093] The first sending unit 503 is configured to send completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
[0094] Optionally, the acquiring unit 502 is configured to:
[0095] In response to detecting that the main thread is stuck, and the duration of the main thread being stuck is less than or equal to a preset duration, obtaining an input event sent by a system process to the main thread;
[0096] The first sending unit 503 is used to send completion indication information for the input event to the system process.
[0097] Optionally, the detection unit 501 is used to:
[0098] Sending a target task to the main thread;
[0099] The completion status of the target task is polled periodically to determine whether the main thread is stuck.
[0100] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0101] For the i-th polling, if the completion status of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.
[0102] Optionally, the device further comprises:
[0103] The second sending unit is used to determine the time difference between the time corresponding to the i-th polling and the time of sending the target task to the main thread as the freeze duration of the main thread.
[0104] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck includes:
[0105] For the i-th polling, if the completion status of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.
[0106] Optionally, the period of polling the completion status of the target task is less than a timeout response threshold at which the system process generates no response from the application, and the preset duration is greater than the timeout response threshold.
[0107] Since the device 500 is a device corresponding to the method provided in the above method embodiment, the specific implementation of each unit of the device 500 is based on the same concept as the above method embodiment. Therefore, for the specific implementation of each unit of the device 500, reference can be made to the description part of the above method embodiment, which will not be repeated here.
[0108] The embodiment of the present application also provides an electronic device, the electronic device comprising a processor and a memory;
[0109] The processor is used to execute instructions stored in the memory so that the electronic device executes the event processing method provided by the above method embodiment.
[0110] An embodiment of the present application provides a computer-readable storage medium, including instructions, wherein the instructions instruct a device to execute the event processing method provided by the above method embodiment.
[0111] An embodiment of the present application provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute the event processing method provided by the above method embodiment.
[0112] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.
[0113] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
[0114] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the protection scope of the present application.
Claims
1. An event processing method, characterized in that: Applied to an application process, the application process includes a main thread and a proxy thread, and the method includes: The proxy thread detects whether the main thread is stuck; In response to detecting that the main thread is stuck, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
2. The method according to claim 1, characterized in that In response to detecting that the main thread is stuck, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process, including: In response to detecting that the main thread is stuck and the duration of the main thread being stuck is less than or equal to a preset duration, the proxy thread obtains an input event sent by the system process to the main thread, and sends completion indication information for the input event to the system process.
3. The method according to claim 2, characterized in that The proxy thread detects whether the main thread is stuck, including: The proxy thread sends a target task to the main thread; The proxy thread periodically polls the completion status of the target task to determine whether the main thread is stuck.
4. The method according to claim 3, characterized in that The periodically polling the completion status of the target task to determine whether the main thread is stuck includes: For the i-th polling, if the completion status of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.
5. The method according to claim 4, characterized in that The method further comprises: The time difference between the time corresponding to the i-th polling and the time when the target task is sent to the main thread is determined as the freeze duration of the main thread.
6. The method according to claim 3, characterized in that The periodically polling the completion status of the target task to determine whether the main thread is stuck includes: For the i-th polling, if the completion status of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.
7. The method according to any one of claims 3 to 6, characterized in that: The period of polling the completion status of the target task is less than a timeout response threshold at which the system process generates no response from the application program, and the preset duration is greater than the timeout response threshold.
8. An event processing device, characterized in that: A proxy thread applied to an application process, the device comprising: A detection unit, used to detect whether the main thread is stuck; an acquisition unit, configured to acquire an input event sent by a system process to the main thread in response to detecting that the main thread is stuck; The first sending unit is used to send completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.
9. An electronic device, characterized in that: The electronic device comprises a processor and a memory; The processor is configured to execute instructions stored in the memory, so that the electronic device executes the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The method comprises instructions for instructing a device to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Processing device and method for preventing application program from being unresponsive
CN106155741A
Application program lag event positioning method and device, storage medium and equipment
CN113297052A
Method for realizing high availability of Redis cluster and proxy middleware
CN114785713A