异步消息机制-Handler

参考资料

Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系


目录

  • 1)概述
  • 2)分析
    • 2.1)ThreadLocal工作原理
    • 2.2)MessageQueue工作原理
    • 2.3)Looper工作原理
    • 2.4)Handler总结

1)概述

概述 说明
MessageQueue 并不是真正的队列,采用的是单链表的数据结构来存储消息列表
ThreadLocal 线程内部数据存储类,可以在指定线程内部存储数据
Looper 无限循环的处理消息,有新消息处理,无则阻塞
Handler 将一个任务切换到指定线程处理。注意:线程默认没有Looper的,需要自己创建。但主线程ActivityThread在创建的时候会初始化Looper,不用我们管
屏幕快照 2017-08-04 上午10.50.24.png
异步消息机制-Handler_第1张图片
屏幕快照 2017-08-04 上午10.50.41.png
异步消息机制-Handler_第2张图片
MQ单链表
异步消息机制-Handler_第3张图片
image

2)分析

2.1)ThreadLocal(线程的本地存储)工作原理

通过ThreadLocal可实现Looper在线程中的存取,以方便Handler获取当前线程的Looper。

//举例,不同线程访问同一ThreadLocal对象,值是不一样的
private ThreadLocal mThreadLocal = new ThreadLocal();

//主线程中设置
mThreadLocal.set(true);   //Log:  mThreadLocal.get() =  true
//子线程1
new Thread("Thread#1"){
  @override
  public void run(){
    mThreadLocal.set(false);  //Log:  mThreadLocal.get() = false
  }
}
//子线程2
new Thread("Thread#2"){
  @override
  public void run(){
    Log.i(mThreadLocal.get());  //Log:  null
  }
}
异步消息机制-Handler_第4张图片
ThreadLocal.set(T value)
异步消息机制-Handler_第5张图片
ThreadLocal.get()

2.2)MessageQueue工作原理

方法 说明
enqueueMessage(Message msg, long when) 插入消息
next() 读取消息并从消息队列移除。无限循环,如果MessageQueue中无消息,next()将会阻塞,有消息到来会返回此消息并从MQ中移除

MessageQueue是消息机制的Java层和C++层的连接纽带,大部分核心方法都交给native层来处理


异步消息机制-Handler_第6张图片
native方法

MQ构造

MessageQueue(boolean quitAllowed) {
    mQuitAllowed = quitAllowed;
    //通过native方法初始化消息队列,其中mPtr是供native代码使用
    mPtr = nativeInit();
}
  • MessageQueue是按照Message触发时间的先后顺序排列的,队头的消息是将要最早触发的消息。当有消息需要加入消息队列时,会从队列头开始遍历,直到找到消息应该插入的合适位置,以保证所有消息的时间顺序
    MessageQueue.enqueueMessage()
