Android Handler源码分析核心架构

前言:

  对于一个Android研发而言,亲身体会就是不管在平时开发或者面试的时候,Handler消息机制毋庸置疑都是一个必备的知识点,所以这边留一份个人笔记,如有分析不对的地方,还望指出!

目录:

  1、如何分析Handler源码

  2、源码大致流程:消息的入队与出队

  3、从大致流程进入细化分析

    3.1、Handler、Looper、MessageQueue三者之间的关系

    3.2、Handler、Looper、MessageQueue之间的协作

总结图1:Handler在子线程中发送消息,消息会被添加到MessageQueue消息队列中,再来由Handler所处的当前线程的Looper来不断的轮询MessageQueue以获取出队消息,最后调用dispatchMessage进行消息传递给handleMessage进行处理

 

 

1、如何分析源码

  众所皆知的Android源码的有很多,涉及到一个类或者多个类,一个类中又有很多代码,所以这边最简单的分析方式就是回归到Handler的使用中来,也就是如何使用Handler

  1.1、实例一个Handler对象(主线程)

  1.2、在子线程中使用Handler发送一个消息,如:handler.sendEmptyMessage(1)

  1.3、消息发送出之后(执行1.2步骤),消息最终会被转发到我们new出来的Handler中的handleMessage方法进行处理(子线程消息发送到主线程中处理)

  以上3个步骤即为我们对Handler的基本使用方式,所以,我们可以以发送消息的时机,作为源码分析的切入点,并留下一个疑问:

  问题1:子线程发送的消息为什么是在主线程中接收的呢?

 2、源码大致流程:消息的入队与出队

  2.1、消息发送:sendMessage(Message msg) \ sendEmptyMessage(int what) \ postDelayed(Runnable r, long delayMillis) 等等

  2.2、消息及发送时间处理:sendMessageAtTime(Message msg, long uptimeMillis)

  2.3、消息队列添加:enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis)

  2.4、消息出队:到这里,既然有消息添加到队列中的流程,而且我们最终都会获得相应的消息返回,那么消息是如何出队的呢?带着这个疑问,我们最终在MessageQueue 消息队列中找到一个函数名称为 next() 的函数。

  问题2:这个next()函数 是在什么时候调用的呢?

  在Handler源码上,以消息发送作为分析切入点来查看,如2.1罗列的几种消息发送方式,我们都可以很清楚的发现,消息都是调用了sendMessageDelayed(Message msg, long delayMillis),最终调用到sendMessageAtTime(Message msg, long uptimeMillis),然后在该方法里面调用了enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis),到这里,不管从方法名称还是局部变量的名称来看,这边都出现了一个队列的信息,所以可以知道Handler的消息发送最终是在sendMessageAtTime里面调用了MessageQueue.enqueueMessage()对消息进行队列添加,然后调用了MessageQueue.next()进行消息轮询并返回Message结果。

3、从大致流程进入细化分析

  3.1、Handler、Looper、MessageQueue三者之间的 关系图2 如下:

  

  在分析到第2步的sendMessageAtTime结束时,我们这边引出了一个消息队列的内容:MessageQueue queue = mQueue

 1     /**
 2      * Enqueue a message at the front of the message queue, to be processed on
 3      * the next iteration of the message loop.  You will receive it in
 4      * {@link #handleMessage}, in the thread attached to this handler.
 5      * <b>This method is only for use in very special circumstances -- it
 6      * can easily starve the message queue, cause ordering problems, or have
 7      * other unexpected side-effects.</b>
 8      *  
 9      * @return Returns true if the message was successfully placed in to the 
10      *         message queue.  Returns false on failure, usually because the
11      *         looper processing the message queue is exiting.
12      */
13     public final boolean sendMessageAtFrontOfQueue(Message msg) {
14         MessageQueue queue = mQueue;
15         if (queue == null) {
16             RuntimeException e = new RuntimeException(
17                 this + " sendMessageAtTime() called with no mQueue");
18             Log.w("Looper", e.getMessage(), e);
19             return false;
20         }
21         return enqueueMessage(queue, msg, 0);
22     }
23 
24     private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
25         msg.target = this;
26         if (mAsynchronous) {
27             msg.setAsynchronous(true);
28         }
29         return queue.enqueueMessage(msg, uptimeMillis);
30     }

  问题:mQueue是什么东西?这个mQueue是怎么来的?所以我们在Handler的构造方法中找到了它的初始化位置

 1     /**
 2      * Use the {@link Looper} for the current thread with the specified callback interface
 3      * and set whether the handler should be asynchronous.
 4      *
 5      * Handlers are synchronous by default unless this constructor is used to make
 6      * one that is strictly asynchronous.
 7      *
 8      * Asynchronous messages represent interrupts or events that do not require global ordering
 9      * with respect to synchronous messages.  Asynchronous messages are not subject to
10      * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
11      *
12      * @param callback The callback interface in which to handle messages, or null.
13      * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
14      * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
15      *
16      * @hide
17      */
18     public Handler(Callback callback, boolean async) {
19         if (FIND_POTENTIAL_LEAKS) {
20             final Class<? extends Handler> klass = getClass();
21             if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
22                     (klass.getModifiers() & Modifier.STATIC) == 0) {
23                 Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
24                     klass.getCanonicalName());
25             }
26         }
27 
28         mLooper = Looper.myLooper();
29         if (mLooper == null) {
30             throw new RuntimeException(
31                 "Can't create handler inside thread that has not called Looper.prepare()");
32         }
33         mQueue = mLooper.mQueue;
34         mCallback = callback;
35         mAsynchronous = async;
36     }

  到此,从上面的两段源码,且带着第2点中,next()被调用的时机问题,我们引出了os/Handler中的两个成员变量

    final Looper mLooper;
    final MessageQueue mQueue;

   MessageQueue 对象是从Looper中获得的,也就是说mQueue是在Looper中实例化的,所以很明显,Handler中的消息队列MessageQueue 是从轮询器Looper中获得的。

 

  那么问题来了:为什么消息队列要在轮询器中进行实例化,请看以下源码

 1     /**
 2      * Return the {@link MessageQueue} object associated with the current
 3      * thread.  This must be called from a thread running a Looper, or a
 4      * NullPointerException will be thrown.
 5      */
 6     public static @NonNull MessageQueue myQueue() {
 7         return myLooper().mQueue;
 8     }
 9 
