android异步消息处理机制

android异步消息处理主要由四部分组成:Handler,Looper,Message,MessageQueuejava

Message:线程之间传递的消息,能够在内部携带少许消息android

MessageQueue:异步

Looper:每一个线程有且最多只能有一个Looper对象,它是一个ThreadLocal(线程本地存储对象);oop

               (ThreadLocal:实现一个线程本地存储对象,对于一个变量,每一个线程都要各自的值,全部线程共享相同的对象,可是其中一个线程中的变量改变不会影响其余线程中该变量的值)线程

/**

 * Implements a thread-local storage, that is, a variable for which each thread

 * has its own value. All threads share the same 
{@code
 ThreadLocal} object,

 * but each sees a different value when accessing it, and changes made by one

 * thread do not affect the other threads. The implementation supports

 * 
{@code
 null} values.

 *

 * @see java.lang.Thread

 * @author Bob Lee

 */

               Looper内部维护了一个MQ,loop()方法调用后线程开始不断从队列中取出消息执行code

Handler:1.用于处理和发送消息;对象

                2.建立时关联一个Looper和Looper中的MQ;队列

                3.一个线程能够有多个Handler;it

                4.Handler可在任意线程发送消息,消息会被添加到关联的MQ中;io

                5.Handler是在它关联的Looper线程中处理消息的。

相关文章
相关标签/搜索