boolean enqueueMessage(Message msg, long when) {
    // 每一个普通Message必须有一个target
    if (msg.target == null) {
        throw new IllegalArgumentException("Message must have a target.");
    }
    if (msg.isInUse()) {
        throw new IllegalStateException(msg + " This message is already in use.");
    }
    synchronized (this) {
        if (mQuitting) {  //正在退出时,回收msg,加入到消息池
            msg.recycle();
            return false;
        }
        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        if (p == null || when == 0 || when < p.when) {
            //p为null(代表MessageQueue没有消息) 或者msg的触发时间是队列中最早的, 则进入该该分支
            msg.next = p;
            mMessages = msg;
            needWake = mBlocked; //当阻塞时需要唤醒
        } else {
            //将消息按时间顺序插入到MessageQueue。一般地,不需要唤醒事件队列,除非
            //消息队头存在barrier,并且同时Message是队列中最早的异步消息。
            needWake = mBlocked && p.target == null && msg.isAsynchronous();
            Message prev;
            for (;;) {
                prev = p;
                p = p.next;
                if (p == null || when < p.when) {
                    break;
                }
                if (needWake && p.isAsynchronous()) {
                    needWake = false;
                }
            }
            msg.next = p;
            prev.next = msg;
        }
        //消息没有退出,我们认为此时mPtr != 0
        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true;
}

MessageQueue.next()

Message next() {
    final long ptr = mPtr;
    if (ptr == 0) { //当消息循环已经退出,则直接返回
        return null;
    }
    int pendingIdleHandlerCount = -1; // 循环迭代的首次为-1
    int nextPollTimeoutMillis = 0;
    for (;;) {
        if (nextPollTimeoutMillis != 0) {
            Binder.flushPendingCommands();
        }
        //阻塞操作,当等待nextPollTimeoutMillis时长,或者消息队列被唤醒,都会返回
        nativePollOnce(ptr, nextPollTimeoutMillis);
        synchronized (this) {
            final long now = SystemClock.uptimeMillis();
            Message prevMsg = null;
            Message msg = mMessages;
            if (msg != null && msg.target == null) {
                //当消息Handler为空时,查询MessageQueue中的下一条异步消息msg,则退出循环。
                do {
                    prevMsg = msg;
                    msg = msg.next;
                } while (msg != null && !msg.isAsynchronous());
            }
            if (msg != null) {
                if (now < msg.when) {
                    //当异步消息触发时间大于当前时间,则设置下一次轮询的超时时长
                    nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                } else {
                    // 获取一条消息,并返回
                    mBlocked = false;
                    if (prevMsg != null) {
                        prevMsg.next = msg.next;
                    } else {
                        mMessages = msg.next;
                    }
                    msg.next = null;
                    //设置消息的使用状态,即flags |= FLAG_IN_USE
                    msg.markInUse();
                    return msg;   //成功地获取MessageQueue中的下一条即将要执行的消息
                }
            } else {
                //没有消息
                nextPollTimeoutMillis = -1;
            }
            //消息正在退出,返回null
            if (mQuitting) {
                dispose();
                return null;
            }
            //当消息队列为空,或者是消息队列的第一个消息时
            if (pendingIdleHandlerCount < 0 && (mMessages == null || now < mMessages.when)) {
                pendingIdleHandlerCount = mIdleHandlers.size();
            }
            if (pendingIdleHandlerCount <= 0) {
                //没有idle handlers 需要运行,则循环并等待。
                mBlocked = true;
                continue;
            }
            if (mPendingIdleHandlers == null) {
                mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
            }
            mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
        }
        //只有第一次循环时,会运行idle handlers,执行完成后,重置pendingIdleHandlerCount为0.
        for (int i = 0; i < pendingIdleHandlerCount; i++) {
            final IdleHandler idler = mPendingIdleHandlers[i];
            mPendingIdleHandlers[i] = null; //去掉handler的引用
            boolean keep = false;
            try {
                keep = idler.queueIdle();  //idle时执行的方法
            } catch (Throwable t) {
                Log.wtf(TAG, "IdleHandler threw exception", t);
            }
            if (!keep) {
                synchronized (this) {
                    mIdleHandlers.remove(idler);
                }
            }
        }
        //重置idle handler个数为0,以保证不会再次重复运行
        pendingIdleHandlerCount = 0;
        //当调用一个空闲handler时,一个新message能够被分发,因此无需等待可以直接查询pending message.
        nextPollTimeoutMillis = 0;
    }
}

MessageQueue.quit()

void quit(boolean safe) {
        // 当mQuitAllowed为false,表示不运行退出,强行调用quit()会抛出异常
        if (!mQuitAllowed) {
            throw new IllegalStateException("Main thread not allowed to quit.");
        }
        synchronized (this) {
            if (mQuitting) { //防止多次执行退出操作
                return;
            }
            mQuitting = true;
            if (safe) {
                removeAllFutureMessagesLocked(); //移除尚未触发的所有消息
            } else {
                removeAllMessagesLocked(); //移除所有的消息
            }
            //mQuitting=false,那么认定为 mPtr != 0
            nativeWake(mPtr);
        }
    }

2.3)Looper工作原理

Looper构造方法

private Looper(boolean quitAllowed){
  //创建MQ
  mQueue = new MessageQueue(quitAllowed);
  //记录当前线程
  mThread = Thread.currentThread();
}

Looper.prepare()

public static final void prepare() {  
        //每个线程只允许执行一次该方法,第二次执行会抛出异常
        if (sThreadLocal.get() != null) {  
            throw new RuntimeException("Only one Looper may be created per thread");  
        }  
        sThreadLocal.set(new Looper(true));  
}  

