ActivityThread管理application进程中主线程的执行、调度,执行activities、broadcasts和activity manager请求的其他操作。
AMS中持有的ActivityThread mSystemThead实际上是SystemServer进程的主线程,它的的生命周期如下图所示:
目录
1 systemServer.createSystemContext()
2 ActivityThread.systemMain()
3 ActivityThread.attach()
4. AMS成员函数ActivityThread mSystemThread
5. AMS.setSystemProcess()
systemserver进程在创建系统上下文的时候调用了ActivityThread.systemMain()来创建该进程的主线程ActivityThread。
///frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer implements Dumpable {
private Context mSystemContext;
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
createSystemContext();
}
private void createSystemContext() {
//创建了systemserver的主线程
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
}
该方法中new ActivityThread(),并调用了其attach()方法。
/frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
implements ActivityThreadInternal {
@UnsupportedAppUsage
public static ActivityThread systemMain() {
ThreadedRenderer.initForSystemProcess();
ActivityThread thread = new ActivityThread();
thread.attach(true, 0);
return thread;
}
}
attach()会给ActivityThread的成员变量sCurrentActivityThread赋值,this表示当前进程创建的ActivityThread实例,从前边分析我们知道此处的this是systemserver进程的主线程。
/frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
implements ActivityThreadInternal {
@UnsupportedAppUsage
private static volatile ActivityThread sCurrentActivityThread;
@UnsupportedAppUsage
private void attach(boolean system, long startSeq) {
//被赋值当前实例的ActivityThread
sCurrentActivityThread = this;
mSystemThread = system;
...
}
}
mSystemThread在AMS的构造函数中调用ActivityThread.currentActivityThread()被赋值。ActivityThread.currentActivityThread()返回的是他的成员变量sCurrentActivityThread,而该成员变量由systemserver进程启动时被赋值,返回的是systemserver进程的主线程。因此AMS中持有的mSystemThread是systemserver进程的主线程。
// /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback, ActivityManagerGlobalLock {
final ActivityThread mSystemThread;
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
// 当前进程的
mSystemThread = ActivityThread.currentActivityThread();
mUiContext = mSystemThread.getSystemUiContext();
}
}
ActivityThread.currentActivityThread() 方法如下:
///frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
implements ActivityThreadInternal {
@UnsupportedAppUsage
private static volatile ActivityThread sCurrentActivityThread;
@UnsupportedAppUsage
public static ActivityThread currentActivityThread() {
return sCurrentActivityThread;
}
}
在该方法中使用AMS持有的mSystemThread来安装package=“android”系统application,所以该系统应用运行在systemserver进程中。
public void setSystemProcess() {
try {
//为ActivityThread安装相关system application信息
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
synchronized (this) {
//4.为systemserver 主进程开辟一个ProcessRecord来维护进程的相关信息
ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
false,
0,
new HostingRecord("system"));
app.setPersistent(true);
app.setPid(MY_PID); //PID是进程ID,AMS在systemserver进程中
app.mState.setMaxAdj(ProcessList.SYSTEM_ADJ);
app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
addPidLocked(app);
updateLruProcessLocked(app, false, null);
updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE);
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
...
}