Skip to content

Commit

Permalink
完善代码
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchong211 committed May 14, 2022
1 parent 6d10564 commit dd33a8d
Show file tree
Hide file tree
Showing 20 changed files with 635 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AppProcessLib/build.gradle
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'com.android.library'
}
apply from: rootProject.projectDir.absolutePath + "/yc.gradle"
//迁移到jitpack
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
Expand Down
Expand Up @@ -21,6 +21,7 @@
* @time : 2018/04/15
* @desc : 前后台监听
* @revise :
* GitHub : https://github.com/yangchong211/YCEfficient
*/
public class AppStateMonitor implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {

Expand Down
Expand Up @@ -11,6 +11,7 @@
* @time : 2018/04/15
* @desc : Shared singleton background thread for each process.
* @revise :
* GitHub : https://github.com/yangchong211/YCEfficient
*/
public final class BackgroundThread extends HandlerThread {

Expand Down
Expand Up @@ -6,6 +6,7 @@
* @time : 2018/04/15
* @desc : 前后台监听listener
* @revise :
* GitHub : https://github.com/yangchong211/YCEfficient
*/
public interface StateListener {

Expand Down
1 change: 1 addition & 0 deletions AppStartLib/.gitignore
@@ -0,0 +1 @@
/build
58 changes: 58 additions & 0 deletions AppStartLib/README.md
@@ -0,0 +1,58 @@
#### 目录介绍
- 01.App启动器介绍
- 02.启动任务
- 03.自定义启动任务
- 04.配置任务调度器参数



### 01.App启动器介绍
- **启动器**
- 本质所有任务就是一个有向无环图,通过Systrace确定wallTime和cpuTime,然后选择合适的线程池,这里的线程池有两种(cpu定容线程池,io缓存线程池),cpuTime长的就证明他消耗cpu时间片,多线程并发的本质就是抢夺时间片,所以cpuTime长的要选择定容线程池,防止他并发时候影响cpu效率,反之选择缓存线程池,再构造任务之间的图关系,因为有些任务有先后顺序(正确使用启动速度优化30%很容易)
- **Multdex**
- 5.0以下开新进程Activity去加载dex,其实就是为了第一时间显示第一个Activity,属于伪优化,其实在加载dex过程中,Multdex先将dex压缩成了zip,然后又解压zip,而他是可以直接去加载dex的,这里多了一个压缩又解压的过程,所以其实真正的优化应该是避免先解压再压缩。


### 02.启动任务
- 启动任务,在Application的onCreate中调用.
- add顺序无所谓,不影响图的关系,注意await()会阻塞等待所有非主线程AppStartTask中needWait返回true的任务,所以需要按需使用,主线程的任务本来就是阻塞的,所以,如果不需要等待非主线程任务的执行,则不用重写AppStartTask的needWait方法,也不用调用AppStartTaskDispatcher.await()方法。
``` java
AppTaskDispatcher.create()
.setShowLog(true)
.addAppStartTask(new AppCoreTask())
.addAppStartTask(new AppMonitorTask())
.addAppStartTask(new AppDelayTask())
.addAppStartTask(new AppThreadTask())
.addAppStartTask(new AppLazyTask())
.start()
.await();
```


### 03.自定义启动任务
- abstract必须重写的方法
| 方法 |参数或返回值 | 作用 |
| :-------- | :--------| :--: |
| run| void | 这里写你要执行的任务代码 |
| getDependsTaskList| 返回 List<Class<? extends AppStartTask>> | 这个方法用于构造图,这里返回当前任务需要依赖的任务,只有依赖任务执行完,当前任务才执行,如果没有,返回null |
| isRunOnMainThread| 返回boolean | 是否运行在主线程 |
- 根据实际需求选择重写的方法
| 方法 |参数或返回值 | 作用 |
| :-------- | :--------| :--: |
| priority| 返回int | 这个代表当前线程的优先级,优先级高不一定会优先执行,而是获得cpu时间片的几率会变高,默认返回Process.THREAD_PRIORITY_BACKGROUND |
| runOnExecutor| 返回 Executor | 在isRunOnMainThread返回false的情况下起作用,决定当前任务在哪个线程池执行,需要使用Systrace确定wallTime和cpuTime,占cpuTime多建议的使用cpu线程池:TaskExceutorManager.getInstance().getCPUThreadPoolExecutor() ,默认返回TaskExceutorManager.getInstance().getIOThreadPoolExecutor() |
| needWait| 返回boolean | 这个方法决定了你的这个任务是否是需要等待执行完的,需结合启动管理器AppStartTaskDispatcher.await方法使用 |



### 04.配置任务调度器参数
- 配置任务调度器参数,即AppStartTaskDispatcher的配置方法
| 方法 |参数或返回值 | 作用 |
| :-------- | :--------| :--: |
| create | 返回AppStartTaskDispatcher | 创建本次的任务分发器 |
| setShowLog| 返回AppStartTaskDispatcher | 是否输出日志,需使用者自行控制,默认为false,设为true可以看到任务的执行顺序,执行情况等信息 |
| setAllTaskWaitTimeOut| 返回AppStartTaskDispatcher | 阻塞等待所有非主线程AppStartTask中needWait返回true的任务的保护时间,即最多等待时间,超过这个时间不再阻塞,只在调用了await方法后生效 |
| addAppStartTask| 返回AppStartTaskDispatcher | 添加任务 |
| start| 返回AppStartTaskDispatcher | 开始执行任务 |
| await| void | 阻塞等待所有非主线程AppStartTask中needWait返回true的任务 |

34 changes: 34 additions & 0 deletions AppStartLib/build.gradle
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'
apply from: rootProject.projectDir.absolutePath + "/yc.gradle"
//迁移到jitpack
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies["appcompat"]
implementation rootProject.ext.dependencies["annotation"]
implementation 'com.github.yangchong211.YCThreadPool:EasyExecutor:1.3.7'
}
Empty file added AppStartLib/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions AppStartLib/proguard-rules.pro
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions AppStartLib/src/main/AndroidManifest.xml
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxy.appstartfaster" />
82 changes: 82 additions & 0 deletions AppStartLib/src/main/java/com/yc/appstart/AppStartTask.java
@@ -0,0 +1,82 @@
package com.yc.appstart;

import android.os.Process;


import com.yc.easyexecutor.DelegateTaskExecutor;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

/**
* @author: 杨充
* @email : yangchong211@163.com
* @time : 2018/04/15
* @desc : app启动任务分发器
* @revise :
* GitHub : https://github.com/yangchong211/YCEfficient
*/
public abstract class AppStartTask implements TaskInterface {

/**
* 当前Task依赖的Task数量(等父亲们执行完了,孩子才能执行),默认没有依赖
*/
private final CountDownLatch mDepends = new CountDownLatch(getTaskSize());

/**
* 当前Task等待,让父亲Task先执行
*/
public void waitToNotify() {
try {
mDepends.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public int priority() {
//优先级
return Process.THREAD_PRIORITY_BACKGROUND;
}

private int getTaskSize() {
return getDependsTaskList() == null ? 0 : getDependsTaskList().size();
}

/**
* 执行任务代码,让子类实现
*/
public abstract void run();

/**
* 刷新
*/
public void notifyTask() {
mDepends.countDown();
}

@Override
public Executor runOnExecutor() {
return DelegateTaskExecutor.getInstance().getIOThreadExecutor();
}

@Override
public List<Class<? extends AppStartTask>> getDependsTaskList() {
return null;
}

@Override
public boolean needWait() {
return false;
}

/**
* 是否在主线程执行
*
* @return true表示在主线程
*/
public abstract boolean isRunOnMainThread();

}

0 comments on commit dd33a8d

Please sign in to comment.