Coroutine running method and device, storage medium and electronic equipment
By listening to the lifecycle changes of the creation entity and utilizing proxy classes and a set of pre-defined mapping relationships, the memory leak problem caused by the coroutine scope being out of the lifecycle is solved, enabling coroutine execution controlled by the lifecycle, reducing memory leaks, and improving management efficiency.
Patent Information
- Application Number
- CN202111470856.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-03
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2041-12-03
AI Technical Summary
In existing technologies, the scope of a coroutine is detached from the lifecycle of the entity that created it, leading to memory leaks.
By listening to changes in the lifecycle of the created entity, and using proxy classes and a set of pre-defined mapping relationships, the system can map the lifecycle events of coroutines to their response events, and automatically cancel coroutines to avoid memory leaks.
It enables coroutine execution controlled by its lifecycle, reduces memory leaks, and improves the management efficiency and security of coroutines.
Smart Images

Figure CN114168284B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of electronic equipment technology, and in particular to a method, apparatus, storage medium and electronic equipment for coroutine operation. Background Technology
[0002] With the rapid development of processing and storage capabilities of electronic devices, a vast number of applications have been rapidly disseminated and used, improving users' quality of life and entertainment experience.
[0003] The basic unit of execution for an application in the memory environment is a process. A process can have multiple threads, and a thread can have multiple coroutines. In related technologies, coroutines can be created by establishing a coroutine scope and executing the corresponding coroutine body within that scope. However, coroutine scopes created in this way are detached from the lifecycle of the creating entity, which can easily lead to memory leaks. For example, when a coroutine contains many time-consuming operations, the pointer to the creating entity held within the coroutine will remain referenced, preventing the creating entity from being released and causing a memory leak. Summary of the Invention
[0004] This application provides a method, apparatus, storage medium, and electronic device for running coroutines, which can realize coroutines with lifecycle control and reduce memory leaks.
[0005] In a first aspect, embodiments of this application provide a method for running coroutines, including:
[0006] During the coroutine's execution, the lifecycle changes of the creating entity are monitored;
[0007] Determine the current lifecycle event based on the monitoring results;
[0008] Perform corresponding operations based on the lifecycle events and the preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events.
[0009] Secondly, embodiments of this application provide a coroutine operation device, comprising:
[0010] The listening unit is used to monitor changes in the lifecycle of the created entity during the execution of the coroutine;
[0011] The determination unit is used to determine the current lifecycle event based on the monitoring results.
[0012] The processing unit is configured to perform corresponding operations based on the lifecycle events and a preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events.
[0013] In one embodiment, the apparatus further includes:
[0014] The judgment unit is used to determine whether the lifecycle event is a destruction event before performing the corresponding operation based on the lifecycle event and the preset mapping relationship set;
[0015] The destruction unit is used to perform a destruction operation on the coroutine when the determination unit determines that it is true.
[0016] In one embodiment, the processing unit is specifically used for:
[0017] Determine whether there exists a target sample lifecycle event that matches the lifecycle event in the preset mapping relationship set;
[0018] If it exists, then determine the target sample response event corresponding to the target sample lifecycle event based on the correspondence;
[0019] Perform the operation corresponding to the target sample response event.
[0020] In one embodiment, the processing unit is further configured to:
[0021] If there is no target sample lifecycle event matching the lifecycle event in the preset mapping relationship set, the original running logic of the coroutine is maintained.
[0022] In one embodiment, the monitoring unit is specifically used for:
[0023] The lifecycle changes of the created entity are monitored through a proxy class, wherein the proxy class is obtained by extending the Job class of the coroutine through a target extension function, and the target extension function can monitor the lifecycle changes of the created entity.
[0024] In some embodiments, the apparatus further includes:
[0025] A creation unit is used to create a coroutine scope and create the coroutine within the coroutine scope before listening to changes in the lifecycle of the creation subject.
[0026] The acquisition unit is used to acquire the Job class returned by the coroutine execution;
[0027] An extension unit is used to extend the Job class with methods through a target extension function to generate a proxy class.
[0028] In one embodiment, the preset mapping relationship set is stored within the coroutine's coroutine scope.
[0029] Thirdly, embodiments of this application also provide a computer-readable storage medium storing a plurality of instructions adapted for loading by a processor to execute the above-described coroutine execution method.
[0030] Fourthly, embodiments of this application also provide an electronic device, including a processor and a memory, wherein the processor is electrically connected to the memory, the memory is used to store instructions and data, and the processor is used to execute the above-described coroutine execution method.
[0031] This application implements a method that monitors lifecycle changes of the creating entity during coroutine execution and determines the current lifecycle event based on the monitoring results. It then executes corresponding operations based on the lifecycle event and a preset mapping relationship set, where the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events. This solution enables coroutines controlled by their lifecycles, reducing memory leaks. Attached Figure Description
[0032] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0033] Figure 1 This is a flowchart illustrating the coroutine execution method provided in an embodiment of this application.
[0034] Figure 2 This is another flowchart illustrating the coroutine execution method provided in the embodiments of this application.
[0035] Figure 3 This is a schematic diagram of the structure of the coroutine operation device provided in the embodiments of this application.
[0036] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0037] Figure 5 This is another structural schematic diagram of the electronic device provided in the embodiments of this application. Detailed Implementation
[0038] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0039] There are many ways to create a Kotlin coroutine on Android. For example, you can create a coroutine scope using `GlobalScope.launch()` or `CoroutineScope(Context).launch()` and execute the corresponding coroutine body within that scope. However, coroutines created in this way are essentially detached from the lifecycle of their creator. For instance, if a coroutine is created within an Activity, it will by default hold the Activity's `this` pointer. If the coroutine contains many time-consuming operations, this pointer will remain referenced, preventing the Activity from being released and causing a memory leak. Furthermore, because the coroutine's lifecycle is isolated from the creator's, it cannot respond to changes in the lifecycle.
[0040] Based on this, embodiments of this application provide a method, apparatus, storage medium, and electronic device for coroutine operation. These will be described in detail below.
[0041] In one embodiment, a coroutine execution method is provided, applicable to electronic devices such as smartphones, tablets, and laptops. (Reference) Figure 1 The specific process of this coroutine execution method can be as follows:
[0042] 101. During the operation of a coroutine, monitor the lifecycle changes of the creating entity.
[0043] Coroutines are a more lightweight concept than threads. Just as a process can have multiple threads, a thread can have multiple coroutines. A coroutine is neither a process nor a thread, but a special function that can be suspended at a certain point and resumed execution from that point. Coroutines are not managed by the operating system kernel but are entirely controlled by the program, meaning they execute in user mode. Coroutine switching involves hardware context, with the switched memory stored in user-defined variables (user stack or heap). The coroutine switching process only occurs in user mode (i.e., without entering kernel mode), thus avoiding the resource consumption of thread switching. Therefore, it is highly efficient, resulting in a significant performance improvement.
[0044] In this embodiment, the creation entity is the carrier for creating coroutines, such as Activity.
[0045] In one implementation, monitoring the lifecycle changes of the creation entity can be achieved through a proxy class. This proxy class is obtained by extending the Job class of the coroutine using a target extension function, which can then monitor the lifecycle changes of the creation entity. The Job class is the handle of the coroutine, representing the return value of a standard coroutine production function; it acts as a switch, controlling the start or cancellation of the coroutine. In practice, the extension function doesn't actually modify the original Job class; instead, it uses static imports to place a series of related functions in a newly created file.
[0046] In this embodiment, the target extension function can be a Kotlin extension function. The proxy class uses the Job class generated during the coroutine operation and the Kotlin extension function to execute the proxy strategy after the Job class is created. It should be noted that extension functions are syntactic sugar for Kotlin. Since what needs to be changed is the Kotlin code, the Job class methods can be extended through extensions, and the proxy strategy can be executed in the extended methods.
[0047] A proxy coroutine (the coroutine corresponding to the proxy class) essentially designates a parent coroutine for the currently generated coroutine, and the proxy also provides proxy events to extend the functionality of the generated coroutine. Through the proxy mechanism, the proxy coroutine uses the Job class handle produced by the proxy coroutine to control the generated coroutine during its creation process. That is, in one implementation, before listening to changes in the lifecycle of the creation entity, the following process may also be included:
[0048] Create a coroutine scope, and create coroutines within that scope;
[0049] Get the Job class returned by the coroutine execution;
[0050] The Job class is extended with target extension functions to generate proxy classes.
[0051] The proxy class implements listeners related to the lifecycle of the creation entity, enabling an automatic cancellation mechanism. The proxy class is a proxy coroutine handle for the Job class. Specifically, a method can be extended to the Job class through a target extension function, and the extended method (i.e., the target extension function) can be called externally to execute the proxy strategy and generate the proxy class.
[0052] 102. Determine the current lifecycle event based on the monitoring results.
[0053] The lifecycle events are standard lifecycle events in the Android system, and from top to bottom, they include: the creation phase (on_Create), which indicates the creation of the page (Activity) and the completion of initialization; the start phase (on_Start), which indicates that the page (Activity) is being started in the background; the visible phase (on_Resume), which indicates that the page (Activity) appears in the foreground; the stopped but visible phase (on_Pause), which indicates that the page (Activity) is stopping; the invisible stopped phase (on_Stop), which indicates that the page (Activity) is about to stop; the destruction phase (on_Destroy), which indicates that the page (Activity) is about to be destroyed; and the restart phase (on_Restart), which indicates that the page (Activity) is restarted.
[0054] Specifically, when a change in the lifecycle of an external creation entity is detected, the lifecycle event of the current creation entity is determined. For example, when the lifecycle of the creation entity changes from the generation phase to the start phase, the current lifecycle event can be determined as on_Start; when the lifecycle of the creation entity changes from the start phase to the visible phase, the current lifecycle event can be determined as on_Resume.
[0055] 103. Perform corresponding operations based on the lifecycle events and the preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events.
[0056] In this context, a response event can essentially be a function, and users can customize and use it according to their actual needs.
[0057] In this embodiment, the preset mapping set is stored within the coroutine's scope. In practical applications, this preset mapping set can be represented as a map collection of key-value pairs. For example, in the map collection, the key represents a lifecycle event, and the value represents a response event. Internally, the coroutine uses the map collection to store lifecycle events and their corresponding function operations (i.e., response events), thus realizing a coroutine controlled by its lifecycle. When the external lifecycle changes, the key is checked, the corresponding value is retrieved, and the value function is executed.
[0058] For example, if the requirement for responding to an event is to print a log message when the lifecycle is in the `on_create` (generation phase), then the key in the map collection would be `on_Create`, and the corresponding value would be the function that prints the log message. When the current lifecycle event is `on_Create`, the corresponding value function is retrieved from the map collection based on the mapping relationship, and the log message printing operation is executed. That is, in one implementation, when performing the corresponding operation based on the lifecycle event and the preset mapping relationship set, the following process may be included:
[0059] Determine whether there exists a target sample lifecycle event that matches the lifecycle event in the preset mapping relationship set;
[0060] If it exists, then determine the target sample response event corresponding to the target sample lifecycle event based on the correspondence;
[0061] Execute the operation corresponding to the target sample response event.
[0062] In addition, in some implementations, if there is no target sample lifecycle event in the preset mapping relationship set that matches the monitored lifecycle event, the original running logic of the coroutine is maintained.
[0063] In practical applications, adding events within `On_Destroy` is uncontrollable and can easily affect external systems, causing memory leaks. Therefore, in this embodiment, the sample lifecycle events stored in the preset mapping relationship set include: `on_Create`, `on_Start`, `on_Resume`, `on_Pause`, and `on_Stop`. When the creation entity's lifecycle is in the `On_Destroy` phase, it should be directly destroyed without choosing to expose custom processing. That is, in one embodiment, before performing the corresponding operations based on the lifecycle events and the preset mapping relationship set, the following process may also be included:
[0064] Determine whether a lifecycle event is a destruction event;
[0065] If so, then perform a destruction operation on that coroutine.
[0066] As shown above, the coroutine execution method provided in this embodiment uses Kotlin extension functions to execute the proxy step after the Job class is created. The proxy class implements listening for lifecycle-related events of the creation entity, achieving an automatic cancellation mechanism for the coroutine. Internally, the coroutine uses a map collection to store lifecycle events and their corresponding function operations, realizing a coroutine controlled by its lifecycle. When the lifecycle of the external creation entity changes, the corresponding response events can be implemented through the proxy class's map collection, avoiding memory leaks.
[0067] In another embodiment, a coroutine creation method supporting lifecycle management is also provided. The following will describe this coroutine creation method in detail using the Android system as an example.
[0068] refer to Figure 2 In this embodiment, a coroutine scope can be created using the Kotlin coroutine library, and coroutines can be created within it to obtain the returned Job class. Then, a proxy strategy is executed by calling the extension function of the returned Job class to generate a proxy class. The proxy class takes over the events owned by the coroutine itself, as well as the extended lifecycle-aware events.
[0069] In this embodiment, the Job class can be proxied using Kotlin's proxy-by mechanism. Since the proxy class takes over the operation of the original coroutine, when the lifecycle of the external creation entity changes, the proxy class can obtain the lifecycle change listener event, thus implementing a lifecycle event observer. A map collection (of type Map) is stored in memory.<Lifecycle.Event,()-> The `Unit>` variable, where the key is a lifecycle event and the value is the function to be executed, storing the caller's execution operations. During coroutine execution, the following situations may occur:
[0070] If the event is an onDestroy event, the cancel() function of the proxied class is actively called in the listener callback to perform the destruction of the coroutine, thus avoiding memory leaks.
[0071] If the event is another type of event, the listener callback checks if the key in the proxy class's map collection exists for an event corresponding to the current lifecycle. If an event corresponding to the current lifecycle exists, the function in the value is executed; otherwise, the original logic of the coroutine (non-proxy coroutine) is executed.
[0072] As shown above, the common coroutine method provided in this embodiment executes the proxy step after the Job class is created using Kotlin extension functions. The proxy class implements lifecycle-related listening, achieving an automatic cancellation mechanism for the coroutine. Internally, a map collection stores lifecycle events and their corresponding function operations, ensuring the coroutine is lifecycle-controlled. When the lifecycle of the external creation entity changes, the proxy class's map collection can be used to map and respond to corresponding events, avoiding memory leaks.
[0073] In another embodiment of this application, a coroutine execution device is also provided. This coroutine execution device can be integrated into an electronic device in the form of software or hardware, specifically including devices such as mobile phones, tablets, and laptops. Figure 3 As shown, the coroutine execution device 300 may include: a monitoring unit 301, a determination unit 302, and a processing unit 303, wherein:
[0074] The listening unit 301 is used to listen for changes in the lifecycle of the created entity during the operation of the coroutine;
[0075] Determining unit 302 is used to determine the current lifecycle event based on the listening results;
[0076] The processing unit 303 is used to perform corresponding operations based on the lifecycle event and the preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events.
[0077] In one embodiment, the coroutine operation device 300 further includes:
[0078] The judgment unit is used to determine whether the lifecycle event is a destruction event before performing the corresponding operation based on the lifecycle event and the preset mapping relationship set;
[0079] The destruction unit is specifically used to perform a destruction operation on the coroutine when the judgment unit determines that it is true.
[0080] In one embodiment, the processing unit 303 is specifically used for:
[0081] Determine whether there exists a target sample lifecycle event that matches the lifecycle event within the preset mapping relationship set;
[0082] If such a correspondence exists, the target sample response event corresponding to the target sample lifecycle event is determined based on this correspondence.
[0083] Execute the operation corresponding to the response event of the target sample.
[0084] In one embodiment, the processing unit 303 is further configured to:
[0085] If there is no target sample lifecycle event matching the lifecycle event in the preset mapping relationship set, the original running logic of the coroutine will be maintained.
[0086] In one embodiment, the listening unit 301 is specifically used for:
[0087] The lifecycle changes of the created entity are monitored through a proxy class. This proxy class is obtained by extending the Job class of the coroutine through a target extension function, which can monitor the lifecycle changes of the created entity.
[0088] In some embodiments, the coroutine operation device 300 further includes:
[0089] The creation unit is used to create a coroutine scope before listening to changes in the lifecycle of the creation subject, and to create the coroutine within that coroutine scope.
[0090] The retrieval unit is used to retrieve the Job class returned by the execution of this coroutine;
[0091] An extension unit is used to extend the Job class with methods through a target extension function to generate a proxy class.
[0092] In one implementation, the preset mapping set is stored within the coroutine's scope.
[0093] As can be seen from the above, the coroutine execution device provided in this application monitors the lifecycle changes of the creating entity during coroutine execution and determines the current lifecycle event based on the monitoring results; it then performs corresponding operations according to the lifecycle event and a preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events. This solution can realize coroutines controlled by the lifecycle, reducing memory leaks.
[0094] In another embodiment of this application, an electronic device is also provided, which may be a smartphone, tablet computer, or other smart terminal. Figure 4 As shown, the electronic device 400 includes a processor 401 and a memory 402. The processor 401 and the memory 402 are electrically connected.
[0095] The processor 401 is the control center of the electronic device 400. It connects various parts of the electronic device through various interfaces and lines. By running or loading applications stored in the memory 402 and calling data stored in the memory 402, it performs various functions of the electronic device and processes data, thereby monitoring the electronic device as a whole.
[0096] In this embodiment, the processor 401 in the electronic device 400 loads the instructions corresponding to the processes of one or more applications into the memory 402 according to the following steps, and the processor 401 runs the applications stored in the memory 402 to realize various functions:
[0097] During the coroutine's execution, the lifecycle changes of the creating entity are monitored;
[0098] Determine the current lifecycle event based on the monitoring results;
[0099] Perform corresponding operations based on the lifecycle event and the preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events.
[0100] In one embodiment, before performing the corresponding operation based on the lifecycle event and the preset mapping relationship set, the processor 401 may perform the following operations:
[0101] Determine whether the lifecycle event is a destruction event;
[0102] When the judgment unit determines that it is yes, the destruction operation for the coroutine is executed.
[0103] In one embodiment, when performing corresponding operations based on the lifecycle event and the preset mapping relationship set, the processor 401 may perform the following operations:
[0104] Determine whether there exists a target sample lifecycle event that matches the lifecycle event within the preset mapping relationship set;
[0105] If such a correspondence exists, the target sample response event corresponding to the target sample lifecycle event is determined based on this correspondence.
[0106] Execute the operation corresponding to the response event of the target sample.
[0107] In one embodiment, if there is no target sample lifecycle event matching the lifecycle event in the preset mapping relationship set, the processor 401 may also perform the following operations:
[0108] Maintain the original operating logic of the coroutine.
[0109] In one implementation, when listening for changes in the lifecycle of the created entity, the processor 401 may perform the following operations:
[0110] The lifecycle changes of the created entity are monitored through a proxy class. This proxy class is obtained by extending the Job class of the coroutine through a target extension function, which can monitor the lifecycle changes of the created entity.
[0111] In one implementation, before listening for changes in the lifecycle of the created entity, the processor 401 may also perform the following operations:
[0112] Create a coroutine scope, and create the coroutine within that scope;
[0113] Obtain the Job class returned by the coroutine execution;
[0114] The Job class is extended using the target extension function to generate a proxy class.
[0115] In one implementation, the preset mapping set is stored within the coroutine's scope.
[0116] Memory 402 can be used to store applications and data. The applications stored in memory 402 contain instructions that can be executed by the processor. Applications can be composed of various functional modules. Processor 401 executes various functional applications and coroutines by running the applications stored in memory 402.
[0117] In some embodiments, such as Figure 5 As shown, the electronic device 400 also includes: a display screen 403, a control circuit 404, a radio frequency circuit 405, an input unit 406, a sensor 408, and a power supply 409. The processor 401 is electrically connected to the display screen 403, the control circuit 404, the radio frequency circuit 405, the input unit 406, the camera 407, the sensor 408, and the power supply 409.
[0118] The display screen 403 can be used to display information input by the user or information provided to the user, as well as various graphical user interfaces of electronic devices, which can be composed of images, text, icons, videos, and any combination thereof.
[0119] The control circuit 404 is electrically connected to the display screen 403 and is used to control the display screen 403 to display information.
[0120] The radio frequency circuit 405 is used to transmit and receive radio frequency signals to establish wireless communication with electronic devices or other electronic devices, and to transmit and receive signals with servers or other electronic devices.
[0121] The input unit 406 can be used to receive input numeric or character information or user characteristic information (such as fingerprints), and to generate keyboard, mouse, joystick, optical, or trackball signal inputs related to user settings and function control. The input unit 406 may include a fingerprint recognition module.
[0122] Camera 407 can be used to acquire image information. This camera can be a single camera with one lens, or it can have two or more lenses.
[0123] Sensor 408 is used to collect information about the external environment. Sensor 408 may include an ambient light sensor, an acceleration sensor, a light sensor, a motion sensor, and other sensors.
[0124] The power supply 409 is used to supply power to the various components of the electronic device 400. In some embodiments, the power supply 409 can be logically connected to the processor 401 through a power management system, thereby enabling functions such as managing charging, discharging, and power consumption through the power management system.
[0125] although Figure 5As not shown in the diagram, the electronic device 400 may also include a speaker, a Bluetooth module, etc., which will not be described in detail here.
[0126] As can be seen from the above, the electronic device provided in this application, during the operation of a coroutine, monitors the lifecycle changes of the creating entity and determines the current lifecycle event based on the monitoring results; it then performs corresponding operations according to the lifecycle event and a preset mapping relationship set, wherein the preset mapping relationship set includes the correspondence between sample lifecycle events and sample response events. This solution can realize coroutines controlled by the lifecycle, reducing memory leaks.
[0127] In some embodiments, a computer-readable storage medium is also provided, which stores a plurality of instructions adapted to be loaded by a processor to execute any of the above-described coroutine execution methods.
[0128] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, which may include: read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0129] The foregoing has provided a detailed description of the coroutine running method, apparatus, storage medium, and electronic device provided in the embodiments of this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A method for running coroutines, characterized in that, include: During the coroutine's execution, the lifecycle changes of the creating entity are monitored; Determine the current lifecycle event based on the monitoring results; Perform corresponding operations based on the lifecycle events and the preset mapping relationship set, wherein the preset mapping relationship set includes: the correspondence between sample lifecycle events and sample response events, and the response events are customized by the user according to actual needs; Before performing the corresponding operation based on the lifecycle events and the preset mapping relationship set, the method further includes: Determine whether the lifecycle event is a destruction event; If so, then perform a destruction operation on the coroutine.
2. The coroutine execution method according to claim 1, characterized in that, The step of performing corresponding operations based on the lifecycle events and the preset mapping relationship set includes: Determine whether there exists a target sample lifecycle event that matches the lifecycle event in the preset mapping relationship set; If it exists, then determine the target sample response event corresponding to the target sample lifecycle event based on the correspondence; Perform the operation corresponding to the target sample response event.
3. The coroutine execution method according to claim 2, characterized in that, Also includes: If there is no target sample lifecycle event matching the lifecycle event in the preset mapping relationship set, the original running logic of the coroutine is maintained.
4. The coroutine execution method according to claim 1, characterized in that, The monitoring of lifecycle changes of the created entity includes: The lifecycle changes of the created entity are monitored through a proxy class, wherein the proxy class is obtained by extending the Job class of the coroutine through a target extension function, and the target extension function can monitor the lifecycle changes of the created entity.
5. The coroutine execution method according to claim 4, characterized in that, Before monitoring changes in the lifecycle of the created entity, the following is also included: Create a coroutine scope, and create the coroutine within the coroutine scope; Obtain the Job class returned by the coroutine execution; The Job class is extended using the target extension function to generate a proxy class.
6. The coroutine execution method according to claim 5, characterized in that, The set of preset mapping relationships is stored within the coroutine's scope.
7. A coroutine operation device, characterized in that, include: The listening unit is used to monitor changes in the lifecycle of the created entity during the execution of the coroutine; The determination unit is used to determine the current lifecycle event based on the monitoring results. The processing unit is configured to perform corresponding operations based on the lifecycle events and the preset mapping relationship set, wherein the preset mapping relationship set includes: the correspondence between sample lifecycle events and sample response events, and the response events are customized by the user according to actual needs; Before performing the corresponding operation based on the lifecycle events and the preset mapping relationship set, the method further includes: Determine whether the lifecycle event is a destruction event; If so, then perform a destruction operation on the coroutine.
8. A computer-readable storage medium, characterized in that, The storage medium stores a plurality of instructions, which are adapted to be loaded by a processor to execute the coroutine execution method according to any one of claims 1-6.
9. An electronic device, characterized in that, It includes a processor and a memory, the processor being electrically connected to the memory, the memory being used to store instructions and data; the processor being used to execute the coroutine execution method according to any one of claims 1-6.