Initialization method and device based on Android platform, computer equipment and readable storage medium
By building a directed acyclic graph on the Android platform and using the graph traversal algorithm to determine the task sequence, the problem of chaotic and time-consuming task execution during the Android application initialization process is solved, and efficient and stable application startup and improved user experience is achieved.
Patent Information
- Application Number
- CN202510492707.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-08-08
AI Technical Summary
On the Android platform, during the initialization process of the application, the task execution is chaotic and time-consuming, making it difficult to meet the user's needs for rapid response.
By monitoring the life cycle state of the application, multiple initialization tasks are obtained in the onAttachBaseContext stage, directed acyclic graph characterization tasks and dependencies are constructed, and tasks are determined using the preset graph traversal algorithm to determine the task execution order and execute tasks in sequence.
Optimize the task execution process, improve application startup efficiency and stability, and improve user experience.
Smart Images

Figure CN120447980A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software processing, and in particular to an initialization method, device, computer equipment and readable storage medium based on an Android platform. Background Art
[0002] On the Android platform, the initialization process of an app is crucial to its startup performance and user experience. As apps become increasingly complex, encompassing numerous initialization tasks with interdependent dependencies, traditional initialization methods can easily lead to issues like chaotic task execution and lengthy startup times. Existing technologies are inadequate in handling task dependencies and optimizing the startup process, making it difficult to meet user demands for fast app responsiveness. Therefore, a more efficient and organized initialization method is needed to improve application startup efficiency and stability. Summary of the Invention
[0003] The object of the present invention is to provide an initialization method, device, computer equipment and readable storage medium based on the Android platform.
[0004] In a first aspect, an embodiment of the present invention provides an initialization method based on an Android platform, comprising:
[0005] In response to a user's startup operation on a target application, monitoring a life cycle state of the target application;
[0006] When the target application is in the onAttachBaseContext lifecycle, obtaining multiple initialization tasks of the target application;
[0007] Based on the multiple initialization tasks, a directed acyclic graph is constructed, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driving dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of initialization tasks that it is dependent on;
[0008] Determining an execution order of the plurality of initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm;
[0009] The multiple initialization tasks are executed based on the execution order to complete the initialization of the target application.
[0010] In a possible implementation, determining the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm includes:
[0011] Determining a topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm;
[0012] An execution order of the multiple initialization tasks is determined based on the topology sequence.
[0013] In a possible implementation, the preset graph traversal algorithm is a breadth-first algorithm;
[0014] Determining the topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm includes:
[0015] Traversing vertices based on a breadth-first algorithm, determining vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree;
[0016] Repeat the steps of traversing vertices based on the breadth-first algorithm, determining vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree, until all vertices in the directed acyclic graph are traversed, and determining the topological sequence of multiple initialization tasks based on the deletion order of each vertex.
[0017] In a possible implementation, constructing a directed acyclic graph further includes:
[0018] If there is a target initialization task that does not explicitly declare a dependency, the dependency is automatically inferred based on the resource access type of the target initialization task, which includes file reading and writing, network requests, or database operations. Among them, tasks with conflicting access to the same resource will be automatically added with dependency edges to form a serial execution order.
[0019] In a possible implementation, executing the multiple initialization tasks further includes:
[0020] A priority label is marked for each initialization task. When the in-degree of at least two initialization tasks is 0 at the same time, the tasks are executed in descending order of priority. The priority label is dynamically calculated based on the sensitivity of the initialization task to the user-visible delay.
[0021] In a possible implementation, the traversal of vertices based on the breadth-first algorithm further includes:
[0022] Dynamically monitor the number of CPU cores of the current device and adjust the size of the parallel execution thread pool according to the number of cores, wherein the maximum number of concurrent threads in the thread pool does not exceed twice the number of cores.
[0023] In a possible implementation, executing the multiple initialization tasks further includes:
[0024] If the target initialization task fails to execute, all out-degree dependent tasks of the target initialization task are skipped, and the preset degradation logic is triggered, which includes loading the local cache or calling the backup service interface.
[0025] In a second aspect, an embodiment of the present invention provides an initialization device based on an Android platform, comprising:
[0026] A trigger module is configured to monitor the lifecycle state of a target application in response to a user's startup operation on the target application; when the target application is in the onAttachBaseContext lifecycle, obtain multiple initialization tasks of the target application; and construct a directed acyclic graph based on the multiple initialization tasks, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driver dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of initialization tasks that it is dependent on;
[0027] An initialization module is used to determine the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm; and execute the multiple initialization tasks based on the execution order to complete the initialization of the target application.
[0028] In a third aspect, an embodiment of the present invention provides a computer device, comprising a processor and a non-volatile memory storing computer instructions, wherein when the computer instructions are executed by the processor, the computer device executes the method described in the first aspect.
[0029] In a fourth aspect, an embodiment of the present invention provides a readable storage medium, wherein the readable storage medium includes a computer program, and when the computer program is executed, the computer device where the readable storage medium is located is controlled to execute the method described in the first aspect.
[0030] Compared with the existing technology, the beneficial effects provided by the present invention include: using the disclosed initialization method, device, computer equipment and readable storage medium based on the Android platform, by responding to the target application startup operation, monitoring its life cycle status, and obtaining multiple initialization tasks in the onAttachBaseContext stage. By constructing a directed acyclic graph to represent tasks and dependencies, using a preset graph traversal algorithm to determine the order of task execution, and executing tasks in sequence to complete application initialization. This design effectively sorts out task dependencies, optimizes the execution process, improves application startup efficiency, enhances user experience, and has good application prospects. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] To more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly describes the drawings required for use in the embodiments. It should be understood that the following drawings illustrate only certain embodiments of the present invention and should not be construed as limiting the scope of the present invention. Those skilled in the art can, without inventive effort, derive other relevant drawings from these drawings.
[0032] Figure 1 A schematic diagram of the steps of the initialization method based on the Android platform provided in an embodiment of the present invention;
[0033] Figure 2 A schematic block diagram of the structure of an initialization device based on the Android platform provided in an embodiment of the present invention;
[0034] Figure 3 A schematic block diagram of the structure of a computer device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0035] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings of the embodiments of the present invention. It should be understood that the described embodiments are only a portion of the embodiments of the present invention, not all of them. Generally, the components of the embodiments of the present invention described and illustrated in the drawings herein may be arranged and designed in a variety of different configurations.
[0036] The specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0037] In order to solve the technical problems in the above background technology, Figure 1 This is a flow chart of an initialization method based on the Android platform provided in an embodiment of the present disclosure. The initialization method based on the Android platform is introduced in detail below.
[0038] Step S201: In response to a user's startup operation on a target application, monitoring the lifecycle state of the target application;
[0039] Step S202: when the target application is in the onAttachBaseContext lifecycle, obtain multiple initialization tasks of the target application;
[0040] Step S203: construct a directed acyclic graph based on the multiple initialization tasks, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driving dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of other initialization tasks that depend on it;
[0041] Step S204, determining the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm;
[0042] Step S205 , executing the multiple initialization tasks based on the execution order to complete the initialization of the target application.
[0043] In the embodiments of the present invention, for example, in the mobile internet era, there are numerous applications on the Android platform, and users frequently launch various applications every day. To ensure efficient and stable application startup, a reasonable initialization method is crucial. The following describes each step of the initialization method based on the Android platform, using a local processor as the execution subject.
[0044] When a user clicks a target application icon on an Android device, a launch request is generated.
[0045] For example, suppose a user clicks on the popular social app "XX Social." After receiving the launch request, the local processor begins monitoring the app's lifecycle. On the Android platform, the app lifecycle is managed by a series of callback methods, much like a complex production line, with each stage having specific tasks and timings.
[0046] In the example of the "XX Social" app, the local processor monitors the various lifecycle stages of the app from its launch, such as initial creation and execution. It utilizes the relevant interfaces and mechanisms provided by the Android system to obtain real-time information about the app's status. This is analogous to a factory, where various sensors and monitoring devices provide real-time feedback on the operating status of each link in the production line. The local processor serves as the hub for collecting and analyzing this feedback.
[0047] In the Android app lifecycle, onAttachBaseContext is triggered relatively early. When the local processor detects that the "XX Social" app has entered the onAttachBaseContext lifecycle, it begins to retrieve multiple initialization tasks for the app.
[0048] Initialization tasks can be understood as the various raw materials and tools required to prepare before launching an application. For an "XX social" application, these initialization tasks could encompass a wide range of aspects. For example, initializing the basic user interface settings is like setting up a stage: first determining basic elements such as stage size, background color, and lighting layout. Specifically, this might involve setting the overall style of the application's main interface, whether minimalist or ornate, and determining the placement of various functional modules, such as the message list, friend list, and status posting button.
[0049] Another example involves initializing network connection tasks. This is because the "XX Social" application needs to interact with the local processor in real time to obtain friends' updates and send user messages. Therefore, basic network request parameters must be set, such as the local processor's address, port number, and timeout. This can be thought of as establishing a communication channel with the outside world, requiring the initial determination of the channel's starting and end points, as well as some of the channel's traffic rules.
[0050] Additionally, there may be tasks related to initializing the database. The "XX Social" application needs to store user information, such as friend lists, chat logs, and posted updates. Therefore, a database connection must be established and the necessary table structures created. This is like building a warehouse: first, the framework must be established and different storage areas must be defined to enable orderly storage and management of various data.
[0051] The local processor obtains these initialization tasks through various channels, including configuration files and code logic provided by the application. The configuration file can be thought of as a detailed construction drawing, which records the specific requirements and parameters of various initialization tasks; the code logic specifies the specific process and conditions for obtaining these tasks.
[0052] After receiving multiple initialization tasks for the "XX Social" application, the local processor begins constructing a directed acyclic graph (DAG). This DAG can be imagined as a complex task relationship network.
[0053] Taking the initialization tasks of the "XX social" application as an example, assume that there are the following initialization tasks: Task A is to initialize the basic settings of the user interface, Task B is to initialize the network connection, Task C is to initialize the database connection, Task D is to load the user's friend list, and Task E is to load the user's chat history.
[0054] Task A might be a relatively independent task, independent of other tasks, and can serve as a starting vertex in a directed acyclic graph. Tasks B and C might also be relatively independent initialization tasks, and they can also serve as starting vertices because they do not need to wait for other specific tasks to complete before they can begin execution.
[0055] Task D, loading the user's friend list, may need to wait until Task C initializes the database connection, as the friend list data is stored in the database. Therefore, there is a directed edge from Task C to Task D, indicating that Task D depends on Task C. Similarly, Task E, loading the user's chat history, may also depend on Task C initializing the database connection and Task B initializing the network connection, as the chat history may require some of the latest data from the local processor. Thus, there are directed edges from both Task C and Task B to Task E.
[0056] In this directed acyclic graph, the in-degree and out-degree of each task have clear meanings. For example, Task C, which initializes a database connection, has two out-degrees because both Task D and Task E depend on it. Task D, on the other hand, has an in-degree of 1 because Task C is its only predecessor.
[0057] By constructing such a directed acyclic graph, the local processor can clearly understand the sequence and dependencies between various initialization tasks, just like a project manager understands the relationship between various tasks in the project through a project flowchart, providing an important basis for subsequently determining the order of task execution.
[0058] Assume the local processor uses a breadth-first algorithm. First, the local processor identifies vertices in the directed acyclic graph with an in-degree of 0. These are initialization tasks that have no prerequisites. In the example of the "XX Social" app, Task A initializes basic user interface settings, Task B initializes network connections, and Task C initializes database connections. These tasks all have an in-degree of 0, so they are selected first.
[0059] The local processor stores these vertices with an in-degree of 0 in a preset queue and processes them sequentially in the queue's first-in, first-out order. Task A is processed first. When task A is completed, task A and all directed edges from task A are deleted. The DAG is then checked to see if any new vertices have their in-degrees changed to 0. Assume that after task A is completed, no new vertices have their in-degrees changed to 0. Next, task B is removed from the queue for processing. After processing, task B and its related directed edges are also deleted. The processor then checks to see if any new vertices have their in-degrees changed to 0. Similarly, after task C is processed, since task D depends on task C, its in-degree becomes 0, and task D is added to the queue. Task D is then processed in the queue order. After task D is processed, the in-degree of task E may also meet the requirements. In this case, task E is added to the queue and processed.
[0060] Through this breadth-first traversal algorithm, the local processor can gradually determine the execution order of all initialization tasks based on the dependencies between tasks. This can be understood as finding each executable task in a complex task chain according to certain rules, ensuring the entire initialization process proceeds in an orderly manner.
[0061] After determining the execution order of multiple initialization tasks of the "XX Social" application, the local processor will execute these tasks in this order.
[0062] First, Task A initializes the basic user interface settings. The local processor configures the application's user interface based on previously acquired parameters and configuration information. For example, it adjusts the interface's color scheme to make it more comfortable and aesthetically pleasing, and sets the styles and interaction effects of each function button to make it easier for users to operate.
[0063] Next, Task B initializes the network connection. The local processor establishes a stable connection with the target local processor based on pre-set network parameters. Network connectivity is tested and optimized to ensure data transmission stability and speed. For example, appropriate network caching strategies are set to reduce duplicate data requests and improve application responsiveness.
[0064] Next, Task C initializes the database connection. The local processor establishes a connection based on the database configuration information. It creates the necessary data table structures and performs database initialization operations. For example, it creates a friend list and chat history table, and sets the corresponding fields and indexes to enable efficient data storage and query.
[0065] After completing the previous basic tasks, Task D is executed sequentially to load the user's friend list. The local processor reads the friend list data from the connected database and performs necessary processing and parsing. For example, it formats the friend's profile picture, nickname, and other information for correct display in the application interface.
[0066] Finally, Task E is executed to load the user's chat history. The local processor combines the local chat history in the database with the latest chat history retrieved from the local processor over the network, integrating and displaying it. The chat history is sorted and categorized for easy viewing and searching by the user.
[0067] Once all initialization tasks are completed in order, the initialization of the "XX Social" application is complete. At this point, the application has the basic conditions for normal operation and users can start using various application functions, such as viewing friends' updates, sending messages, and making voice and video calls.
[0068] In the embodiment of the present invention, the determining of the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm can be implemented through the following examples.
[0069] Determining a topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm;
[0070] An execution order of the multiple initialization tasks is determined based on the topology sequence.
[0071] In an embodiment of the present invention, for example, after constructing the directed acyclic graph of the "XX social" application initialization task, the local processor determines the topological sequence according to a preset graph traversal algorithm. Assume that the preset algorithm is a breadth-first search algorithm (BFS).
[0072] In the directed acyclic graph of the "XX Social" application, there are initialization tasks such as Task A (initialize basic user interface settings), Task B (initialize network connection), Task C (initialize database connection), Task D (load the user's friend list), and Task E (load the user's chat history).
[0073] The local processor first finds the vertices in the directed acyclic graph with in-degree 0: Task A, Task B, and Task C. These tasks have no prerequisites and are like the foundational work that can be started at the beginning of a project. The local processor stores them in a pre-set queue.
[0074] Task A is removed from the head of the queue for processing. After processing, it and its related directed edges are deleted. At this point, the graph is checked to see if there are any new vertices with in-degree 0. Task B is then removed for processing. Similarly, its related elements are deleted after processing. Again, no new vertices with in-degree 0 are found. Task C is then removed for processing. Since Task D depends on Task C, its in-degree becomes 0 after Task C is processed, and Task D is added to the queue. Task D is then processed. After Task D is processed, Task E's in-degree meets the requirements, and Task E is added to the queue.
[0075] As the above operations progress, the local processor determines a topological sequence of multiple initialization tasks based on the vertex processing order: Task A, Task B, Task C, Task D, Task E. This topological sequence can be understood as an ordered list of tasks, clearly defining the order of each task in the entire initialization process.
[0076] After obtaining the topology sequence, the local processor directly uses it as the execution order of multiple initialization tasks.
[0077] For the "XX Social" application, the local processor first executes Task A, performs basic settings on the application's user interface, and builds a basic framework for user operations, such as setting the overall color tone of the interface and laying out the positions of various functional modules.
[0078] Then execute Task B to establish a stable network connection and prepare for subsequent data interaction, such as configuring parameters such as the local processor address and port number, and testing network connectivity.
[0079] Then execute Task C, connect to and initialize the database, create the data table structure, such as creating a data table to store friend information and chat records, and set field properties.
[0080] Then, execute Task D, which reads the user's friend list data from the initialized database and processes it, such as parsing friend avatars, nicknames, and other information.
[0081] Finally, execute Task E to load the user's chat history, integrate the data on the local and local processor sides, and perform sorting and display settings for user convenience.
[0082] In an embodiment of the present invention, the preset graph traversal algorithm is a breadth-first algorithm;
[0083] Determining the topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm includes:
[0084] Traversing vertices based on a breadth-first algorithm, determining vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree;
[0085] Repeat the steps of traversing vertices based on the breadth-first algorithm, determining vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree, until all vertices in the directed acyclic graph are traversed, and determining the topological sequence of multiple initialization tasks based on the deletion order of each vertex.
[0086] In this embodiment of the present invention, for example, the local processor uses a breadth-first algorithm to traverse a directed acyclic graph (DAG) constructed by initializing tasks for the "XX Social" application. In this graph, tasks A (initializing basic user interface settings), B (initializing network connections), C (initializing database connections), D (loading the user's friend list), and E (loading the user's chat history) appear as vertices, with dependencies connected by directed edges.
[0087] The local processor first scans the entire directed acyclic graph, searching for vertices with an in-degree of 0. These are initialization tasks with no prerequisites. The local processor quickly determines that Tasks A, B, and C all have an in-degree of 0. These three tasks, like the cornerstone tasks at the start of a project, can be initiated first. The local processor records these three vertices with an in-degree of 0 and then deletes them and all directed edges from the directed acyclic graph. For example, if Task A has no dependencies on other tasks, its deletion also removes the edges connecting it to other tasks, as these edges are no longer relevant in determining the subsequent topological sequence.
[0088] After deleting Tasks A, B, and C, along with their associated edges, the local processor again traverses the remaining DAG vertices using the breadth-first algorithm. It discovers that Task D, which originally depended on Task C, has a zero in-degree after its deletion. The local processor records Task D and deletes it and its associated edges from the graph.
[0089] Then, the local processor continues to traverse and finds that task E has been deleted because its dependent tasks B and D have been deleted. The local processor then records task E and deletes task E and its related edges.
[0090] At this point, all vertices in the directed acyclic graph have been traversed and processed. The local processor, following the recorded order of vertex deletion—Task A, Task B, Task C, Task D, Task E—determines the topological sequence of the "XX Social" application's multiple initialization tasks. This topological sequence can be understood as a precise blueprint for task execution, clearly outlining the order in which each initialization task should be prioritized within the overall initialization process. Subsequently, the local processor will systematically schedule the execution of each initialization task based on this topological sequence, ensuring that the "XX Social" application completes its initialization process efficiently and stably, providing a positive user experience.
[0091] In the embodiment of the present invention, when constructing the directed acyclic graph, the following implementation method is also provided.
[0092] If there is a target initialization task that does not explicitly declare a dependency, the dependency is automatically inferred based on the resource access type of the target initialization task, which includes file reading and writing, network requests, or database operations. Among them, tasks with conflicting access to the same resource will be automatically added with dependency edges to form a serial execution order.
[0093] In an embodiment of the present invention, for example, in the initialization task of the "XX social" application, there are multiple tasks, such as task A (initializing basic user interface settings), task B (initializing network connection), task C (initializing database connection), task D (loading the user's friend list), task E (loading the user's chat history), task F (reading the local configuration file to obtain personalized settings) and task G (sending the first login confirmation request to the local processor), etc.
[0094] When the local processor starts building a directed acyclic graph, for some tasks that explicitly declare dependencies, such as task D's dependency on task C (because the friend list data is stored in the database, the database connection must be initialized first), the corresponding directed edges will be directly constructed according to the declaration.
[0095] However, for some target initialization tasks that do not explicitly declare dependencies, the local processor automatically infers dependencies based on their resource access types. For example, Task F (reading the local configuration file to obtain personalized settings) and Task E (loading the user's chat history). Task F performs file read and write operations and reads the local configuration file; Task E may also involve reading some chat history files stored locally. Because they both access the same resource, such as local files, there is a possibility of access conflicts. To avoid problems such as data reading errors or inconsistencies, the local processor automatically adds a dependency edge between Task F and Task E, so that they form a serial execution order. Specifically, the local processor determines that Task F should be executed first, and after reading the local configuration file, it executes Task E to load the user's chat history. This ensures order when accessing the same resource.
[0096] Next, let's look at Task G (sending an initial login confirmation request to the local processor). It performs a network request. Task B (initializing the network connection) is the foundational task for establishing a network connection for the entire application. Although Task G doesn't explicitly declare a dependency on Task B, from a resource access type perspective, Task G's network request relies on a stable network connection, meaning it relies on Task B completing the initialization of the network connection. Therefore, the local processor automatically adds a dependency edge between Task B and Task G, ensuring that Task B executes first to initialize the network connection before Task G executes to send the initial login confirmation request to the local processor.
[0097] In the embodiment of the present invention, the execution of the multiple initialization tasks further provides the following implementation methods.
[0098] A priority label is marked for each initialization task. When the in-degree of at least two initialization tasks is 0 at the same time, the tasks are executed in descending order of priority. The priority label is dynamically calculated based on the sensitivity of the initialization task to the user-visible delay.
[0099] In an embodiment of the present invention, for example, the initialization task set for the "XX Social" application includes Task A (initializing basic user interface settings), Task B (initializing network connections), Task C (initializing database connections), Task D (loading the user's friend list), and Task E (loading the user's chat history). When the local processor begins executing these initialization tasks, it first assigns a priority tag to each task.
[0100] For example, Task A initializes basic user interface settings. This directly impacts whether users can quickly see a complete and properly displayed interface after opening the app, and is highly sensitive to user-visible latency. Once users click the app icon, they expect to see the initial interface quickly. Therefore, the local processor dynamically calculates a higher priority tag based on its sensitivity to user-visible latency.
[0101] Task B, which initializes the network connection, is also critical, but is less sensitive to user-visible latency. This is because the user interface may be performing other preparations while the network connection is initialized, and the user may not immediately notice whether the network connection has been initialized. Therefore, the local processor assigns it a lower priority tag than Task A.
[0102] When the local processor determines the execution order, it discovers that the in-degree of both Task A and Task B is 0 (that is, they have no predecessor dependent tasks). At this point, the local processor executes the tasks in descending priority order. Because Task A has a higher priority than Task B, the local processor executes Task A first. The local processor then calls the relevant code logic and resources to perform basic configuration of the application's user interface, including setting the overall interface layout and loading the default background image.
[0103] After Task A is completed, the local processor then executes Task B, which initializes the network connection, configures network parameters, establishes a connection with the local processor, and so on.
[0104] Next, consider Task C, which initializes the database connection. Its priority calculation takes into account that while the database connection is essential for subsequent data loading, its impact on user-visible latency is relatively indirect. Therefore, it has a lower priority than Tasks A and B.
[0105] If at a certain moment, the in-degrees of Task A, Task B, and Task C are all 0, the local processor will still execute them in the order of priority from high to low, that is, Task A, Task B, and Task C.
[0106] By assigning a priority tag to each initialization task and executing them according to priority when multiple tasks with zero in-degree exist, the local processor can more effectively arrange the execution order of the "XX Social" application's initialization tasks. This helps optimize the user experience, allowing users to see key interface elements appear more quickly when launching the application. It also ensures that all initialization tasks are carried out in an orderly manner, guaranteeing stable startup and normal operation of the application.
[0107] In the embodiment of the present invention, when traversing vertices based on the breadth-first algorithm, the following implementation methods are also provided.
[0108] Dynamically monitor the number of CPU cores of the current device and adjust the size of the parallel execution thread pool according to the number of cores, wherein the maximum number of concurrent threads in the thread pool does not exceed twice the number of cores.
[0109] In an embodiment of the present invention, illustratively, when traversing the vertices of the directed acyclic graph based on the breadth-first algorithm, the local processor dynamically monitors the number of CPU cores of the current device and adjusts the size of the parallel execution thread pool accordingly.
[0110] When a user clicks the "XX Social" app icon, the local processor begins the app's initialization process. While traversing vertices using a breadth-first algorithm to determine the topological sequence of initialization tasks, the local processor first starts monitoring the number of CPU cores on the current device. Assuming the device is a typical mid-range to high-end smartphone, monitoring reveals it has eight cores.
[0111] Based on the number of CPU cores detected, the local processor adjusts the size of the thread pool for parallel execution according to the rules. Because the maximum number of concurrent threads in the thread pool cannot exceed twice the number of cores, the local processor sets the maximum number of concurrent threads in the thread pool to 16.
[0112] The thread pool comes into play when executing specific initialization tasks, such as those with zero in-degree that can be executed in parallel. For example, Task A (initializing basic user interface settings), Task B (initializing network connections), and Task C (initializing database connections) all have zero in-degree and no direct dependencies. The local processor allocates threads from the thread pool to execute these tasks in parallel.
[0113] The local processor assigns a thread to Task A. This thread is responsible for calling the relevant code logic and performing basic configuration of the application's user interface, including setting the interface's color scheme and the placement of various functional modules. Simultaneously, a thread is assigned from the thread pool to Task B. This thread initiates the network connection, sets network request parameters, and establishes a communication link with the local processor. Furthermore, a thread is assigned to Task C, which is responsible for establishing a database connection and creating the data table structure.
[0114] During this process, the local processor monitors thread pool usage and task execution progress in real time. If it detects that certain tasks are executing slowly or that thread pool thread resource utilization is low, the local processor will dynamically adjust thread allocation based on the actual situation. For example, if Task A completes early while Tasks B and C are still in progress, the local processor may temporarily reassign the thread originally assigned to Task A to other subsequent tasks that can be executed in parallel (if any) to improve overall execution efficiency.
[0115] By dynamically monitoring the number of CPU cores on the device and adjusting the thread pool size, the local processor can fully utilize the device's hardware resources and rationally arrange parallel execution of tasks during the initialization of the "XX Social" application. This not only avoids resource contention and system performance degradation caused by excessive threads, but also speeds up application initialization, providing users with a smoother startup experience.
[0116] In the embodiment of the present invention, the execution of the multiple initialization tasks further provides the following implementation methods.
[0117] If the target initialization task fails to execute, all out-degree dependent tasks of the target initialization task are skipped, and the preset degradation logic is triggered, which includes loading the local cache or calling the backup service interface.
[0118] In an embodiment of the present invention, illustratively, taking the "XX Social" social application as an example, when executing multiple initialization tasks, the local processor may face the situation of task execution failure and take corresponding processing measures.
[0119] The initialization tasks for the "XX Social" application include Task A (initializing basic user interface settings), Task B (initializing network connections), Task C (initializing database connections), Task D (loading the user's friend list), and Task E (loading the user's chat history). Assume that Task D is the target initialization task and depends on the completion of Task C (initializing database connections). Task D also has an out-degree dependency on Task E (loading the user's chat history), meaning that Task E depends on the completion of Task D.
[0120] When the local processor executes task D according to the determined execution order, due to some reasons, such as corruption of the friend list data in the database or a temporary exception in the database connection at this time, task D fails to load the user's friend list.
[0121] When the local processor detects that Task D has failed, it immediately takes action. First, it skips all out-degree dependent tasks of Task D, namely Task E. This is because Task E depends on the successful execution of Task D to obtain necessary data or status. Since Task D has failed, continuing to execute Task E will not produce correct results and may even cause further errors.
[0122] Then, the local processor triggers the preset degradation logic. In the "XX Social" application, the degradation logic includes loading the local cache or calling the backup service interface. For example, the local processor will try to load the user's friend list data stored in the local cache (if any). If the application has previously cached the friend list data during normal operation, the local processor can read this data from the local cache. Although it may not be the latest, it can ensure that the application displays relevant information normally to a certain extent, avoiding users from seeing a blank or erroneous interface.
[0123] If the local cache doesn't contain the friend list data, the local handler calls a backup service. Perhaps your application was designed to store a backup data service in addition to the primary database. The local handler sends a request to this backup service, attempting to retrieve the user's friend list data.
[0124] This approach allows the local processor to promptly skip potentially affected subsequent tasks when a target initialization task fails, preventing the error from spreading. Furthermore, by triggering the pre-set degradation logic, the application's basic functionality is maintained as much as possible, providing users with a relatively good user experience, rather than causing the entire application to fail to initialize or run properly due to a single task failure.
[0125] In order to more clearly describe the solution provided by the embodiment of the present invention, a relatively complete implementation method is provided below.
[0126] The initialization method provided in this embodiment is triggered during the onAttachBaseContext lifecycle. Conventionally, initialization optimization is performed during the application lifecycle callback onCreate, typically linearly initializing three-party SDKs and initialization operations. This method performs initialization tasks during the onAttachBaseContext lifecycle without code intrusion, significantly reducing code coupling.
[0127] Based on the directed acyclic graph constructed in the embodiments of the present invention, task execution is directed (ordered) and loop-free. In graph theory, a directed graph is a directed acyclic graph, or DAG, if it is impossible to return from a vertex through several edges. DAGs are often used to represent the driving dependencies between events and manage the scheduling of tasks.
[0128] In the DAG graph, including:
[0129] Vertex: A point in the graph, such as Task 1, Task 2;
[0130] Edge: A line segment connecting two vertices is called an edge;
[0131] In-degree: represents how many edges currently point to the vertex (how many tasks it depends on);
[0132] Out-degree: represents how many edges are emitted from the vertex (how many tasks depend on it).
[0133] After drawing the DAG based on the startup tasks, we need to find the topological sequence of the DAG, that is, sort the execution order of our startup tasks.
[0134] By topologically sorting the DAG, you can choose BFS (breadth-first) or DFS (depth-first). This method chooses BFS. The sorting process using the BFS algorithm is as follows:
[0135] (1) Find the vertex with 0 in-degree in the graph;
[0136] (2) Delete these vertices in the graph one by one, and then find the vertices with 0 in-degree;
[0137] (3) After deletion, find the vertex with 0 in-degree and repeat the second step.
[0138] The above solution, along with the selection of initialization timing and method, allows for non-intrusive initialization, significantly reducing code coupling and facilitating development, maintenance, and iteration. This method significantly reduces initialization time, quickly responds to user application startup operations, and improves user experience while providing more information.
[0139] Please refer to Figure 2 , Figure 2 An initialization device 110 based on the Android platform provided in an embodiment of the present invention includes:
[0140] A trigger module 1101 is configured to monitor the lifecycle state of a target application in response to a user's startup operation on the target application; when the target application is in the onAttachBaseContext lifecycle, obtain multiple initialization tasks of the target application; and construct a directed acyclic graph based on the multiple initialization tasks, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driver dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of other initialization tasks that depend on it.
[0141] The initialization module 1102 is configured to determine an execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm; and execute the multiple initialization tasks based on the execution order to complete the initialization of the target application.
[0142] It should be noted that the implementation principles of the aforementioned Android-based initialization device 110 can be referenced to the implementation principles of the aforementioned Android-based initialization method and will not be elaborated upon here. It should be understood that the division of the various modules of the aforementioned device is merely a division of logical functions. In actual implementation, they may be fully or partially integrated into a single physical entity or physically separated. Furthermore, these modules may be implemented entirely in software invoked by a processing element, or entirely in hardware. Alternatively, some modules may be implemented in software invoked by a processing element, while others may be implemented in hardware. For example, the Android-based initialization device 110 may be a separate processing element, or integrated into a chip of the aforementioned device. Furthermore, it may be stored in the form of program code in the memory of the aforementioned device, invoked by a processing element of the aforementioned device, and execute the functions of the aforementioned Android-based initialization device 110. The implementation of the other modules is similar. Furthermore, these modules may be fully or partially integrated together, or implemented independently. The processing element described herein may be an integrated circuit with signal processing capabilities. During implementation, the steps of the aforementioned method or the aforementioned modules may be performed by hardware integrated logic circuits within the processor element or by software instructions.
[0143] For example, the above modules may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASICs), one or more digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs). For another example, when a module is implemented by scheduling program code on a processing element, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0144] The embodiment of the present invention provides a computer device 100, which includes a processor and a non-volatile memory storing computer instructions. When the computer instructions are executed by the processor, the computer device 100 executes the aforementioned initialization device 110 based on the Android platform. Figure 3 As shown, Figure 3 This is a structural block diagram of a computer device 100 provided in an embodiment of the present invention. The computer device 100 includes an initialization device 110 based on the Android platform, a memory 111, a processor 112, and a communication unit 113.
[0145] In order to realize the transmission or interaction of data, the memory 111, the processor 112 and the communication unit 113 are electrically connected to each other directly or indirectly. For example, the electrical connection between these components can be achieved through one or more communication buses or signal lines. The initialization device 110 based on the Android platform includes at least one software function module that can be stored in the memory 111 in the form of software or firmware or solidified in the operating system (OS) of the computer device 100. The processor 112 is used to execute the initialization device 110 based on the Android platform stored in the memory 111, such as the software function modules and computer programs included in the initialization device 110 based on the Android platform.
[0146] An embodiment of the present invention provides a readable storage medium, which includes a computer program. When the computer program is running, it controls the computer device where the readable storage medium is located to execute the aforementioned initialization device 110 based on the Android platform.
[0147] For illustrative purposes, the foregoing description has been made with reference to specific embodiments. However, the above illustrative discussion is not intended to be exhaustive or to limit the present disclosure to the precise forms disclosed. Numerous modifications and variations are possible in light of the above teachings. These embodiments have been selected and described in order to best illustrate the principles of the present disclosure and its practical application, thereby enabling those skilled in the art to best utilize the present disclosure and to utilize various embodiments with various modifications as appropriate for the specific application contemplated.
Claims
1. An initialization method based on the Android platform, characterized in that, include: In response to a user's startup operation on a target application, monitoring a life cycle state of the target application; When the target application is in the onAttachBaseContext lifecycle, obtaining multiple initialization tasks of the target application; Based on the multiple initialization tasks, a directed acyclic graph is constructed, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driving dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of initialization tasks that it is dependent on; Determining an execution order of the plurality of initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm; The multiple initialization tasks are executed based on the execution order to complete the initialization of the target application.
2. The method according to claim 1, characterized in that Determining the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm includes: Determining a topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm; An execution order of the multiple initialization tasks is determined based on the topology sequence.
3. The method according to claim 2, characterized in that The preset graph traversal algorithm is a breadth-first algorithm; Determining the topological sequence of the multiple initialization tasks based on a preset graph traversal algorithm includes: Traversing the vertices based on the breadth-first algorithm, determining the vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree; Repeat the steps of traversing vertices based on the breadth-first algorithm, determining vertices with 0 in-degree in the directed acyclic graph, and deleting the vertices with 0 in-degree, until all vertices in the directed acyclic graph are traversed, and determining the topological sequence of multiple initialization tasks based on the deletion order of each vertex.
4. The method according to claim 1, wherein When constructing the directed acyclic graph, the method further includes: If there is a target initialization task that does not explicitly declare a dependency, the dependency is automatically inferred based on the resource access type of the target initialization task, which includes file reading and writing, network requests, or database operations. Among them, tasks with conflicting access to the same resource will be automatically added with dependency edges to form a serial execution order.
5. The method according to claim 1, wherein The executing of the multiple initialization tasks further includes: A priority label is marked for each initialization task. When the in-degree of at least two initialization tasks is 0 at the same time, the tasks are executed in descending order of priority. The priority label is dynamically calculated based on the sensitivity of the initialization task to the user-visible delay.
6. The method according to claim 3, characterized in that When traversing vertices based on the breadth-first algorithm, the method further includes: Dynamically monitor the number of CPU cores of the current device and adjust the size of the parallel execution thread pool according to the number of cores, wherein the maximum number of concurrent threads in the thread pool does not exceed twice the number of cores.
7. The method according to claim 1, characterized in that The executing of the multiple initialization tasks further includes: If the target initialization task fails to execute, all out-degree dependent tasks of the target initialization task are skipped, and the preset degradation logic is triggered, which includes loading the local cache or calling the backup service interface.
8. An initialization device based on the Android platform, characterized in that: include: a trigger module, configured to monitor the life cycle state of a target application in response to a user's startup operation on the target application; When the target application is in the onAttachBaseContext lifecycle, obtaining multiple initialization tasks of the target application; Based on the multiple initialization tasks, a directed acyclic graph is constructed, wherein the vertices of the directed acyclic graph are used to represent each initialization task, the edges are used to represent the driving dependency relationship between each of the initialization tasks, the in-degree of each initialization task represents the number of other initialization tasks that currently depend on it, and the out-degree of each initialization task represents the number of initialization tasks that it is dependent on; An initialization module is used to determine the execution order of the multiple initialization tasks from the directed acyclic graph based on a preset graph traversal algorithm; and execute the multiple initialization tasks based on the execution order to complete the initialization of the target application.
9. A computer device, characterized in that: The computer device includes a processor and a non-volatile memory storing computer instructions. When the computer instructions are executed by the processor, the computer device executes the method according to any one of claims 1 to 7.
10. A readable storage medium, characterized in that: The readable storage medium includes a computer program, and when the computer program is executed, the computer device where the readable storage medium is located is controlled to execute the method according to any one of claims 1 to 7.