For large apps, there are many startup tasks and complex task dependencies. The key problems are to ensure the singleness of task logic, decouple the starting task logic, rationally utilize the advantages of multi-core CPU and improve the running efficiency of thread.

So the purpose of launching the Framework core core is to solve two things:

  • Solve the task dependency problem, make the individual task responsibility clearer, the code is more elegant, can establish the startup specification, the subsequent maintenance is convenient.
  • Use multi-core CPU to improve task execution efficiency.

Research framework

Several similar frameworks for addressing task dependencies are now open source.

A. Startup Thread scheduling is not supported at this stage. Online introduction of the article is also a lot, the basic is unable to land the state.

B. Alpha thread switching support is imperfect and has not been maintained for a long time.

C. Android-startup supports perfect thread scheduling to meet basic requirements.

  • But the overall design is slightly complex and redundant; Android-startup does not manage node dependencies in the diagram. It directly resolves the dependencies in task ManagerTopological ordering of directed acyclic graphs is obtained by BFS methodAnd then dump all tasks into the thread pool for execution, waiting for the pre-dependent execution to wait and wake up through waitCountDownLatch. Alpha manages tasks through a bidirectional linked list. At the beginning, it performs the head task, notifes the successor node when the task is executed, and the successor stage determines whether the task can continue to be executed depending on whether the task is completed.
indicators App Startup Android Startup DGAppStartup
Manual configuration
Automatic configuration
Depend on the support
The closed-loop processing
The thread of control
Asynchronous waiting
Relying on the callback
Time-consuming statistics
Thread priority
Multiple processes

Dependency design

The overall design of dependencies is based on acyclic graph, and the specific code implementation refers to Alpha design. The disadvantages of topology sorting are avoided by the following node driving operation. DGAppStartup is also not designed to build a task diagram by configuration, because configuration needs to be parsed first, then reflection calls, etc.

1. Build directed acyclic graphs



1) Similar to the principle of linked list, each task node establishes a reference set of subsequent task nodes, and notifies subsequent nodes to run after the completion of task execution.

2) The successor node will check whether all dependent tasks are completed and execute the current task if all dependent tasks are completed.

Depend on the set

The dependency configuration section refers to the official Startup method, which builds directed acyclic graphs based on Class instances.

Improve task execution efficiency

Scenario: ABCD four tasks must be executed in the APP Create phase. In the absence of a startup frame, the four ABCD tasks need to be placed into the main thread for serial execution. However, with a startup frame, ABCD can be executed asynchronously at the same time and the next step can be executed after the completion of the four tasks through lock waiting.

Code design

By adding the isWaitOnMainThread attribute, it marks whether the current task must be executed at this stage, then counts all tasks marked with the attribute, the main thread adds the lock wait, and after each task completes, the count is subtracted by 1. When the count reaches zero, the lock wait is released, and the main thread continues to execute.

conclusion

The framework has been practiced in over 100 million apps and has been running stably for half a year. The overall effect is in line with expectations. The overall optimization is 400ms with simple task removal and thread scheduling. Frame address: github.com/Caij/DGAppS… If I can help you, Little Finger click Star Follow

Chapter three Combat to be continued…