watchdog工作流程

本文介绍了Android系统中watchdog的运作流程。在系统启动时,watchdog在各种服务启动后开始运行。它通过mHandlerCheckers和mMonitorChecker监控不同服务的线程和对象锁。当超时达到设定值,例如1分钟,watchdog会进入杀进程状态,确保系统的稳定运行。
摘要由CSDN通过智能技术生成

1、watchdog的产生

在系统启动阶段,systemserver.java中startOtherServices

final Watchdog watchdog = Watchdog.getInstance();
watchdog.init(context, mActivityManagerService);
......省略各种服务启动代码

在ActivityManagerService、PowerManagerService服务启动时有:

Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);

在PackageManagerService、InputManagerService、MediaRouterService、MountService、WindowManagerService等服务启动时有:

Watchdog.getInstance().addMonitor(this);
或
Watchdog.getInstance().addThread(mHandler)

服务启动完成之后运行watchdog

Watchdog.getInstance().start();
运行watchdog线程

watchdog.java:

public static Watchdog getInstance() {
        if (sWatchdog == null) {
            sWatchdog = new Watchdog();
        }

        return sWatchdog;
}

public void init(Context context, ActivityManagerService activity) {
        mResolver = context.getContentResolver();
        mActivity = activity;

        context.registerReceiver(new RebootRequestReceiver(),
                new IntentFilter(Intent.ACTION_REBOOT),
                android.Manifest.permission.REBOOT, null);

    }

构造函数中将主线程、i/o、fg、ui、display加入到mHandlerCheckers列表中
private Watchdog() {
        super("watchdog");
        // Initialize handler checkers for each common thread we want to check.  Note
        // that we are not currently checking the background thread, since it can
        // potentially hold longer running operations with no guarantees about the timeliness
        // of operations there.

        // The shared foreground thread is the main checker.  It is where we
        // will also dispatch monitor checks and do other work.
        mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
                "foreground thread", DEFAULT_TIMEOUT);
        mHandlerCheckers.add(mMonitorChecker);
        // Add checker for main thread.  We only do a quick check since there
        // can be UI running on the thread.
        mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
                "main thread", DEFAULT_TIMEOUT));
        // Add checker for shared UI thread.
        mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
                "ui thread", DEFAULT_TIMEOUT));
        // And also check IO thread.
        mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
                "i/o thread", DEFAULT_TIMEOUT));
        // A
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值