|
| 1 | +> 老司机们都知道,Android的线程间通信就靠Handler、Looper、Message、MessageQueue这四个麻瓜兄弟了,那么,他们是怎么运作的呢?下面做一个基于主要源代码的大学生水平的分析。 [原文链接](http://anangryant.leanote.com/post/Handler%E3%80%81Looper%E3%80%81Message%E3%80%81MessageQueue%E5%88%86%E6%9E%90) |
| 2 | +
|
| 3 | +##Looper(先分析这个是因为能够引出四者的关系) |
| 4 | +在Looper中,维持一个`Thread`对象以及`MessageQueue`,通过Looper的构造函数我们可以知道: |
| 5 | +``` |
| 6 | + private Looper(boolean quitAllowed) { |
| 7 | + mQueue = new MessageQueue(quitAllowed);//传入的参数代表这个Queue是否能够被退出 |
| 8 | + mThread = Thread.currentThread(); |
| 9 | + } |
| 10 | +``` |
| 11 | +`Looper`在构造函数里干了两件事情: |
| 12 | +1. 将线程对象指向了创建`Looper`的线程 |
| 13 | +2. 创建了一个新的`MessageQueue` |
| 14 | + |
| 15 | +分析完构造函数之后,接下来我们主要分析两个方法: |
| 16 | +1. `looper.loop()` |
| 17 | +2. `looper.prepare()` |
| 18 | + |
| 19 | +###looper.loop()(在当前线程启动一个Message loop机制,此段代码将直接分析出Looper、Handler、Message、MessageQueue的关系) |
| 20 | +``` |
| 21 | + public static void loop() { |
| 22 | + final Looper me = myLooper();//获得当前线程绑定的Looper |
| 23 | + if (me == null) { |
| 24 | + throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); |
| 25 | + } |
| 26 | + final MessageQueue queue = me.mQueue;//获得与Looper绑定的MessageQueue |
| 27 | +
|
| 28 | + // Make sure the identity of this thread is that of the local process, |
| 29 | + // and keep track of what that identity token actually is. |
| 30 | + Binder.clearCallingIdentity(); |
| 31 | + final long ident = Binder.clearCallingIdentity(); |
| 32 | + |
| 33 | + //进入死循环,不断地去取对象,分发对象到Handler中消费 |
| 34 | + for (;;) { |
| 35 | + Message msg = queue.next(); // 不断的取下一个Message对象,在这里可能会造成堵塞。 |
| 36 | + if (msg == null) { |
| 37 | + // No message indicates that the message queue is quitting. |
| 38 | + return; |
| 39 | + } |
| 40 | +
|
| 41 | + // This must be in a local variable, in case a UI event sets the logger |
| 42 | + Printer logging = me.mLogging; |
| 43 | + if (logging != null) { |
| 44 | + logging.println(">>>>> Dispatching to " + msg.target + " " + |
| 45 | + msg.callback + ": " + msg.what); |
| 46 | + } |
| 47 | + |
| 48 | + //在这里,开始分发Message了 |
| 49 | + //至于这个target是神马?什么时候被赋值的? |
| 50 | + //我们一会分析Handler的时候就会讲到 |
| 51 | + msg.target.dispatchMessage(msg); |
| 52 | +
|
| 53 | + if (logging != null) { |
| 54 | + logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); |
| 55 | + } |
| 56 | +
|
| 57 | + // Make sure that during the course of dispatching the |
| 58 | + // identity of the thread wasn't corrupted. |
| 59 | + final long newIdent = Binder.clearCallingIdentity(); |
| 60 | + if (ident != newIdent) { |
| 61 | + Log.wtf(TAG, "Thread identity changed from 0x" |
| 62 | + + Long.toHexString(ident) + " to 0x" |
| 63 | + + Long.toHexString(newIdent) + " while dispatching to " |
| 64 | + + msg.target.getClass().getName() + " " |
| 65 | + + msg.callback + " what=" + msg.what); |
| 66 | + } |
| 67 | + |
| 68 | + //当分发完Message之后,当然要标记将该Message标记为 *正在使用* 啦 |
| 69 | + msg.recycleUnchecked(); |
| 70 | + } |
| 71 | + } |
| 72 | +``` |
| 73 | +*分析了上面的源代码,我们可以意识到,最重要的方法是:* |
| 74 | +1. `queue.next()` |
| 75 | +2. `msg.target.dispatchMessage(msg)` |
| 76 | +3. `msg.recycleUnchecked()` |
| 77 | + |
| 78 | +其实Looper中最重要的部分都是由`Message`、`MessageQueue`组成的有木有!这段最重要的代码中涉及到了四个对象,他们与彼此的关系如下: |
| 79 | +1. `MessageQueue`:装食物的容器 |
| 80 | +2. `Message`:被装的食物 |
| 81 | +3. `Handler`(msg.target实际上就是`Handler`):食物的消费者 |
| 82 | +4. `Looper`:负责分发食物的人 |
| 83 | + |
| 84 | + |
| 85 | +###looper.prepare()(在当前线程关联一个Looper对象) |
| 86 | +``` |
| 87 | + private static void prepare(boolean quitAllowed) { |
| 88 | + if (sThreadLocal.get() != null) { |
| 89 | + throw new RuntimeException("Only one Looper may be created per thread"); |
| 90 | + } |
| 91 | + //在当前线程绑定一个Looper |
| 92 | + sThreadLocal.set(new Looper(quitAllowed)); |
| 93 | + } |
| 94 | +``` |
| 95 | +以上代码只做了两件事情: |
| 96 | +1. 判断当前线程有木有`Looper`,如果有则抛出异常(在这里我们就可以知道,Android规定一个线程只能够拥有一个与自己关联的`Looper`)。 |
| 97 | +2. 如果没有的话,那么就设置一个新的`Looper`到当前线程。 |
| 98 | + |
| 99 | +-------------- |
| 100 | +##Handler |
| 101 | +由于我们使用Handler的通常性的第一步是: |
| 102 | +``` |
| 103 | + Handler handler = new Handler(){ |
| 104 | + //你们有没有很好奇这个方法是在哪里被回调的? |
| 105 | + //我也是!所以接下来会分析到哟! |
| 106 | + @Override |
| 107 | + public void handleMessage(Message msg) { |
| 108 | + //Handler your Message |
| 109 | + } |
| 110 | + }; |
| 111 | +``` |
| 112 | +所以我们先来分析`Handler`的构造方法 |
| 113 | +``` |
| 114 | +//空参数的构造方法与之对应,这里只给出主要的代码,具体大家可以到源码中查看 |
| 115 | +public Handler(Callback callback, boolean async) { |
| 116 | + //打印内存泄露提醒log |
| 117 | + .... |
| 118 | + |
| 119 | + //获取与创建Handler线程绑定的Looper |
| 120 | + mLooper = Looper.myLooper(); |
| 121 | + if (mLooper == null) { |
| 122 | + throw new RuntimeException( |
| 123 | + "Can't create handler inside thread that has not called Looper.prepare()"); |
| 124 | + } |
| 125 | + //获取与Looper绑定的MessageQueue |
| 126 | + //因为一个Looper就只有一个MessageQueue,也就是与当前线程绑定的MessageQueue |
| 127 | + mQueue = mLooper.mQueue; |
| 128 | + mCallback = callback; |
| 129 | + mAsynchronous = async; |
| 130 | + |
| 131 | + } |
| 132 | +``` |
| 133 | +*带上问题:* |
| 134 | +1. `Looper.loop()`死循环中的`msg.target`是什么时候被赋值的? |
| 135 | +2. `handler.handleMessage(msg)`在什么时候被回调的? |
| 136 | + |
| 137 | +###A1:`Looper.loop()`死循环中的`msg.target`是什么时候被赋值的? |
| 138 | +要分析这个问题,很自然的我们想到从发送消息开始,无论是`handler.sendMessage(msg)`还是`handler.sendEmptyMessage(what)`,我们最终都可以追溯到以下方法 |
| 139 | +``` |
| 140 | +public boolean sendMessageAtTime(Message msg, long uptimeMillis) { |
| 141 | + //引用Handler中的MessageQueue |
| 142 | + //这个MessageQueue就是创建Looper时被创建的MessageQueue |
| 143 | + MessageQueue queue = mQueue; |
| 144 | + |
| 145 | + if (queue == null) { |
| 146 | + RuntimeException e = new RuntimeException( |
| 147 | + this + " sendMessageAtTime() called with no mQueue"); |
| 148 | + Log.w("Looper", e.getMessage(), e); |
| 149 | + return false; |
| 150 | + } |
| 151 | + //将新来的Message加入到MessageQueue中 |
| 152 | + return enqueueMessage(queue, msg, uptimeMillis); |
| 153 | + } |
| 154 | +``` |
| 155 | + |
| 156 | +我们接下来分析`enqueueMessage(queue, msg, uptimeMillis)`: |
| 157 | +``` |
| 158 | +private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { |
| 159 | + //显而易见,大写加粗的赋值啊! |
| 160 | + **msg.target = this;** |
| 161 | + if (mAsynchronous) { |
| 162 | + msg.setAsynchronous(true); |
| 163 | + } |
| 164 | + return queue.enqueueMessage(msg, uptimeMillis); |
| 165 | + } |
| 166 | +``` |
| 167 | + |
| 168 | + |
| 169 | +###A2:`handler.handleMessage(msg)`在什么时候被回调的? |
| 170 | +通过以上的分析,我们很明确的知道`Message`中的`target`是在什么时候被赋值的了,我们先来分析在`Looper.loop()`中出现过的过的`dispatchMessage(msg)`方法 |
| 171 | + |
| 172 | +``` |
| 173 | +public void dispatchMessage(Message msg) { |
| 174 | + if (msg.callback != null) { |
| 175 | + handleCallback(msg); |
| 176 | + } else { |
| 177 | + if (mCallback != null) { |
| 178 | + if (mCallback.handleMessage(msg)) { |
| 179 | + return; |
| 180 | + } |
| 181 | + } |
| 182 | + //看到这个大写加粗的方法调用没! |
| 183 | + **handleMessage(msg);** |
| 184 | + } |
| 185 | + } |
| 186 | +``` |
| 187 | + |
| 188 | +加上以上分析,我们将之前分析结果串起来,就可以知道了某些东西: |
| 189 | +`Looper.loop()`不断地获取`MessageQueue`中的`Message`,然后调用与`Message`绑定的`Handler`对象的`dispatchMessage`方法,最后,我们看到了`handleMessage`就在`dispatchMessage`方法里被调用的。 |
| 190 | + |
| 191 | +------------------ |
| 192 | +通过以上的分析,我们可以很清晰的知道Handler、Looper、Message、MessageQueue这四者的关系以及如何合作的了。 |
| 193 | + |
| 194 | +#总结: |
| 195 | +当我们调用`handler.sendMessage(msg)`方法发送一个`Message`时,实际上这个`Message`是发送到**与当前线程绑定**的一个`MessageQueue`中,然后**与当前线程绑定**的`Looper`将会不断的从`MessageQueue`中取出新的`Message`,调用`msg.target.dispathMessage(msg)`方法将消息分发到与`Message`绑定的`handler.handleMessage()`方法中。 |
| 196 | + |
| 197 | +一个`Thread`对应多个`Handler` |
| 198 | +一个`Thread`对应一个`Looper`和`MessageQueue`,`Handler`与`Thread`共享`Looper`和`MessageQueue`。 |
| 199 | +`Message`只是消息的载体,将会被发送到**与线程绑定的唯一的**`MessageQueue`中,并且被**与线程绑定的唯一的**`Looper`分发,被与其自身绑定的`Handler`消费。 |
| 200 | + |
| 201 | +------ |
| 202 | +- Enjoy Android :) 如果有误,轻喷,欢迎指正。 |
| 203 | + |
| 204 | + |
| 205 | + |
0 commit comments