Looper.loop()会循环调用MessageQueue的next()来获取消息,而next()是一个阻塞操作,当没有消息时,next()会阻塞,导致loop()也一直阻塞。

public static void loop() {  
        //myLooper() => return sThreadLocal.get();
        // 获取sThreadLocal存储的Looper实例,每个线程是不一样的
        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);  
            }  
  
            //交给Handler的dispatch去处理
            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);  
            }  
            //把分发后的Message回收到消息池,以便重复利用
            msg.recycle();  
        }  
}  
//为子线程创建Looper
//在没有Looper的子线程中创建Handler会报错!!!!!`
new Thread("Thread#2"){
  @override
  public void run(){
    Looper.prepare();
    Handler handler = new Handler();
    Looper.loop();
  }
}
方法 说明
loop() 无限循环调用MQ的next()获取消息
getMainLooper() 任何地方获取主线程Looper
quit 立刻退出Looper
quitSafely 安全退出,会将MQ中的消息处理完毕

PS:子线程中创建的Looper,完成任务后应quit,否则此子线程会一直处于等待状态。

public void quit() {
    mQueue.quit(false); //消息移除
}
public void quitSafely() {
    mQueue.quit(true); //安全地消息移除
}

2.4)Handler总结

  • Looper.prepare()通过ThreadLocal在本线程保存Looper实例,只能调用一次,否则异常。其保存的MQ对象也只能是一个。
  • Looper.loop()无限循环读MQ
  • Handler的构造方法中获取当前线程保存的Looper实例
  • 通过Looper获取MessageQueue
//构造方法
...
mLooper = Looper.myLooper();  //从当前线程的TLS中获取Looper对象
...
mQueue = mLooper.mQueue;  //消息队列,来自Looper对象
  • sendXXX调用MQ的enqueueMessage(),然后加入MQ中


    异步消息机制-Handler_第7张图片
    最终都是调用MessageQueue.enqueueMessage()
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {  
       //将自身赋予msg.target
       //方便loop()中的msg.target.dispatchMessage(msg)
       msg.target = this;  
       if (mAsynchronous) {  
           msg.setAsynchronous(true);  
       }  
       return queue.enqueueMessage(msg, uptimeMillis);  
   } 
  • Looper.loop()中的msg.target.dispatchMessage(msg)最终会进入handleMessage

2.5)消息池

  • obtain
    从消息池取Message,都是把消息池表头的Message取走,再把表头指向next
public static Message obtain() {
    synchronized (sPoolSync) {
        if (sPool != null) {
            Message m = sPool;
            sPool = m.next;
            m.next = null; //从sPool中取出一个Message对象,并消息链表断开
            m.flags = 0; // 清除in-use flag
            sPoolSize--; //消息池的可用大小进行减1操作
            return m;
        }
    }
    return new Message(); // 当消息池为空时,直接创建Message对象
}
  • recycle
    把不再使用的消息加入消息池
    将Message加入到消息池的过程,都是把Message加到链表的表头
public void recycle() {
    if (isInUse()) { //判断消息是否正在使用
        if (gCheckRecycle) { //Android 5.0以后的版本默认为true,之前的版本默认为false.
            throw new IllegalStateException("This message cannot be recycled because it is still in use.");
        }
        return;
    }
    recycleUnchecked();
}

//对于不再使用的消息,加入到消息池
void recycleUnchecked() {
    //将消息标示位置为IN_USE,并清空消息所有的参数。
    flags = FLAG_IN_USE;
    what = 0;
    arg1 = 0;
    arg2 = 0;
    obj = null;
    replyTo = null;
    sendingUid = -1;
    when = 0;
    target = null;
    callback = null;
    data = null;
    synchronized (sPoolSync) {
        //静态变量MAX_POOL_SIZE代表消息池的可用大小;消息池的默认大小为50
        if (sPoolSize < MAX_POOL_SIZE) { //当消息池没有满时,将Message对象加入消息池
            next = sPool;
            sPool = this;
            sPoolSize++; //消息池的可用大小进行加1操作
        }
    }
}

你可能感兴趣的:(异步消息机制-Handler)