Handler分析–①Message

Message是什么

Message是包含描述与任意数据对象的消息,可以发送到Handler.这个对象包含int类型的what/arg1/agr2和Object类型的obj以及Runnable类型的callback.我们可以选择使用其中的某些数据类型

Message怎么使用

参考Handler

Message源码分析

主要对外的数据类型

// 用户自定义的消息代码,以便收件人可以识别此消息的内容。
public int what;
// 使用下面4个设置需要传输的数据
public int arg1;
public int arg2;
public Object obj;
Bundle data;
// 消息需要被执行的时间,注意这里是 SystemClock.uptimeMillis()
// 表示系统开机到当前的时间总数,单位是毫秒,但是,当系统进入深度睡眠(CPU休眠、屏幕休眠、设备等待外部输入)时间就会停止
public long when;
// callback
Runnable callback;
复制代码

Message全局池

全局池(缓存池)主要作用就是避免在多数情况下重新创建新对象.

// class锁
public static final Object sPoolSync = new Object();
// Message池,以单链表的形式实现
private static Message sPool;
Message next;
// 目前池的大小
private static int sPoolSize = 0;
// 池的最大容量
private static final int MAX_POOL_SIZE = 50;
// Message正在使用/已被回收的标志位
int flags;
// 回收Message,判断标志位状态,异常时要不要报错,默认报错
private static boolean gCheckRecycle = true;

// 从池中获取一个Message实例对象
public static Message obtain() {
    // 线程安全的单链表移除数据操作,移除并获取队首数据
    synchronized (sPoolSync) {
        // 判空操作,常量池没有数据直接new,有的话则拿出一个返回,单链表操作
        if (sPool != null) {
            Message m = sPool;
            sPool = m.next;
            m.next = null;
            // 把flag设置为0,池中的状态 flags = FLAG_IN_USE
            m.flags = 0; // clear in-use flag
            sPoolSize--;
            return m;
        }
    }
    return new Message();
}

// 带状态检查的回收
public void recycle() {
    // 判断flag状态 
    if (isInUse()) {
        // flag != 0时根据gCheckRecycle值判断要不要报错(默认是报错的)
        if (gCheckRecycle) {
            throw new IllegalStateException("This message cannot be recycled because it "
                    + "is still in use.");
        }
        return;
    }
    // 不检查状态回收
    recycleUnchecked();
}

// 不检查状态回收
void recycleUnchecked() {
    // 设置flag为已回收状态
    flags = FLAG_IN_USE;
    // 数据初始化
    what = 0;
    arg1 = 0;
    arg2 = 0;
    obj = null;
    replyTo = null;
    sendingUid = UID_NONE;
    workSourceUid = UID_NONE;
    when = 0;
    target = null;
    callback = null;
    data = null;

    // 线程安全的单链表增加数据操作,把回收的Message放在队首
    synchronized (sPoolSync) {
        if (sPoolSize < MAX_POOL_SIZE) {
            next = sPool;
            sPool = this;
            sPoolSize++;
        }
    }
}
复制代码

Message在哪里进行回收的呢?

在Looper.loop中进行处理Message与回收的

...
// 获取本线程对应的MessageQueue实例(每一个线程只有一个MessageQueue)
final MessageQueue queue = me.mQueue;
...
for (;;) {
    // 阻塞获取message
    Message msg = queue.next();
    ...
    // 分发处理Message
    msg.target.dispatchMessage(msg);
    ...
    // 调用Message的不检查状态回收操作进行回收.
    msg.recycleUnchecked();
}
复制代码

参考

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享