Android异步线程,Looper, MessageQueue
来源:互联网 发布:理光软件研究所业务 编辑:程序博客网 时间:2024/05/19 02:39
Looper类
Looper类有一个静态成员变量mThreadLocal,该类提供线程局部存储(Thread Local Storage),也就是内部存储的变量的作用域是线程,内部使用了Thread.getCurrentThread作为key来获取变量。
调用Looper类的静态方法prepare(),可以为当前线程创建Looper对象,并且把Looper对象放到线程局部存储里,MessageQueue是Looper的成员,他们是聚合关系。
public static void prepare() { prepare(true);}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的静态方法loop(),来使当前线程进入处理Message的循环。
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; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg); if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked(); } }
代码中在用死循环来处理MessageQueue中的Message,其中msg.target就是sendMessage的handler,所以target.dispatchMessage就是以下代码
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
调用了handleMessage,也就是我们平常所实现的handler的handleMessage方法。
0 0
- Android异步线程,Looper, MessageQueue
- Android线程 异步 asynctask Looper MessageQueue handler
- Android异步消息处理线程之----Looper+MessageQueue+Handler
- Looper,MessageQueue,Handler,异步消息线程
- Android异步消息处理 Handler Looper MessageQueue
- 1.Android内核 异步线程的实现(Looper、MessageQueue、Handler)
- Android线程间通信 Handler、Looper、MessageQueue
- Android线程—Handler、Looper、Message、MessageQueue
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解 .
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理三:Handler+Looper+MessageQueue深入详解 .
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- UNPv2第七章:互斥锁与条件变量
- nyoj14 会场安排问题
- java复习一——java 语言基础
- iOS开发总结之UIScrollView主要属性
- java报错:Can't load IA 32-bit .dll on a AMD 64-bit platform
- Android异步线程,Looper, MessageQueue
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- FragmentTransaction与Fragment生命周期的关系
- 阿里百川 用户反馈(feedback) 与 (com.umeng.lib.xxxxx 及 和 alipaySDK-xxxxx) 冲突的问题。
- Git详解之二 Git基础
- select版的TCP通信
- continue VS break
- linux系统调用和库函数调用的区别
- UNPv2第八章:读写锁