Handler阻塞和唤醒条件

106 篇文章 0 订阅
41 篇文章 1 订阅

作者:贾东风

阻塞条件

分析MessageQueue的next函数,发生阻塞只会存在以上两种情况

  1. 队列中消息个数为0,且没有可处理的IdleHandler,此时会一直阻塞
  2. 队列中消息个数不为0,但是队列头消息还未到执行时间,且没有IdleHandler要执行,此时会阻塞时间n,n表示队列头消息距离执行的时间

以上两种阻塞情况,mBlock都为true.只有这两种情况需要唤醒。

一言以蔽之,没有IdleHandle要执行,且队列中没有立即要可执行的消息时,会阻塞

唤醒条件

唤醒条件:

  1. 队列为空,或者队列头消息还未到执行时间,且当前消息待执行时间小于队列头消息,此时才需要唤醒。一言以蔽之,也就是当前消息插入到队列头部时,才需要唤醒。

  2. mBlocked为true,也就是没有Idle消息要处理,且当前队列头消息时同步屏障消息,且当前消息时异步消息,此时需要立即处理,因为该异步消息对响应及时性要求比较高。

boolean enqueueMessage(Message msg, long when) {
        ...

        synchronized (this) {
            ...

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            //队列为空,或者队列头消息还未到执行时间,且当前消息待执行时间小于队列头消息,此时才需要唤醒
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            // We can assume mPtr != 0 because mQuitting is false.
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值