Handler源码分析笔记

作者: oddly | 来源:发表于2022-03-05 17:59 被阅读0次

    Handler

    我们都知道Handler由MessageMessageQueueHandlerLooper组成,接下来我们带着问题,从源码中寻找 Handler 的具体流程与实现。

    问题

    • 消息是如何发送出去的?
    • 消息时如何被得到的?
    • 轮询器的启动在什么时候?
    • 轮询器与消息队列的绑定是如何建立的?
    • 如何确保总是能够在对应的线程中获取到正确的轮询器实例?
    • 对消息队列的操作,消费者和生产者,主线程与子线程如何进行通信?
    • Handler只能用于主线程UI更新?

    解析

    首先从轮询器的启动开始,所有的java程序都有一个main方法作为程序的入口,而Android中这个main方法在ActivityThread中

    public static void main(String[] args) {
            ...
    
            Looper.prepareMainLooper();
    
            ...
                
            ActivityThread thread = new ActivityThread();
            thread.attach(false, startSeq);
    
            if (sMainThreadHandler == null) {
                sMainThreadHandler = thread.getHandler();
            }
    
            if (false) {
                Looper.myLooper().setMessageLogging(new
                        LogPrinter(Log.DEBUG, "ActivityThread"));
            }
    
            // End of event ActivityThreadMain.
            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            Looper.loop();
    
            throw new RuntimeException("Main thread loop unexpectedly exited");
        }
    }
    

    在代码中我们可以看到 Looper.prepareMainLooper()和Looper.loop() 两个方法

    先从Looper.prepareMainLooper()进行分析

        private static Looper sMainLooper;  // guarded by Looper.class 保存Looper类
        public static void prepareMainLooper() {
            prepare(false);
            synchronized (Looper.class) {
                if (sMainLooper != null) {
                    throw new IllegalStateException("The main Looper has already been prepared.");
                }
                sMainLooper = myLooper();
            }
        }
    

    在其中prepareMainLooper,Looper将全局静态变量 sMainLooper 赋值和调用了 prepare() 方法

        // sThreadLocal.get() will return null unless you've called prepare().
        static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
        private static void prepare(boolean quitAllowed) {
            if (sThreadLocal.get() != null) {
                throw new RuntimeException("Only one Looper may be created per thread");
            }
            sThreadLocal.set(new Looper(quitAllowed));
        }
        //myLooper就是调用了sThreadLocal的get方法
        public static @Nullable Looper myLooper() {
            return sThreadLocal.get();
        }
    

    在 prepare 中调用了全局静态变量 sThreadLocal 的 set 方法

    那么ThreadLocal是什么呢?

    ThreadLocal本质是一个Map,不过其中的 key 值是线程 Thread ,它通过线程来存储和读取数据。正如其名,用来存储线程本地数据【避免其他线程读取或修改】。

    可就是说在此处,sThreadLocal 中存储了与主线程对应的 Looper 实例,只要是主线程中调用sThreadLocal 的get方法就能获取这个轮询器,若是其他子线程就获取不到这个轮询器

    然后就到了讲解 Looper.loop() ,也就是在这里开始死循环,使得主线程一直存活

    public static void loop() {
            final Looper me = myLooper();
            if (me == null) {
                throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
            }
            final MessageQueue queue = me.mQueue;
        
            ...
                
            for (;;) {
                Message msg = queue.next(); // might block
                if (msg == null) {
                    // No message indicates that the message queue is quitting.
                    return;
                }
    
                ...
    
                final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
                final long dispatchEnd;
                try {
                    msg.target.dispatchMessage(msg);
                    dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
                } finally {
                    if (traceTag != 0) {
                        Trace.traceEnd(traceTag);
                    }
                }
                ...
                /*
                * 经过上述步骤消息都未被处理,于是将其回收
                */
                msg.recycleUnchecked();
            }
        }
    

    在 loop 中我们主要看到 MessageQueue 、 msg.target.dispatchMessage(msg) 这三处地方

    我们先看 msg.target.dispatchMessage(msg) 之后,回来了解 MessageQueue

    public final class Message implements Parcelable {
        ...
        Handler target;
        Runnable callback;
        ...
    }
    

    从 Message 中了解 target 就是 Handler,而 callback 是用户设置的回调任务,只要不设置这个 callback 就会进入 handleMessage

    public class Handler {
        ...
        public void dispatchMessage(Message msg) {
            if (msg.callback != null) {
                handleCallback(msg);
            } else {
                if (mCallback != null) {
                    if (mCallback.handleMessage(msg)) {
                        return;
                    }
                }
                //这里调用了用户自定义的handleMessage去处理业务逻辑
                handleMessage(msg);
            }
        }
        ...
    }
    

    这里我们看到了消息时如何被得回应的,那么我们只需要知道 Message 是怎么发送的和在什么时候给 target 赋值确保Handler对象不出错。

    Message 是如何发送的?这个问题想必都知道答案

    public class Handler {
        final Looper mLooper;
        final MessageQueue mQueue;
        ...
        public final boolean sendMessage(Message msg)
        {
            return sendMessageDelayed(msg, 0);
        }
        
        public final boolean sendMessageDelayed(Message msg, long delayMillis)
        {
            if (delayMillis < 0) {
                delayMillis = 0;
            }
            return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
        }
        //最终进入该方法,此处就出现了 MessageQueue
        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
            MessageQueue queue = mQueue;
            if (queue == null) {
                RuntimeException e = new RuntimeException(
                        this + " sendMessageAtTime() called with no mQueue");
                Log.w("Looper", e.getMessage(), e);
                return false;
            }
            return enqueueMessage(queue, msg, uptimeMillis);
        }
        
        private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
            msg.target = this;//在此处Handler给Message的target赋值
            if (mAsynchronous) {
                msg.setAsynchronous(true);
            }
            return queue.enqueueMessage(msg, uptimeMillis);
        }
        ...
    }
    

    经过一层一层的调用,最终Handler调用了 enqueueMessage 方法,将 Message 放入它的全局变量MessageQueue中,且将Message的target赋值为this【发送消息的Handler本身】

    那么我们知道了发送的消息最终去向【MessageQueue】,那么Handler中的MessageQueue又是什么时候初始化的?

        //我们平常使用的Handler无参构造函数最终都会到这里
        public Handler(Callback callback, boolean async) {
            if (FIND_POTENTIAL_LEAKS) {
                final Class<? extends Handler> klass = getClass();
                if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                        (klass.getModifiers() & Modifier.STATIC) == 0) {
                    Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                        klass.getCanonicalName());
                }
            }
            //此处就是为什么子线程不能创建Handler的原因
            mLooper = Looper.myLooper();
            if (mLooper == null) {
                throw new RuntimeException(
                    "Can't create handler inside thread " + Thread.currentThread()
                            + " that has not called Looper.prepare()");
            }
            mQueue = mLooper.mQueue;
            mCallback = callback;
            mAsynchronous = async;
        }
    

    mLooper = Looper.myLooper() 之前我们分析过 myLooper 就是调用了sThreadLocal的get方法,此处只有主线程才有对应的 Looper 实例存在,这也就是为什么子线程中不能用无参构造方法实例化Handler,如果创建会报下列错误提示

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    

    当然也不是没有解决方案,不是没有 Looper 报错吗,那就给呗!!!解决方案

    继续向下,我们看到 mQueue = mLooper.mQueue ,也就是说 MessageQueue 是从Looper中获取的

    public final class Looper {
        private static Looper sMainLooper;  // guarded by Looper.class
        final MessageQueue mQueue;
        final Thread mThread;
        ...
        private Looper(boolean quitAllowed) {
            mQueue = new MessageQueue(quitAllowed);
            mThread = Thread.currentThread();
        }
        ...
    }
    

    嗯,MessageQueue 对象在私有构造函数中就已经实例化了,那么还记得什么时候调用了Looper的构造函数吗?

    绕了一圈终于到了讲解 MessageQueue 了

    public final class MessageQueue {
        Message mMessages;//
        ...
        boolean enqueueMessage(Message msg, long when) {
            ...
            synchronized (this) {
                if (mQuitting) {//是否退出,只有调用了quit方法后是true
                    IllegalStateException e = new IllegalStateException(
                            msg.target + " sending message to a Handler on a dead thread");
                    Log.w(TAG, e.getMessage(), e);
                    msg.recycle();
                    return false;
                }
                msg.markInUse();
                //SystemClock.uptimeMillis() + delayMillis
                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;
        }
    }
    

    此时你可能有些疑惑,不是队列吗,那么为什么没有数组或者List呢?因为 MessageQueue 采用链表的方式实现队列。

    public final class Message implements Parcelable {
        ...
        // sometimes we store linked lists of these things
        /*package*/ Message next;
        //最大Message池为50个
        private static final int MAX_POOL_SIZE = 50;
        ...
    }
    

    我们将 if 语句分开看

    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;
    }
    

    首先Message p被赋值为全局变量 mMessages【我称其为“即将发送Message”】如果

    • “即将发送Message”是null
    • msg 的 when 是0 【when = SystemClock.uptimeMillis() + delayMillis】
    • msg 的 when 小于“即将发送Message”的 when 【 msg 发送事件的等待时间 小于 mMessages】

    其中一个成立,将 msg 的 next 指向 p ,在将 mMessages 赋值为 msg【实际上就是将msg放在队列最前面】

    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;
    }
    

    到else,基本就是 mMessages有值且 msg 发送事件的等待时间 大于 mMessages,于是就把 msg 放入链表中,通过循环将 msg 放入链表的合适位置,确保队列中的元素等待时间是递增的

    既然已经讲了存放,那么就该到读取了

        Message next() {
            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();
                }
    
                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;
                    if (msg != null && msg.target == 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;
                    }
    
                    if (mQuitting) {
                        dispose();
                        return null;
                    }
    
                    // If first time idle, then get the number of idlers to run.
                    // Idle handles only run if the queue is empty or if the first message
                    // in the queue (possibly a barrier) is due to be handled in the future.
                    if (pendingIdleHandlerCount < 0
                            && (mMessages == null || now < mMessages.when)) {
                        pendingIdleHandlerCount = mIdleHandlers.size();
                    }
                    if (pendingIdleHandlerCount <= 0) {
                        // No idle handlers to run.  Loop and wait some more.
                        //基本不会进入这个if
                        mBlocked = true;
                        continue;
                    }
    
                    if (mPendingIdleHandlers == null) {
                        mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                    }
                    mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
                }
    
                // Run the idle handlers.
                // We only ever reach this code block during the first iteration.
                for (int i = 0; i < pendingIdleHandlerCount; i++) {
                    final IdleHandler idler = mPendingIdleHandlers[i];
                    mPendingIdleHandlers[i] = null; // release the reference to the handler
    
                    boolean keep = false;
                    try {
                        keep = idler.queueIdle();
                    } catch (Throwable t) {
                        Log.wtf(TAG, "IdleHandler threw exception", t);
                    }
    
                    if (!keep) {
                        synchronized (this) {
                            mIdleHandlers.remove(idler);
                        }
                    }
                }
    
                // Reset the idle handler count to 0 so we do not run them again.
                pendingIdleHandlerCount = 0;
    
                // While calling an idle handler, a new message could have been delivered
                // so go back and look again for a pending message without waiting.
                nextPollTimeoutMillis = 0;
            }
        }
    

    此处我们看到了MessageQueue调用了 native 方法【就是java调用了c方法】,其具体的实现在 /frameworks/base/core/jni/android_os_MessageQueue.cpp 点击此处查看

    //可以理解为阻塞,ptr相当于Message指针,timeoutMillis阻塞时间
    private native void nativePollOnce(long ptr, int timeoutMillis); 
    //唤醒,之前在enqueueMessage中有调用该方法唤醒
    private native static void nativeWake(long ptr);
    

    之后我们主要看 next 对 return 有关的部分

    final long now = SystemClock.uptimeMillis();
    Message prevMsg = null;
    Message msg = mMessages;
    //msg 对应的 Handler 被销毁,于是取出队列中的下一个 Message
    if (msg != null && msg.target == null) {
        do {
            prevMsg = msg;
            msg = msg.next;
        } while (msg != null && !msg.isAsynchronous());
    }
    if (msg != null) {
        //还没有到 msg 的发送时间,需要阻塞等待
        if (now < msg.when) {
            // 下一条消息未准备好。设置一个超时时间,当它准备好时唤醒。
            nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
        } else {
            // 到 msg 的发送时间
            mBlocked = false;
            //将 mMessages 变为 msg 的 next
            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 {
        // nextPollTimeoutMillis = -1 代表nativePollOnce将一直阻塞直到被唤醒
        nextPollTimeoutMillis = -1;
    }
    

    到了这里Handler的源码分析就结束了,可以再回去看看Handler的流程图是不是会有新的感悟

    相关文章

      网友评论

        本文标题:Handler源码分析笔记

        本文链接:https://www.haomeiwen.com/subject/mojbrrtx.html