谈谈我对MessageQueue中的“同步屏障”的理解

同步屏障是什么

同步屏障 就是一个空的消息(Message ),该Message 的 target 为 null,而同步消息的target就是 Handler,这是因为 Handler 发送消息的时候都会调用如下方法:


```java
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this; //this 指的就是Handler对象
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }
为什么说 **同步屏障**是一个空的消息呢?来看看这个消息是怎么形成的就知道了。

# MessageQueue中同步屏障是怎么实现的
代码如下:

```java
 /**
     * Posts a synchronization barrier to the Looper's message queue.
    
     *
     * @hide
     */
    public int postSyncBarrier() {
        return postSyncBarrier(SystemClock.uptimeMillis());
    }

该方法是hide的,业务代码中不能直接使用,可以通过反射方式进行调用。调用该方法,会在消息队列中插入一个空msg。该方法会返回一个 token,需要根据这个 token 来删除这个 同步屏障,为什么要删除下文接着分析;先看看 postSyncBarrier(long when) 的实现;

private int postSyncBarrier(long when) {
        // Enqueue a new sync barrier token.
        // We don't need to wake the queue because the purpose of a barrier is to stall it.
        synchronized (this) {
        //这个token 就是用来删除的 同步屏障 的
            final int token = mNextBarrierToken++; 
            //从全局消息池中获取一个消息,这样做的目的是避免分配更多的内存,从消息池中获取的消息 target 已经置为 null 了。
            final Message msg = Message.obtain();
            msg.markInUse();//标记该消息正在使用
            msg.when = when;
            msg.arg1 = token;

            Message prev = null;
            Message p = mMessages;
            //按照消息的时间来把这条消息插入链表中,在我看来几乎是插入到表头,因为 when 表示的是系统开机到现在的时间,肯定比其他延时消息的时间小,这个自己理解下;下面的都是链表的操作啦,so easy
            if (when != 0) {
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
            }
            if (prev != null) { // invariant: p == prev.next
                msg.next = p;
                prev.next = msg;
            } else {
                msg.next = p;
                mMessages = msg;
            }
            return token;
        }
    }

以上我们就在消息队列中插入了一个 同步屏障,同步屏障有啥子好处呢?
这个就得先了解下什么是同步消息和异步消息了。

同步消息和异步消息

我们平时使用Hander 来发送的消息大多数都是同步消息

 Message message = new Message();
        message.what = 11;
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 11){

                }
            }
        };
        handler.sendMessage(message);
 public Handler() {
 // 这里的false就表示这是一个同步消息, 是用来给变量 mAsynchronous 赋值的,上面的 Handler 的 enqueueMessage 中就判断了 mAsynchronous 为 true, 就 msg.setAsynchronous(true);
        this(null, false);
    }

从上面的代码我们就知道可以通过 Message 的 setAsynchronous 来设置是否为异步消息,另外一种方式就是 如下调用

public Handler(boolean async) {
        this(null, async);
    }

但是这个方法是隐藏的,只用反射调用。
同步消息和异步消息有什么区别呢? 下文会分析。

同步屏障有什么用处?

1.异步消息 必须结合 同步屏障 使用,也就是说就算有异步消息,但是没有设置 同步屏障,也是没效果的
2. 如果使用了同步屏障,那么异步消息将会优先执行,如果没有删除 同步屏障,那么同步消息将不会得到执行。

下面来分析下原因:

Handler 是通过 Looper.loop()来源源不断的处理 消息,loop 调用的是 MessageQueue 的 next()方法。所以我们看看 next方法,假设当前的消息队列中存在如下图所示的消息:
在这里插入图片描述
上面有五个消息,我们分别用 1到5 来表示各个消息。

看看next 代码是怎么实现的:

 Message next() {
        // Return here if the message loop has already quit and been disposed.
        // This can happen if the application tries to restart a looper after quit
        // which is not supported.
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }

