在Handler的构造方法中,首先通过Looper.myLooper()方法获取当前线程的Looper对象,如果Looper对象为空,就抛出异常,说当前线程还没有调用Looper.prepare()方法。如果Looper不为空,Handler就会持有Looper的MessageQueue对象mQueue。

我们再看Looper.myLooper()和Looper.prepare()两个方法:

static final ThreadLocal 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));
 }//获取当前线程的Looper对象
 public static @Nullable Looper myLooper() {
 return sThreadLocal.get();
 }

这里有一个很关键的类:ThreadLocal,它一个线程内部的数据存储类,通过它存储的数据只有在它自己的线程才能获取到,其他线程是获取不到的。所以sThreadLocal.get()获取的就是当前线程的Looper对象。在Looper.prepare()方法中我们看到了如果当前线程已经有Looper对象,就会抛出异常,说一个线程只能创建一个Looper对象,所以Looper对象与自己所在的线程是相对应的。
再看Looper的构造方法:

private Looper(boolean quitAllowed) {
 mQueue = new MessageQueue(quitAllowed);
 mThread = Thread.currentThread();
 }

Looper的构造方法是私有的,外界不能直接创建Looper对象,只能通过Looper.prepare()方法创建对象并且通过Looper.myLooper()获取对象,这就保证了一个线程只能有一个Looper对象。Looper.prepare()不能调用两次。

在Looper的构造方法中会创建一个MessageQueue对象,这个就是负责存放消息的消息队列,也就是Handler所持有的mQueue 对象。它是由Looper创建和管理的。

看完了Handler、Looper和MessageQueue对象的创建,接着看消息的发送:

Handler发送消息的方法有很多,但无论你是send一个Message还是post一个Runnable;无论你是延时发送还是不延时发送,最终都会调用Handler的enqueueMessage()方法。

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
 //把this赋值给msg的target属性,this就是Handler对象。
 msg.target = this;
 if (mAsynchronous) {
 msg.setAsynchronous(true);
 }
 //把消息存放到MessageQueue
 return queue.enqueueMessage(msg, uptimeMillis);
 }

这里直接把消息存放到MessageQueue 就完事了。那么消息又是从哪里被取出来的呢?
Looper里有一个Looper.loop()方法,我们看一下它的源码。

public static void loop() {
final MessageQueue queue = me.mQueue;
 //一个死循环
 for (;😉 {
 //从MessageQueue中取出一条消息
 Message msg = queue.next();
 if (msg == null) {
 // No message indicates that the message queue is quitting.
 return;
 }
 //把消息交给Handler处理。
 msg.target.dispatchMessage(msg);
 }
 }

从上面的代码中我们看到loop()会开启一个死循环,不断地从MessageQueue中取出消息并交给Handler处理。在前面的enqueueMessage()方法中我们知道了msg.target就是发送消息的Handler对象。

这里有同学可能会有疑问:上面的代码中明明如果(msg == null),就退出方法,为什么我还说loop()里面是个死循环呢?这是因为MessageQueue的next()方法取出消息的时候,如果没有消息,next()方法会阻塞线程,直到MessageQueue有消息进来,然后取出消息并返回。所以queue.next()一般不会返回null,除非调用Looper的quit()或者quitSafely()方法结束消息轮询,queue.next()才会返回null,才会结束循环。

public void quit() {
 mQueue.quit(false);
 }
 public void quitSafely() {
 mQueue.quit(true);
 }

最后我们来看 一下消息的处理:Handler的dispatchMessage(msg)方法。

public void dispatchMessage(Message msg) {
 //如果Message有自己的callback,就由Message的callback处理
 if (msg.callback != null) {
 handleCallback(msg);
 } else {
 //如果Handler有自己的mCallback,就由Handler的mCallback处理
 if (mCallback != null) {
 if (mCallback.handleMessage(msg)) {
 return;
 }
 }
 //默认的处理消息的方法
 handleMessage(msg);
 }
 }

处理消息的方法有三个:

1、优先级最高的是Message自己的callback,这是一个Runnable对象,我们用Handler post一个Runnable的时候,其实就是把这个Runnable赋值个一个Message对象的callback,然后把Message对象发送给MessageQueue。
2、优先级第二的是Handler自己的mCallback,在我们创建一个Handler对象的使用可以传一个Handler.Callback对象给它并实现Callback里的handleMessage(msg)方法,作为我们的消息处理方法。
3、优先级最低的是Handler自己的handleMessage(msg)方法,这也是我们最常用的消息处理方法。

到这里我们的分析就结束了,现在你对Handler机制是不是有了深刻的认识呢。

不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊~