从MessageQueue的removeMessages来认识anr原理

前面接触了handler,MessageQueue,Looper,这里趁热打铁,通过ANR的原理继续来熟悉handler相关处理。

ANR检查调用超时,显然,需要有相应的检查,没有检查就没有所谓的超时,比如你上班早退了,会有人找你,你旷工,会有人找你,因为这些有监管,而如果你上班超时,到点还不走,却没有人找你,因为对这个没有监管。

ANR超时的处理很巧妙,开始事件的时候,向系统的MessageQueue发送一个表示超时的message,(使用postDelay即可实现),执行时间为超时的时刻,如果时间很快就完成了,就调用MessageQueue的removeMessages删除掉这个message,如果超时,这个超时message到点了就执行,通知系统发生超时。

这个设计是handler机制的经典应用,这里看看service超时的具体处理实现。

1. 埋雷,发送延时消息

private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) {
    ... 
    scheduleServiceTimeoutLocked(r.app);
}

void scheduleServiceTimeoutLocked(ProcessRecord proc) {
    if (proc.executingServices.size() == 0 || proc.thread == null) {
        return;
    }
    long now = SystemClock.uptimeMillis();
    Message msg = mAm.mHandler.obtainMessage(
            ActivityManagerService.SERVICE_TIMEOUT_MSG);
    msg.obj = proc;
    
    //当超时后仍没有remove该SERVICE_TIMEOUT_MSG消息,则执行service Timeout流程
    mAm.mHandler.sendMessageAtTime(msg,
        proc.execServicesFg ? (now+SERVICE_TIMEOUT) : (now+ SERVICE_BACKGROUND_TIMEOUT));
}

2.扫雷,任务及时完成后,调用removeMessages清理掉指定的message(根据message编码和对象来区分),

private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,
            boolean finishing) {
    ...
    if (r.executeNesting <= 0) {
        if (r.app != null) {
            r.app.execServicesFg = false;
            r.app.executingServices.remove(r);
            if (r.app.executingServices.size() == 0) {
                //当前服务所在进程中没有正在执行的service
                mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app);
        ...
    }
    ...
}

handler.java中的removeMessages

756    /**
757     * Remove any pending posts of messages with code 'what' and whose obj is
758     * 'object' that are in the message queue.  If <var>object</var> is null,
759     * all messages will be removed.
760     */
761    public final void removeMessages(int what, Object object) {
762        mQueue.removeMessages(this, what, object);
763    }

参考资料

https://www.jianshu.com/p/388166988cef

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值