// nextPollTimeoutMillis = 0, 功能类似于 Object.wait(0), 但是要复杂和强大的多,这里不细说, nativePollOnce(ptr, -1)表示要一直等到需要 nativeWake唤醒
            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                //msg.target == null,表示这是一个同步屏障,我们上图的消息1 就是一个同步屏障,
                if (msg != null && msg.target == null) {// 
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    //通过循环找到下一个异步消息,如果没有异步消息,那么就会一个个循环,知道 msg = null;
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                //根据时间来判断消息是否到时见了,没到则阻塞等待剩余时间
                if (msg != null) {
                    if (now < msg.when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                    } else {
                        // Got a message.
                        
                        mBlocked = false;
                        //有异步消息,则将异步消息的链断开掉这个异步消息
                        if (prevMsg != null) {
                            prevMsg.next = msg.next;
                        } else {//不是异步消息,则头结点执行下一个消息
                            mMessages = msg.next;
                        }
                        msg.next = null;
                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    //没有消息了,等有消息的时候来唤醒;或者有同步屏障且没有异步消息,nextPollTimeoutMillis 置为 -1 ,表示要无线等待下去,需要删除同步屏障来唤醒。这也就是为什么 没有删除同步屏障,剩下的同步消息就无法执行的原因。
                    nextPollTimeoutMillis = -1;
                }
                .....省略部分代码
           }

我们分析下上面图中的消息;

消息1 是同步屏障, 所以 if (msg != null && msg.target == null) 成立,走到里面的循环,
prevMsg = msg; // prevMsg 指向第一个消息
msg = msg.next; // msg 指向下一条消息;

由于消息2 是异步消息 !msg.isAsynchronous() = false,

所以循环结束, 由于 msg 指向指向了 消息2 即异步消息,所以if (msg != null) 成立, 走到下面这里:

                        mBlocked = false;
                        if (prevMsg != null) {  //1 
                            prevMsg.next = msg.next;
                        } else {
                            mMessages = msg.next;
                        }
                        msg.next = null;
                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;

prevMsg 指向了消息1 , 语句1 成立,执行 prevMsg.next = msg.next 就是让消息1 的执行消息3了,msg.next = null; 就是断开 指向异步消息的 msg;然后返回这条消息;

然后下次 再次 调用next获取消息的时候,由于以及没有异步消息了,但是同步屏障没有移除,所以 if (msg != null && msg.target == null) 成立,然后走do …while 直到 msg = null; 由于 msg = null ,所以走到 nextPollTimeoutMillis = -1; 接着for循环 调用 nativePollOnce(ptr, -1);
一直阻塞,直到移除 同步屏障 时 调用 nativeWake 来唤醒。

下面看看 移除同步屏障 是怎么实现的。

同步屏障怎么删除

 public void removeSyncBarrier(int token) {
        // Remove a sync barrier token from the queue.
        // If the queue is no longer stalled by a barrier then wake it.
        synchronized (this) {
            Message prev = null;
            Message p = mMessages;//指向消息队列的第一条消息
            //循环找到 对应的 同步屏障
            while (p != null && (p.target != null || p.arg1 != token)) {
                prev = p;
                p = p.next;
            }
            //p 为空 表示 该同步屏障已被移除或该同步屏障就不存在
            if (p == null) {
                throw new IllegalStateException("The specified message queue synchronization "
                        + " barrier token has not been posted or has already been removed.");
            }
            final boolean needWake;
            //下面就是 结点的移除操作 和 唤醒操作啦
            if (prev != null) {
                prev.next = p.next;
                needWake = false;
            } else {
                mMessages = p.next;
                needWake = mMessages == null || mMessages.target != null;
            }
            p.recycleUnchecked();

            // If the loop is quitting then it is already awake.
            // We can assume mPtr != 0 when mQuitting is false.
            if (needWake && !mQuitting) {
                nativeWake(mPtr); //相当于 notify 唤醒 调用了 nativePollOnce(ptr, nextPollTimeoutMillis);
            }
        }
    }

总结下:

  1. 异步消息 要结合 同步屏障使用才有效果,不然就和同步消息是一样的
  2. 使用同步屏障 后 能确保异步消息 能优先于 同步消息 执行
  3. 使用同步屏障后要删除同步屏障,否则消息队列中的同步消息将得不到执行。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值