android异步消息处理主要由四部分组成:Handler,Looper,Message,MessageQueue
Message:线程之间传递的消息,可以在内部携带少量消息
MessageQueue:
Looper:每个线程有且最多只能有一个Looper对象,它是一个ThreadLocal(线程本地存储对象);
(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()方法调用后线程开始不断从队列中取出消息执行
Handler:1.用于处理和发送消息;
2.创建时关联一个Looper和Looper中的MQ;
3.一个线程可以有多个Handler;
4.Handler可在任意线程发送消息,消息会被添加到关联的MQ中;
5.Handler是在它关联的Looper线程中处理消息的。