android异步消息处理机制

本文详细介绍了Android中的消息处理机制,包括Handler、Looper、Message及MessageQueue等核心组件的工作原理及其相互之间的协作方式。

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线程中处理消息的。


转载于:https://my.oschina.net/u/2350638/blog/498939

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值