Skip to content

Commit d087943

Browse files
authored
Update 2.Android线程间通信之Handler消息机制.md
1 parent 356f5de commit d087943

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

OperatingSystem/AndroidKernal/2.Android线程间通信之Handler消息机制.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private Looper(boolean quitAllowed) {
179179
mThread = Thread.currentThread();
180180
}
181181
```
182-
从代码上可以看到prepare()方法的作用是创建一个Looper对象,然后将该Looper对象保存到sThreadLocal中。
182+
从代码上可以看到prepare()方法的作用是创建一个Looper对象,然后将该Looper对象保存到sThreadLocal中。也就是说,prepare函数通过ThreadLocal机制,巧妙地把Looper和调用prepare的线程(也就是最终的处理线程)绑定在一起了。当事件源向这个Looper发送消息的时候,其实是把消息加到这个Looper的消息队列里了。那么,该消息就将由和Looper绑定的处理线程来处理。
183183

184184
这里有点麻烦了,我们上面使用的代码中并没有调用Looper.prepare()方法啊,理论上这里应该是null,Handler是无法使用的,为什么我们还能正常使用Handler?Looper.prepare()究竟是什么时候调用的?那我们需要看一下prepare()和prepare(boolean quitAllowed)方法都有哪些地方调用了:
185185

@@ -921,4 +921,4 @@ public class MyActivity extends Activity {
921921
---
922922

923923
- 邮箱 :charon.chui@gmail.com
924-
- Good Luck!
924+
- Good Luck!

0 commit comments

Comments
 (0)