首先看一张Android系统启动流程图:


     一个进程最重要的两项指标一个是启动了Binder线程池,也就是能够进程Binder进程间通信了。还有一个是启动了Handler消息循环,能够使用了消息循环机制。 


     1、那么systemserver进程是什么时候实现上面两个机制的呢?见代码:

     启动了Binder线程池。是子线程池。

public static final void zygoteInit(String[] argv)
            throws ZygoteInit.MethodAndArgsCaller 
{
        ......
        zygoteInitNative();
        ......
}

     启动了Binder主线程池,例如以下:

extern "C" status_t system_init()
{
    LOGI("Entered system_init()");
    
    sp<ProcessState> proc(ProcessState::self());
    
    sp<IServiceManager> sm = defaultServiceManager();
    LOGI("ServiceManager: %p\n", sm.get());
    
    sp<GrimReaper> grim = new GrimReaper();
    sm->asBinder()->linkToDeath(grim, grim.get(), 0);
    
    char propBuf[PROPERTY_VALUE_MAX];
    property_get("system_init.startsurfaceflinger", propBuf, "1");
    if (strcmp(propBuf, "1") == 0) {
        // Start the SurfaceFlinger
        SurfaceFlinger::instantiate();
    }

    // Start the sensor service
    SensorService::instantiate();

    // On the simulator, audioflinger et al don't get started the
    // same way as on the device, and we need to start them here
    if (!proc->supportsProcesses()) {

        // Start the AudioFlinger
        AudioFlinger::instantiate();

        // Start the media playback service
        MediaPlayerService::instantiate();

        // Start the camera service
        CameraService::instantiate();

        // Start the audio policy service
        AudioPolicyService::instantiate();
    }

    // And now start the Android runtime.  We have to do this bit
    // of nastiness because the Android runtime initialization requires
    // some of the core system services to already be started.
    // All other servers should just start the Android runtime at
    // the beginning of their processes's main(), before calling
    // the init function.
    LOGI("System server: starting Android runtime.\n");
    
    AndroidRuntime* runtime = AndroidRuntime::getRuntime();

    LOGI("System server: starting Android services.\n");
    runtime->callStatic("com/android/server/SystemServer", "init2");
        
    // If running in our own process, just go into the thread
    // pool.  Otherwise, call the initialization finished
    // func to let this process continue its initilization.
    if (proc->supportsProcesses()) {
        LOGI("System server: entering thread pool.\n");
        ProcessState::self()->startThreadPool();//已经启动过线程池了
        IPCThreadState::self()->joinThreadPool();//当前主线程增加线程池
        LOGI("System server: exiting thread pool.\n");
    }
    return NO_ERROR;
}

     启动了Hander消息循环机制,是子线程的Handler:

public static final void init2() {
        Slog.i(TAG, "Entered the Android system server!");
        Thread thr = new ServerThread();
        thr.setName("android.server.ServerThread");
        thr.start();
    }
class ServerThread extends Thread {


	@Override
    public void run() {
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
            SystemClock.uptimeMillis());

        Looper.prepare();

        .....
        

        try {
    
            ......
            Slog.i(TAG, "Activity Manager");
            context = ActivityManagerService.main(factoryTest);//初始化了ActivityTask

            ......
            Slog.i(TAG, "Package Manager");
            pm = PackageManagerService.main(context,
                    factoryTest != SystemServer.FACTORY_TEST_OFF);//初始化了PackageMangerService

            ActivityManagerService.setSystemProcess();

            .....

            wm = WindowManagerService.main(context, power,
                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
            ServiceManager.addService(Context.WINDOW_SERVICE, wm);

            ......

        ((ActivityManagerService)ActivityManagerNative.getDefault())
                .systemReady(new Runnable() {
            public void run() {

            }
        });

        

        Looper.loop();
        Slog.d(TAG, "System ServerThread is exiting!");
    }
}
     

     2、那么应用程序进程是什么时候实现上面两个机制的呢?见代码:

     启动了Binder线程池。和上面一样,都是子线程池。

public static final void zygoteInit(String[] argv)
            throws ZygoteInit.MethodAndArgsCaller 
{
        ......
        zygoteInitNative();
        ......
}
     启动了Hander消息循环机制,是主线程的Handler:

public static final void main(String[] args) {
        SamplingProfilerIntegration.start();

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();
        if (sMainThreadHandler == null) {
            sMainThreadHandler = new Handler();
        }

        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        ....

        Looper.loop();

        ......
    }
posted on 2017-07-29 20:50  lxjshuju  阅读(417)  评论(0编辑  收藏  举报