laitimes

Source code analysis of the implementation principle of the Android Startup startup framework

author:Brick moving small yard dragon

#头条创作挑战赛 #

Brief introduction

Today brings you an efficient startup framework AndroidStartUp. It not only solves the dependencies between different tasks, but also controls the initialization order between different tasks. Ensure that different initialization work is placed on different threads to execute. Supports asynchronous awaits between threads.

It is longer and it is recommended to read it after saving. The code address is attached at the end of the article.

Source code analysis of the implementation principle of the Android Startup startup framework

Use steps

1. Define two SDk initialization tasks, InitSdkOne and InitSdkTwo, respectively, which inherit from the Android Startup <T>abstract class.

  • InitSdkOne
Source code analysis of the implementation principle of the Android Startup startup framework
  • InitSdkTwo
Source code analysis of the implementation principle of the Android Startup startup framework

2.Add the task to the initialization manager

Source code analysis of the implementation principle of the Android Startup startup framework

3. Perform tasks

Source code analysis of the implementation principle of the Android Startup startup framework

It can be seen from the execution results that although the InitSdkTwo task is added to the task manager last, it is executed in the main thread first, while the InitSdkTwo task, although the first to be added, is executed last, and needs to wait for the main thread initialization to be completed.

Design pattern

Builder design patterns

Source code analysis of the implementation principle of the Android Startup startup framework

StartupManager internally uses the internal class Builder to build the task information needed to build the StartupManager, and uses the AtomicInteger atomic class to record the number of tasks that need to wait.

Core algorithm

Topological ordering

Topological ordering is mainly used to solve the dependency problem in the directed graph, we form all the tasks that need to be performed into a directed graph, and sort according to the dependencies between tasks and the order of execution.

As shown in the following figure:

Source code analysis of the implementation principle of the Android Startup startup framework

There are 5 tasks that need to be performed, and the following requirements are required if topology ordering is to be satisfied:

  1. Task 1 is executed first and does not need to depend on any conditions.
  2. Task 2 and Task 3 will need to wait until Task 1 is executed.
  3. Task 4 will not be executed until Task 2 is completed.
  4. Task 5 needs to wait for the joint execution of Task 3 and Task 4 to be executed.

The edges with arrows in the diagram can be seen as dependencies between tasks, the circles in the diagram represent tasks, and all tasks together form a directed acyclic graph. The resulting task order is 1->3->2->4->5.

Overall architecture

Source code analysis of the implementation principle of the Android Startup startup framework
  1. Define the task distribution interface:

There are four main methods defined in the dispatcher distribution interface, callCreateMainThread(), waitOnMainThread(), toWait(), and toNotify().

  1. Define the task interface.
Source code analysis of the implementation principle of the Android Startup startup framework

The definition Startup interface is inherited from Dispatcher and StartupExecutor, where StartupExecutor is used to provide a thread pool. At the same time, four methods are defined in Startup, which are create(), dependencies(), depedenciesByName(), and getDependnciesCount().

  1. Define the AndroidStartup abstract class, implement the Startup interface, and implement some of the methods in the interface.
Source code analysis of the implementation principle of the Android Startup startup framework

Use CountDownLatch to control concurrent execution between threads.

  1. Finally, use StartupManager to add tasks and initiate their execution.
Source code analysis of the implementation principle of the Android Startup startup framework

After the StartupManager addition task is completed, call the start method to perform the initialization of the task, and if there is other work before using StartupManager, then you can call await to wait for the main thread execution to end.

summary

AndroidStartup uses topological ordering to resolve dependencies between tasks while supporting child and main thread switching. The child thread can wait for the execution of the main thread task to be executed after execution, ensuring the order between tasks, and currently only implements the manual addition of simple tasks. Automatic injection of tasks is subsequently completed through annotations, bytecode instrumentation and other means.

GitHub address: https://github.com/AndroidYou/AndroidStartUp