启动优化

异步初始化核心思想

子线程分担主线程任务,充分利用CPU,减少运行时间

演化方案

  • 1、new Thread

  • 2、IntentService

  • 3、线程池(合理配置并选择CPU密集型和IO密集型线程池)

  • 4、异步启动器

异步启动器

https://github.com/zeshaoaaa/AppStarter

通过启动器的学习,针对项目做了少量优化,后期时间足够可以引入异步启动器

把主线程中一部分分给线程池,通过线程池submit 配合 Future 做了等待,保证线程池中的任务在onCreat()中运行完成,在Romwe 5.2.4的debug包中取得了效果

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]

Status: ok

LaunchState: COLD

Activity: xxxxx

TotalTime: 1919

WaitTime: 1927

Complete

SZKSG00883:~ 10021077$ adb shell am start -W xxxxxxx

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=xxxxx}

Status: ok

LaunchState: COLD

Activity: xxxxxx

TotalTime: 1832

WaitTime: 1837

设备为三星 GalaxyF52

风险暂时未知,需要大家一起评测

参考文档

https://juejin.cn/post/6844904093786308622#heading-132