10     private Looper(boolean quitAllowed) {
11         mQueue = new MessageQueue(quitAllowed);
12         mThread = Thread.currentThread();
13     }

  MessageQueue在Looper中进行实例化,也就是说一个Looper就有一个MessageQueue,属于绑定关系,从而得出一个Looper只能轮询一个消息队列

  所以可得出如关系图2中Handler、Looper、MessageQueue三者的关系:Handler中持有Looper和MessageQueue,Looper中持有MessageQueue,而且Handler中的MessageQueue来自于Looper中的MessageQueue。

  Handler是使用时通过New实例化出来的,MessageQueue是在Looper中进行实例的,那么这个Looper是如何实例化的?所以这边我们将引出 ActivityThread。而ActivityThread是什么东西呢?这边就稍微介绍一下:

  安卓应用程序作为一个控制类程序,跟Java程序类似,都是有一个入口的,而这个入口就是ActivityThread的main函数:

 1 public static void main(String[] args) {
 2         Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
 3         SamplingProfilerIntegration.start();
 4 
 5         // CloseGuard defaults to true and can be quite spammy.  We
 6         // disable it here, but selectively enable it later (via
 7         // StrictMode) on debug builds, but using DropBox, not logs.
 8         CloseGuard.setEnabled(false);
 9 
10         Environment.initForCurrentUser();
11 
12         // Set the reporter for event logging in libcore
13         EventLogger.setReporter(new EventLoggingReporter());
14 
15         // Make sure TrustedCertificateStore looks in the right place for CA certificates
16         final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
17         TrustedCertificateStore.setDefaultUserDirectory(configDir);
18 
19         Process.setArgV0("<pre-initialized>");
20 
21         Looper.prepareMainLooper();
22 
23         ActivityThread thread = new ActivityThread();
24         thread.attach(false);
25 
26         if (sMainThreadHandler == null) {
27             sMainThreadHandler = thread.getHandler();
28         }
29 
30         if (false) {
31             Looper.myLooper().setMessageLogging(new
32                     LogPrinter(Log.DEBUG, "ActivityThread"));
33         }
34 
35         // End of event ActivityThreadMain.
36         Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
37         Looper.loop();
38 
39         throw new RuntimeException("Main thread loop unexpectedly exited");
40     }

  以上的main函数代码中,我们还需要意识到两个问题:

  1.我们之所以可以在Activity用Handler handler=new Handler()直接创建出来就默认绑定到主线程了,是因为上面的代码为我们做了绑定主线程的Looper的事情,

  2.主线程的Looper是不能在程序中调用退出的,最后一句代码看到没,如果调用的话,就会抛出异常,退出主线程的循环是框架层在调用退出应用程序的时候才调用的

 1     /**
 2      * Initialize the current thread as a looper, marking it as an
 3      * application's main looper. The main looper for your application
 4      * is created by the Android environment, so you should never need
 5      * to call this function yourself.  See also: {@link #prepare()}
 6      */
 7     public static void prepareMainLooper() {
 8         prepare(false);
 9         synchronized (Looper.class) {
10             if (sMainLooper != null) {
11                 throw new IllegalStateException("The main Looper has already been prepared.");
12             }
13             sMainLooper = myLooper();
14         }
15     }
16 
17 
18     private static void prepare(boolean quitAllowed) {
19         if (sThreadLocal.get() != null) {
20             throw new RuntimeException("Only one Looper may be created per thread");
21         }
22         sThreadLocal.set(new Looper(quitAllowed));
23     }

  在ActivityThread的main中调用了 Looper.prepareMainLooper() -> prepare(false) -> sThreadLocal.set(new Looper(quitAllowed)), 这么一来,是不是执行到了上面的Looper构造函数中了?到这里,细心的人会发现这么一个问题:

  问题3:Looper被实例化出来之后并没有直接返回,而是被set到了ThreadLocal中?

  Handler与Looper是成对出现的,一个子线程发送消息,一个主线程接收消息,那么这边就涉及到了多线程,线程之间的通讯,是需要保证数据的安全,即数据隔离,所以使用到了ThreadLocal进行线程管理:如A线程在获取数据时只能获取A线程所控制的数据,而不能去获取到B线程中对应的数据,否则就会引起数据不同步,比如A线程数据被B线程数据所覆盖之类的问题,同时也验证了一个线程只能关联一个Looper对象

  所以问题3解决了。最后这个main的结尾,调用了 Looper.loop(); 进行轮询消息队列! 是不是很完美了?

  3.2、Handler、Looper、MessageQueue之间的协作

  通过前面的源码分析,我们已经知道了消息是如果添加到消息队列了。我们再来看消息的出队分析。

  以下在Looper轮询器中的loop()中我们看到这样一句代码:Message msg = queue.next(); // might block

 1 /**
 2      * Run the message queue in this thread. Be sure to call
 3      * {@link #quit()} to end the loop.
 4      */
 5     public static void loop() {
 6         final Looper me = myLooper();
 7         if (me == null) {
 8             throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
 9         }
10         final MessageQueue queue = me.mQueue;
11 
12         // Make sure the identity of this thread is that of the local process,
13         // and keep track of what that identity token actually is.
14         Binder.clearCallingIdentity();
15         final long ident = Binder.clearCallingIdentity();
16 
17         for (;;) {
18             Message msg = queue.next(); // might block
19             if (msg == null) {
20                 // No message indicates that the message queue is quitting.
21                 return;
22             }
23 
24             // This must be in a local variable, in case a UI event sets the logger
25             final Printer logging = me.mLogging;
26             if (logging != null) {
27                 logging.println(">>>>> Dispatching to " + msg.target + " " +
28                         msg.callback + ": " + msg.what);
29             }
30 
31             final long traceTag = me.mTraceTag;
32             if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
33                 Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
34             }
35             try {
36                 msg.target.dispatchMessage(msg);
37             } finally {
38                 if (traceTag != 0) {
39                     Trace.traceEnd(traceTag);
40                 }
41             }
42 
43             if (logging != null) {
44                 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
45             }
46 
47             // Make sure that during the course of dispatching the
48             // identity of the thread wasn't corrupted.
49             final long newIdent = Binder.clearCallingIdentity();
50             if (ident != newIdent) {
51                 Log.wtf(TAG, "Thread identity changed from 0x"
52                         + Long.toHexString(ident) + " to 0x"
53                         + Long.toHexString(newIdent) + " while dispatching to "
54                         + msg.target.getClass().getName() + " "
55                         + msg.callback + " what=" + msg.what);
56             }
57 
58             msg.recycleUnchecked();
59         }
60     }

  所以通过以上代码,我们可以知道消息的出队,是在Looper这个轮询器中的loop()函数通过死循环的方式:for (;;),不断的通过队列的next()方法中拿到消息:queue.next(),并且如果队列消息为null了,就跳出该轮询。所以问题2是不是已经解决了?

   在出队过程中,也就是MessageQueue消息队列中的next()函数中,我们可以知道next()返回的是一个Message消息对象,从函数注释上来看:当轮询器 loop 轮询的时候会返回一条消息且从代码for (;;)循环的代码中可以看出,是在这里不断的拿到消息队列并返回下一条消息,到这里,我们需要注意的是因为这个消息是可以循环使用的,而且我们可以看到这样一个native函数调用:nativePollOnce(ptr, nextPollTimeoutMillis);所以我们可以得出消息的循环使用内存是通过C++来维护完成的(这边因为对native没有深入研究,所以pass这块!)

  1 Message next() {
  2         // Return here if the message loop has already quit and been disposed.
  3         // This can happen if the application tries to restart a looper after quit
  4         // which is not supported.
  5         final long ptr = mPtr;
  6         if (ptr == 0) {
  7             return null;
  8         }
  9 
 10         int pendingIdleHandlerCount = -1; // -1 only during first iteration
 11         int nextPollTimeoutMillis = 0;
 12         for (;;) {
 13             if (nextPollTimeoutMillis != 0) {
 14                 Binder.flushPendingCommands();
 15             }
 16 
 17             nativePollOnce(ptr, nextPollTimeoutMillis);
 18 
 19             synchronized (this) {
 20                 // Try to retrieve the next message.  Return if found.
 21                 final long now = SystemClock.uptimeMillis();
 22                 Message prevMsg = null;
 23                 Message msg = mMessages;
 24                 if (msg != null && msg.target == null) {
 25                     // Stalled by a barrier.  Find the next asynchronous message in the queue.
 26                     do {
 27                         prevMsg = msg;
 28                         msg = msg.next;
 29                     } while (msg != null && !msg.isAsynchronous());
 30                 }
 31                 if (msg != null) {
 32                     if (now < msg.when) {
 33                         // Next message is not ready.  Set a timeout to wake up when it is ready.
 34                         nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
 35                     } else {
 36                         // Got a message.
 37                         mBlocked = false;
 38                         if (prevMsg != null) {
 39                             prevMsg.next = msg.next;
 40                         } else {
 41                             mMessages = msg.next;
 42                         }
 43                         msg.next = null;
 44                         if (DEBUG) Log.v(TAG, "Returning message: " + msg);
 45                         msg.markInUse();
 46                         return msg;
 47                     }
 48                 } else {
 49                     // No more messages.
 50                     nextPollTimeoutMillis = -1;
 51                 }
 52 
 53                 // Process the quit message now that all pending messages have been handled.
 54                 if (mQuitting) {
 55                     dispose();
 56                     return null;
 57                 }
 58 
 59                 // If first time idle, then get the number of idlers to run.
 60                 // Idle handles only run if the queue is empty or if the first message
 61                 // in the queue (possibly a barrier) is due to be handled in the future.
 62                 if (pendingIdleHandlerCount < 0
 63                         && (mMessages == null || now < mMessages.when)) {
 64                     pendingIdleHandlerCount = mIdleHandlers.size();
 65                 }
 66                 if (pendingIdleHandlerCount <= 0) {
 67                     // No idle handlers to run.  Loop and wait some more.
 68                     mBlocked = true;
 69                     continue;
 70                 }
 71 
 72                 if (mPendingIdleHandlers == null) {
 73                     mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
 74                 }
 75                 mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
 76             }
 77 
 78             // Run the idle handlers.
 79             // We only ever reach this code block during the first iteration.
 80             for (int i = 0; i < pendingIdleHandlerCount; i++) {
 81                 final IdleHandler idler = mPendingIdleHandlers[i];
 82                 mPendingIdleHandlers[i] = null; // release the reference to the handler
 83 
 84                 boolean keep = false;
 85                 try {
 86                     keep = idler.queueIdle();
 87                 } catch (Throwable t) {
 88                     Log.wtf(TAG, "IdleHandler threw exception", t);
 89                 }
 90 
 91                 if (!keep) {
 92                     synchronized (this) {
 93                         mIdleHandlers.remove(idler);
 94                     }
 95                 }
 96             }
 97 
 98             // Reset the idle handler count to 0 so we do not run them again.
 99             pendingIdleHandlerCount = 0;
100 
101             // While calling an idle handler, a new message could have been delivered
102             // so go back and look again for a pending message without waiting.
103             nextPollTimeoutMillis = 0;
104         }
105     }

   到这边,如果所有的分析及源码查看都看懂的话,我们就已经掌握了在整个Handler消息机制中,是如何从一个消息的发送,进行了怎么样的世界环游,最终如何回到了Handler的handleMessage中的!

  分析到这里为止,如果还没蒙圈的人会发现,我们在前面提出过的几个问题都解决了,那么问题1呢?

  子线程发送的消息为什么是在主线程中接收的呢?

  其实我们在前面也已经有提及到了该问题,就是为什么在ActivityThread的main中实例化的Looper对象是被set到了ThreadLocal中。

  在java中,main是不是主线程呢?不需要解释了吧。看代码:当前线程中Looper的获取方式

1     /**
2      * Return the Looper object associated with the current thread.  Returns
3      * null if the calling thread is not associated with a Looper.
4      */
5     public static @Nullable Looper myLooper() {
6         return sThreadLocal.get();
7     }

  程序一开始在ActivityThread在执行main函数时实例化Looper,然后保存到了ThreadLocal中。而我们在主线程中new了一个Handler,那么Handler默认对应的Looper就是主线程的Looper:通过以上代码,从ThreadLocal管理中获取出当前线程(主线程)对应的Looper对象,所以对应的Looper自然也是主线程的Looper,明白了吗?

  所以主线程的Looper在轮询出消息队列MessageQueue中的消息时,就是出于主线程中,这样问题1是不是就清楚了。

   

posted @ 2017-06-28 14:23  itsdf07  阅读(2251)  评论(0编辑  收藏  举报